Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Controllers
  3. OpenHAB
  4. OpenHAB/MQTT Tips & Hints

OpenHAB/MQTT Tips & Hints

Scheduled Pinned Locked Moved OpenHAB
40 Posts 17 Posters 57.8k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • YveauxY Offline
    YveauxY Offline
    Yveaux
    Mod
    wrote on last edited by Yveaux
    #12

    Btw. there's one important thing to understand when using the Perl script. When it receives values from a sensor node through the gateway it's easy to publish the value in the topic-tree.
    When a value has to be sent to an actuator node, the story is different as the script has to know which topic to subscribe to at the MQTT broker.
    Currently when a mysensor node requests a value from the gateway the script automagically translates this into a subscription of the corresponding topic with the MQTT broker. The dimmer node for example, calls gw.request(childID, V_DIMMER) from setup to subscribe itself to dimmer messages.
    Note that sometimes this request gets lost (CRC error or so) and the subscription fails. While not supported by the MySensors library, a robust implementation should wait for a response after requesting this value and retry when it doesn't come.
    The Perl script stores all current subscriptions persistent (file subscriptions.storage) so restarting the Perl script will not force you to restart all nodes to subscribe again.

    http://yveaux.blogspot.nl

    1 Reply Last reply
    0
    • YveauxY Offline
      YveauxY Offline
      Yveaux
      Mod
      wrote on last edited by
      #13

      And another small one; an RGB dimmer!

      MQTT topics (exposed by the MQTT perl script. Node 118 = 3-channel dimmable light):

      /mySensors/118/0/V_DIMMER          dimmable light RED, accepting integer value between 0 and 100
      /mySensors/118/1/V_DIMMER          dimmable light GREEN, accepting integer value between 0 and 100
      /mySensors/118/2/V_DIMMER          dimmable light BLUE, accepting integer value between 0 and 100
      

      Items:

      Dimmer Light_R      {mqtt=">[server:/mySensors/118/0/V_DIMMER:state:*:default]"}
      Dimmer Light_G      {mqtt=">[server:/mySensors/118/1/V_DIMMER:state:*:default]"}
      Dimmer Light_B      {mqtt=">[server:/mySensors/118/2/V_DIMMER:state:*:default]"}
      Color Light_RGB 	"RGB Dimmer" 	(Lights)
      

      Rules to distribute colorwheel value over R,G,B dimmers:

      rule "Set RGB value"
      when
              Item Light_RGB changed
      then
              var HSBType hsbValue = Light_RGB.state as HSBType
              postUpdate( Light_R, hsbValue.red.intValue )
              postUpdate( Light_G, hsbValue.green.intValue )
              postUpdate( Light_B, hsbValue.blue.intValue )
      end
      

      No button control here; just using the colorwheel and on/off buttons in the GUI.

      Enjoy!

      http://yveaux.blogspot.nl

      siodS E 2 Replies Last reply
      0
      • YveauxY Yveaux

        This topic was split off http://forum.mysensors.org/topic/301/the-best-way-to-connect-to-openhab to discuss tips and hints specific to using MySensors together with MQTT middleware and OpenHAB.
        If you made something nice using this combination or have some questions/issues please post them in here!


        @kolaf I just started experimenting with openhab and find it quite hard to get started. There's some documentation ( far from complete, especially when you're just starting) but the general impression is that it's very powerful, mainly due to all the programming options. I'm quite sure you could directly talk to a mysensors gateway using the serial protocol, using e.g. rules, if you want.
        I doubt however if you want to write your own protocol handler...
        Anyway, I'm currently using an Ethernet gateway which tasks to a Perl script I wrote (https://github.com/Yveaux/MySensors_MQTTGateway) that does the conversion to and from MQTT. This script is a MQTT client that runs on the same server I run openhab and the mosquitto broker on. This is the only difference compared to @Damme solution who runs a broker on the gateway.
        I like the flexibility of storing & accessing all data through an MQTT broker which makes up for the apparent overkill going through MQTT just to connect mysensors to openhab.
        As long as your server has enough resources to run it all its not really worth the effort to directly talk to my sensors or create an openhab binding for it.

        DammeD Offline
        DammeD Offline
        Damme
        Code Contributor
        wrote on last edited by Damme
        #14

        rule to calculate absolute humidity and dew point from degree Celsius and rH%

        import java.lang.Math
        import java.lang.Integer
        import java.lang.Double
        
        
        rule "Calculate absolute humidity (g h2o / m3 air) and dew point"
        when
            Item temp1 changed or
            Item hum1 changed
        then
            var temp = temp1.state as DecimalType
            var hum = hum1.state as DecimalType
        	
            var t1 = (17.271*temp.floatValue) / (237.7+temp.floatValue) + Math::log(hum.floatValue*0.01)
            var dew = (237.7 * t1) / (17.271 - t1)
            var Number c1 = ((17.67*temp.floatValue)/(temp.floatValue+243.5))
            var abs = (Math::pow(Math::E,c1.doubleValue)*6.112*2.1674*hum.floatValue) /(273.15+temp.floatValue)
        	
            Dewpoint1.postUpdate(dew)
            AbsHum1.postUpdate(abs)
        end
        
        YveauxY 1 Reply Last reply
        0
        • DammeD Damme

          rule to calculate absolute humidity and dew point from degree Celsius and rH%

          import java.lang.Math
          import java.lang.Integer
          import java.lang.Double
          
          
          rule "Calculate absolute humidity (g h2o / m3 air) and dew point"
          when
              Item temp1 changed or
              Item hum1 changed
          then
              var temp = temp1.state as DecimalType
              var hum = hum1.state as DecimalType
          	
              var t1 = (17.271*temp.floatValue) / (237.7+temp.floatValue) + Math::log(hum.floatValue*0.01)
              var dew = (237.7 * t1) / (17.271 - t1)
              var Number c1 = ((17.67*temp.floatValue)/(temp.floatValue+243.5))
              var abs = (Math::pow(Math::E,c1.doubleValue)*6.112*2.1674*hum.floatValue) /(273.15+temp.floatValue)
          	
              Dewpoint1.postUpdate(dew)
              AbsHum1.postUpdate(abs)
          end
          
          YveauxY Offline
          YveauxY Offline
          Yveaux
          Mod
          wrote on last edited by
          #15

          @Damme Nice calculation example! Thanks!

          http://yveaux.blogspot.nl

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dany
            wrote on last edited by
            #16

            Hi everyone!

            I'm new in the topic. I find it very interesting the world of mysensors.
            I would like to ask whether there is a description of someone that I can set my mqtt openHAB bindings eg .: an Arduino LED dimmer ?

            I already read http://forum.mysensors.org/topic/303/mqtt broker-gateway
            I made a Humidity sensor node, and Relay node, too.

            It would be good if we had a basic description or example project for beginners from all sensor type. :)

            1 Reply Last reply
            0
            • pgoP Offline
              pgoP Offline
              pgo
              wrote on last edited by pgo
              #17

              It is not rocket science to get the openHAB running w/MQTT gateway, see for example my post with DS/Light/Relay in http://forum.mysensors.org/topic/115/implementing-multiple-sensors/60

              But sure, it would be great to put a wiki with all sensor settings for openHAB together on one page. I needed to read/search for some days to put the knowledge together...

              Example of the openhab screenshots on mobile https://github.com/pgo-sk/mysensors/wiki/Home-automation-using-mysensors-and-openHAB
              There you see also the mapping of the sensor

              1 Reply Last reply
              1
              • A Offline
                A Offline
                adi32k
                wrote on last edited by
                #18

                How can I make openhad respod to gw.request(sensor, V_HEATER_SW,0);

                I have a relay actuator sketch and in setup() I have this

                for (int sensor=1 ; sensor<=NUMBER_OF_RELAYS;sensor++)
                {
                gw.present(sensor, S_HEATER);
                gw.request(sensor, V_HEATER_SW,0);
                }

                practically I would like openhab to respond to the gw.request with the actual state of the relay.

                My item definition is the follwing. I am able to ON and OFF the relay, but I need to find a way to get the values from openhab of the relays when the relay actuator arduino reboots.

                Switch Incalzire_Releu_GF_Living2 "Incalzire Releu Living 2" <heating> (Incalzire) {mqtt=">[mysensor:MyMQTT/3/2/V_HEATER_SW:command:ON:1],>[mysensor:MyMQTT/3/2/V_HEATER_SW:command:OFF:0]"}

                1 Reply Last reply
                0
                • raditvR Offline
                  raditvR Offline
                  raditv
                  wrote on last edited by
                  #19

                  is there any sample code for controlling RGB led here?

                  1 Reply Last reply
                  0
                  • ChaoticC Offline
                    ChaoticC Offline
                    Chaotic
                    wrote on last edited by
                    #20

                    Sorry to Necro this thread but had a question,

                    I believe I read somewhere that you can use a serial gateway connected to a pi and have openhab/mqtt run on the pi?

                    How would one go about setting this up? Currently I have 2 Unos one running as the serial gateway. I plan to replace the serial gateway with a nano I'm still waiting on that.

                    I just got the pi last night so I'm still working on getting everything up and running on that but I'd rather just connect the gateway directly to the pi rather than have to get a wifi/ethernet module for one of the arduinos.

                    Also probably outside the scope of this thread (and thats fine) but anyone have a good guide for setting up the pi for openhab/MQTT?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mhortman
                      wrote on last edited by
                      #21

                      @Chaotic , Try this..
                      https://github.com/abouillot/HomeAutomation/tree/master/piGateway
                      Worked great for me

                      SweebeeS 1 Reply Last reply
                      0
                      • M mhortman

                        @Chaotic , Try this..
                        https://github.com/abouillot/HomeAutomation/tree/master/piGateway
                        Worked great for me

                        SweebeeS Offline
                        SweebeeS Offline
                        Sweebee
                        wrote on last edited by
                        #22

                        @mhortman Didn't work for me:

                        sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list~
                        --2015-03-27 10:46:51--  http://repo.mosquitto.org/debian/mosquitto-wheezy.list~
                        Resolving repo.mosquitto.org (repo.mosquitto.org)... 85.119.83.194, 2001:ba8:1f1:f271::2
                        Connecting to repo.mosquitto.org (repo.mosquitto.org)|85.119.83.194|:80... connected.
                        HTTP request sent, awaiting response... 403 Forbidden
                        2015-03-27 10:46:51 ERROR 403: Forbidden.'
                        
                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          quocanhcgd
                          wrote on last edited by
                          #23

                          My house have 4 floor, i have plan build one gateway for each floor. Each floor has 4-5 sensors (temp, hum, relay, light, door, RF light). Each sensors use NRF24 to connect with gateway. Gateway connect to RAS by ethernet.
                          My question:

                          • Can i build 2 gateway mqtt connect to openhab?
                            if not, what my solution to solve ?
                            Thanks
                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            rachmat aditiya27
                            wrote on last edited by
                            #24

                            you can build more than 1 gateway for that, but rather than mqtt. I think it's better to use ethernet or serial gateway because mqtt gateway has lot of problem, I tried it for months. You can forward mqtt from fhem to mosquito.

                            1 Reply Last reply
                            1
                            • YveauxY Yveaux

                              And another small one; an RGB dimmer!

                              MQTT topics (exposed by the MQTT perl script. Node 118 = 3-channel dimmable light):

                              /mySensors/118/0/V_DIMMER          dimmable light RED, accepting integer value between 0 and 100
                              /mySensors/118/1/V_DIMMER          dimmable light GREEN, accepting integer value between 0 and 100
                              /mySensors/118/2/V_DIMMER          dimmable light BLUE, accepting integer value between 0 and 100
                              

                              Items:

                              Dimmer Light_R      {mqtt=">[server:/mySensors/118/0/V_DIMMER:state:*:default]"}
                              Dimmer Light_G      {mqtt=">[server:/mySensors/118/1/V_DIMMER:state:*:default]"}
                              Dimmer Light_B      {mqtt=">[server:/mySensors/118/2/V_DIMMER:state:*:default]"}
                              Color Light_RGB 	"RGB Dimmer" 	(Lights)
                              

                              Rules to distribute colorwheel value over R,G,B dimmers:

                              rule "Set RGB value"
                              when
                                      Item Light_RGB changed
                              then
                                      var HSBType hsbValue = Light_RGB.state as HSBType
                                      postUpdate( Light_R, hsbValue.red.intValue )
                                      postUpdate( Light_G, hsbValue.green.intValue )
                                      postUpdate( Light_B, hsbValue.blue.intValue )
                              end
                              

                              No button control here; just using the colorwheel and on/off buttons in the GUI.

                              Enjoy!

                              siodS Offline
                              siodS Offline
                              siod
                              wrote on last edited by
                              #25

                              @Yveaux
                              could you please post your arduino code of controlling your RGB (I guess it´s a RGB LED Strip!?)?

                              Thanks for your help!!

                              still learning...

                              1 Reply Last reply
                              0
                              • siodS Offline
                                siodS Offline
                                siod
                                wrote on last edited by
                                #26

                                I build up a mqtt gateway and configured it in openhab.cfg. Now I would like to set up a mosquitto server on a raspberry pi, too. Is it possible to add this new MQTT server to the openhab.cfg beside the already set up MQTT gateway?

                                still learning...

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  tomkxy
                                  wrote on last edited by
                                  #27

                                  Why not setup the Mosquitto broker connect OpenHab to it and use the MQTTClientGateway which connects as a client to the Mosquitto broker. Has been discussed at several places in the forum already. You can find the MQTTClientGateway in the development branch.

                                  siodS 1 Reply Last reply
                                  0
                                  • John ConnollyJ Offline
                                    John ConnollyJ Offline
                                    John Connolly
                                    wrote on last edited by
                                    #28
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • T tomkxy

                                      Why not setup the Mosquitto broker connect OpenHab to it and use the MQTTClientGateway which connects as a client to the Mosquitto broker. Has been discussed at several places in the forum already. You can find the MQTTClientGateway in the development branch.

                                      siodS Offline
                                      siodS Offline
                                      siod
                                      wrote on last edited by
                                      #29

                                      @tomkxy
                                      I´m reading in the forums for weeks now, but I don´t get it to work. I´ve succesfully set up Mosquitto on my raspi, also openHAB is running there. MQTTEthernetGateway (from Startpage) is pingable. One sensor is running and working.

                                      I don´t know how to get the MQTTEthernetGateway communicate with the Mosquitto Broker. I´ve read about "bridging" what made me more confused as I already was.

                                      So, right now, when I subscribe to MyMQTT/# using "mosquitto_sub -t MyMQTT/#" on my Raspi I don´t see any communication coming in.

                                      What do I have to do to get the MQTTEthernetGateway (=MQTTClientGateway I suppose!?!) connecting to Mosquitto?

                                      Or: must I setup the MQTTEthernetGateway like this: http://forum.mysensors.org/topic/524/mqtt-client-gateway instead of using the sketch from http://www.mysensors.org/build/mqtt_gateway?

                                      Thanks for your help!!

                                      still learning...

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        tomkxy
                                        wrote on last edited by
                                        #30

                                        @siod I am not sure to what code you are exactly referring to if you say MQTTEthernetGateway.
                                        In the mysensor Github he cleaned up the code I merged from my Github. I did not test that code so far.
                                        So I can only comment on the MQTTClientGateway you can find in my Github https://github.com/tomkxy/Arduino.git in branch MQTTClientGateway.

                                        I suggest you start off ignoring Openhab at the moment. Just test with clients using mosquitto_sub and mosquito_pub commands from mosquito.

                                        In the MQTTClientGateway sketch you need to adapt the IP related infos. See excerpt from my config.

                                        
                                        //replace with ip of server you want to connect to, comment out if using 'remote_host'
                                        uint8_t remote_ip[] =  { 192, 168, 178, 74 };  // Mosquitto broker
                                        
                                        //replace with hostname of server you want to connect to, comment out if using 'remote_ip'
                                        //char* remote_ip = "server.local";
                                        //replace with the port that your server is listening on
                                        #define remote_port 1883
                                        //replace with arduinos ip-address. Comment out if Ethernet-startup should use dhcp. Is ignored on Yun
                                        uint8_t local_ip[] = {192, 168, 178, 11};
                                        //replace with ethernet shield mac. It's mandatory every device is assigned a unique mac. Is ignored on Yun
                                        uint8_t mac[] = { 0xA2, 0xAE, 0xAD, 0xA0, 0xA0, 0xA2 };
                                        

                                        Depending on whether you use the signing feature or not you have to comment the define MY_SIGNING_FEATURE in my_config.h.

                                        If your sketch is running you should see one additional client connected on your mosquito broker.
                                        You can subscribe with mosquito_sub to $SYS/# which gives you all sorts of statistics.

                                        In order to check whether your Arduino connected to the broker you can also put a debug statement in the loop function. Just check and print the return code from the client.connect() call.

                                        If the connect did not work double check that you configured the correct IP address and port, and make also sure that you fulfill the authentication requirements configured in the broker.

                                        If the connection works publish a message to the broker for a topic like MyMQTT/27/5/V_PRESSURE
                                        The MQTTClientGateway sketch should receive that and you should see that in the log.

                                        Next reset your sensor node. You should see a message being processed by MQTTClientGateway and then being published to the MQTT broker.

                                        Only after all that works you start dealing with OpenHab.

                                        Hope that helps.

                                        1 Reply Last reply
                                        0
                                        • siodS Offline
                                          siodS Offline
                                          siod
                                          wrote on last edited by
                                          #31

                                          Hi tomkxy,

                                          thanks for your detailed answer, I will test all your suggestions after I got my GW work on LAN, right now it´s not answering to pings and I don´t get it why....

                                          I´ve uploaded the ntruchsees sketch from http://forum.mysensors.org/topic/524/mqtt-client-gateway now and configured IP address, port and MAC.
                                          When I was talking about MQTTEthernetGateway I was talking about the MySensors Sketch at http://forum.mysensors.org/topic/524/mqtt-client-gateway.

                                          I will get back to you and give your more info as soon as the Gateway in it´s actual state works in my network!

                                          still learning...

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          12

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular