OpenHab Tests


  • Hardware Contributor

    Hello, I mounted a OpenHab machine in a test environment (so I can test my scenarios before going into production). In my system I have OpenHab up and running and MQTT running and working! I made a small button to test the MQTT and it worked:

    Switch mySwitch {mqtt=">[mybroker:/myhouse/office/light:command:ON:1],>[mybroker:/myhouse/office/light:command:OFF:0]"}
    

    I'm using MQTT.fx to capture the MQTT requests, and when i toggle the button on I get "/myhouse/office/light 1" and when I toggle it off "/myhouse/office/light 0". So far so good.

    The problem is when I push a notification, OpenHab does not capture the request because the switch is set to "Outbound (>)". If I set it to "Inbound (<)" and I push a request I can see the button changing, but now if I press the button no request is generated.

    How can I solve this? I made a change to the code to become:

    Switch mySwitch {mqtt=">[mybroker:/myhouse/office/lightin:command:ON:1],>[mybroker:/myhouse/office/lightin:command:OFF:0],<[mybroker:/myhouse/office/lightout:command:ON:1],<[mybroker:/myhouse/office/lightout:command:OFF:0]"}
    

    This worked, I push the button and a request is generated, I push a notification and the button changes, but I don't know if this is the best solution. (side note, I used "lightin" and "lightout" because if I set the same name in the vars I would get a infinite loop).

    Thank you all!



  • You may find it useful to take a look at the definitions Openhab MQTT bindings: https://github.com/openhab/openhab/wiki/MQTT-Binding.

    The format for inbound and outbound messages are different. In your incoming definitions there is a transformation of "ON" or "OFF" and there is a regex filter of "1" or "0" so the question is if these definitions can be used for anything (incoming is "direction, broker, topic, type, transformation, regexfilter"

    It is a bit difficult to understand what you mean by "The problem is when I push a notification", from where do you push something, in what format and what shall it be used for? Type for Openhab messages which can be bound to an item is either a state change or a command´,


  • Hardware Contributor

    Hello I have replaced the code with

    Switch node2_sw2  "sw2 send + recieve example"  (node2,all)      {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:ON:1],>[mysensor:MyMQTT/21/2/V_LIGHT:command:OFF:0],<[mysensor:MyMQTT/21/2/V_LIGHT:command:MAP(1on0off.map)]"}
    

    But the problem persists, the map function keeps sending the same request over and over again, If I make a click in a few seconds my mqtt client is full of requests, over 500 with the same message. Wasn't it supposed to send only one message for click?



  • @soloam Your binding actually is:

    Switch node2_sw2 "sw2 send + recieve example" (node2,all) {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:ON:1]}
    Switch node2_sw2 "sw2 send + recieve example" (node2,all) {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:OFF:0]}
    Switch node2_sw2 "sw2 send + recieve example" (node2,all) {mqtt="<[mysensor:MyMQTT/21/2/V_LIGHT:command:MAP(1on0off.map)]"}

    Not that it matters for your problem but the first two I think can be replaced with:

    Switch node2_sw2 "sw2 send + recieve example" (node2,all) {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:*:default]}

    This is what I think happens: When you toggle the switch in Openhab to ON this is placed on the bus and sent as a MQTT message. The third definition binds exactly the same item to an inbound command so then Openhab reads it executes this command which is put on bus and the is read again by the incoming binding and so on. Effectively you have created a loop by adding the 3rd binding to the first two.

    One solution is to use two switches, one for incoming commands and one for outgoing, like this:

    Switch node2_sw2-1 "sw2 send example" (node2,all) {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:*:default]}
    Switch node2_sw2-2 "sw2 recieve example" (node2,all) {mqtt="<[mysensor:MyMQTT/21/2/V_LIGHT:command:default]}

    Then you use the first one to trigger outbound messages and the second one to read incoming. If you want to see the current state of the lamp (the first switch) in openhab you can use a rule to set that according to changes made by any incoming message.

    I do not know if this is the way but I think it will work.


  • Hardware Contributor

    Found the solution tanks to watou user in OpenHab Forum.

    Switch node2_sw2  "sw2 send + recieve example"  (node2,all)	  {mqtt=">[mysensor:MyMQTT/21/2/V_LIGHT:command:OFF:0],>[mysensor:MyMQTT/21/2/V_LIGHT:command:ON:1],<[mysensor:MyMQTT/21/2/V_LIGHT:state:MAP(1on0off.map)]"}
    

    The secret is in the :state: declaration on the MAP!

    Thank you all



  • @soloam Yes, the state makes the difference of course. Because you had used command for all definitions I assumed that your intention was to also send in a command from outside of Openhab to control the light.
    Glad you found a solution.


 

370
Online

8.2k
Users

9.0k
Topics

96.0k
Posts