MySensors --> MQTT --> OpenHab 2.5



  • I'm in the process of setting up my first install, so please bear with me, I'm strill trying to get my head around all this!

    I've got my first sensor node and an MQTT gateway, successfully publishing messages to Mosquitto. Now I need to setup OpenHAB to subscribe to those messages. From what I've read, I get the impression there are two options:

    I gather there have been efforts to get the MySensors binding officially incorporated into OpenHAB, but it looks like this hasn't been completed yet, so it seems that the MySensors binding is a much more "manual" to set up.

    Would both options work? What are the advantages/disadvantages of choosing one method versus the other?

    Any thoughts/recommendations?


  • Hero Member

    Hey @P72endragon!

    The right binding depends on the way you like to configure the things and nodes in Openhab. If you're using the text files for configuration the better choice may be the MQTT binding. The hardest part will be the initial configuration but from there it is copy and paste. Don't forget to use static node ids here.

    If you like to setup your configuration via PaperUI the binding may be the better choice, because the binding will discover new things in the MySensors network and you're able to link them to items with a few clicks.



  • Hi TimO,

    To be honest, I don't really mind which way I do it (PaperUI/text files). I have noted warnings about not mixing both together though! ;). A lot of the examples and forum posts I've seen use text files, so I'm tempted to go that route

    I've been having a play with it and I seem to have got OpenHAB to auto-discover my temperature sensor (it's displaying it in the inbox) and in the OpenHab log I can see the temperature readings:

    22:13:38.837 [DEBUG] [protocol.mqtt.MySensorsMqttConnection] - Message topic part: 1/0/1/0/0
    22:13:38.843 [DEBUG] [protocol.mqtt.MySensorsMqttConnection] - Converted MQTT message to MySensors Serial format. Sending on to bridge: 1;0;1;0;0;16.1
    22:13:38.858 [DEBUG] [orsAbstractConnection$MySensorsReader] - Message from gateway received: 1;0;1;0;0;16.1
    22:13:38.863 [DEBUG] [ors.internal.gateway.MySensorsGateway] - Node 1 found in gateway
    22:13:38.867 [DEBUG] [ors.internal.gateway.MySensorsGateway] - Child 0 found in node 1
    22:13:38.872 [DEBUG] [sensors.handler.MySensorsThingHandler] - Updating channel: temp(V_TEMP) value to: 16.1
    22:13:38.880 [DEBUG] [sensors.handler.MySensorsThingHandler] - Setting last update for node/child 1/0 to 2020-04-28T22:13:38.000+0100
    

    I'm still trying to get my head around Things, Items and Channels to get OpenHab to actually display this information, so I don't think I'm far away.

    Having said that, like I say, I'm always tempted with text file configs...
    If I go that route, do you happen to have any links to point me in the right direction?
    Where do I set the static node ID's - do they need to go in the sketches for each sensor node, or just in the .things/.items files?
    I think I need to find a good example of how to write the .things/.items files etc...


  • Hero Member

    The node id is set in the sketches via MY_NODE_ID define.
    I'm using the MQTT binding a lot, but not for MySensors. I'm using the MySensors binding with my ethernet gateway. So I can't give you a working example, but it should be possible. Stick to the MQTT binding documentation and the documentation of the MQTT gateway on the MySensors site.
    I myself use PaperUI to get familiar with new bindings and things and then copy for example the channelname from the ui in the text files to reduce typos. I don't have had any problems mixing both ways of configuration. Just be sure to only have one definition left at the end, either in UI or in text files.



  • Let me offer my thoughts and recommendations:

    For myself I have been using MQTT as the connection method for a number of years.

    Doing that, you must have an MQTT broker as the "middle-man" to receive MQTT messages from the MySensors MQTT gateway and then relaying them to OH, which then uses the MQTT binding. Then you do not use the MySensors binding to OH at all.

    The advantage is that you can have a number of sources/sensors feeding information to the MQTT broker. In my case I have three geographical sites with MySensors nodes, each with an MySensors MQTT gateway, feeding the broker. Actually at one of the sites there is also a Sonoff switch that also reports status and gets commands via MQTT. (I am using the Espurna SW on the Sonoff).

    Then my OH installation subscribes to the broker via the OH MQTT binding.
    To dive deeper into details, because of this geographical set-up, I am using a commercial MQTT broker, which is accessible over the internet from all three sites. If you only operate one site, then you are better off using e.g. Mosquitto inside your firewall.

    As far as I understand the OH MQTT binding does not offer auto-discovery unless you use the Homie naming convention. Check the OH documentation please.

    For MySensors nodes I use the MySensors notation so a typical message looks like: mqttgw1/28/3/1/0/0 23.2 for say, a node-ID 28 and child-ID 3 that reports a temperature of 23.2 °C

    When I started using MQTT and the 1.0 binding I made all definitions in the .items text file but when I converted to MQTT 2.0 I have made all the definitions in the PaperUI. It is either/or depending on your preferences but I think the general recommendation in the OH community is to use the PaperUI.

    If you stick with text files I strongly recommend that you use Visual Studio Code and the OpenHAB extension to VS. Then you get syntax checking and nice colours to all keywords.

    As far as I understand, the MySensors OH-binding uses the ethernet or serial MySensors GW. Probably the set-up is simpler in the beginning but offers less flexibility in the long run. I could never have achieved my set-up above using just a serial connection.



  • I second this. I went the mqtt route as well. Auto discovery is cool, but i wanted some additional control. My openhab 2.5 I don't use the UI at all. I just all flat files. This is a preference for me. I also use the VSCode extension for openhab and it works BEAUTIFULLY!



  • Thank you for your comments, it sounds like it's worth trying to get the native OH MQTT working (I don't mind not having auto-discovery - I won't be adding things so regular, once I get it figured out I'm OK with adding things manually).

    I've managed to setup PaperUI sufficiently to get the temperature from my sensor to display on PaperUI's control screen (although I'm not sure I understand quite how it works yet, still a little unclear on Items, Channels, etc)

    @CrankyCoder : I don't suppose you'd mind sharing some snippets of your config files (or point me at some good examples) to get me started - maybe showing a broker and something like a temperature sensor?



  • @P72endragon
    I also use MQTT instead of the binding. Major reason: when I started, it wasn't out and I had the serialGW connected. Once realized I want the controller in a different place, I switched to MQTT and never regret. I feel I have more control with the text files than I have with the UI-driven way.

    First of all, I highly recommend you download MQTT-spy or similar, this will help a lot in figuring out the right notation in OpenHAB. It will just listen to the traffic and log it. You could also use it to send test messages etc.

    Example:
    Gateway configuration

    // Set this node's subscribe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "hefti-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "hefti-in"
    
    // Set MQTT client id
    #define MY_MQTT_CLIENT_ID "hefti-1"
    

    Node configuration (temperature sensor)

    #define CHILD_ID_TEMP 1
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    present(CHILD_ID_TEMP, S_TEMP);
    

    OH example.items

    Number  roofTemp01              "Temperature roof [%.1f °C]" <temperature> (Temperature, Outdoor) {mqtt="<[mosquitto:hefti-out/101/1/1/0/0:state:default]"}
    

    MQTT spy of a recent message:
    mqtt-spy.JPG
    That is basically it. Depending on how you want to display the result (sitemap, PaperUI etc). As you see, the item is displayed in Temperature and Outdoor but you could also specify it directly in the sitemap but I guess this is going too far here.
    Hope that helped.



  • That's great, thank you, I'll have a play and see how far I can get... (yeah, I've already been using MQTT Explorer to understand the message structure)


Log in to reply
 

Suggested Topics

150
Online

9.4k
Users

10.0k
Topics

104.5k
Posts