Openhab - MQTT - Hardware Button // ITEM problem

Hello,
I’m using a ESP-12E microcontroller (forget about the details) that simply triggers my Mosquitto Broker and sets 1 or 0 to a specific topic…toggles perfectly…
My problem now is that I have really really hard times to find a proper items setup that works the same toggle way…I managed to turn the openhab switch ON in the GUI via hardware button, but off does nto work at all…

I did search now 2 days and found so many different solutions (with and without state feedback, extra mapping aso…but nothing works…

here is my item right now that at least makes the button switch ON…
Switch ESPswitch2 “ESP Switch2” {mqtt="<[mosquitto:/MyESP/MySwitch2/Switch2:command:ON]"}

If I do the next logical step: adding <[mosquitto:/MyESP/MySwitch2/Switch2:command:OFF]" nothing works…anymore…I tried :on:1, OFF:0…and so on…no success.

some suggest this: Switch mqttsw1 “Switch 1” (all) {mqtt=">[mymosquitto:/arduino/l1/com:command:off:0],>[mymosquitto:/arduino/l1/com:command:on:1],<[mymosquitto:/arduino/l1/state:state:default]"}

so really confused and frustrated…any help is highly appreciated. Norbert

To send a “1” when you move the switch to ON and “0” when you move the switch to OFF:

Switch myItem { mqtt=">[mosquitto:/MyESP/MySwitch2/Switch2:command:ON:1],>[mosquitto:/MyESP/MySwitch2/Switch2:command:OFF:0]" }

to expand further, so that myItem also shows the state:

Switch myItem { mqtt="<[mosquitto:/MyESP/MySwitch2/Switch2:state:MAP(onoff.map)],>[mosquitto:/MyESP/MySwitch2/Switch2:command:ON:1],>[mosquitto:/MyESP/MySwitch2/Switch2:command:OFF:0]" }

transform/onoff.map:

1=ON
0=OFF
2 Likes

watou, thanks a lot for the fast reply…

What you posted is exactly what I have seen in several instructions on how to have a “GUI button/switch” and send the information towards MQTT broker that forwards towards hardware making an LED/whatever turn ON and OFF.

Here its different…I have a hardware button and want to remotely turn ON/OFF the virtual button/switch in Openhab…so my hardware button is the publisher…and openhab is the subscriber…

not sure/a little confused how in the end the two ends can be kept in sync.

If you only want the openHAB switch to show the current state of the hardware switch, then you ought to just use the first part of my example:

<[mosquitto:/MyESP/MySwitch2/Switch2:state:MAP(onoff.map)]

If your hardware switch publishes “1” to the topic /MyESP/MySwitch2/Switch2, then the above ought to change the state of the switch you see in openHAB.

< means subscribe (> means publish). You can subscribe and treat what your hardware switch publishes as either updating state or sending commands. Since you are only wanting to have openHAB’s switch reflect what state the hardware switch is in, updating state is what you want. Lastly, the transform with the map file will interpret an incoming “0” as “OFF”, which is then used to update the switch’s state to OnOffType.OFF.

sorry, but does not work - i tried similar things before but even with big hope in your code still no reaction…

Summary:
Switch ESPswitch “ESP Switch” {mqtt="<[mosquitto:/MyESP/MySwitch/Switch:state:MAP(mqttswitch.map]"}
plus a map file including 1=ON & 0=OFF does not react in any way…

If I do the following (see Switch2 below)…it turns from OFF to ON if it was OFF…if I do turn OFF in GUI it works again to turn from OFF to ON…but not back to OFF…which I guess is obvious as it only covers the ON type…

Switch ESPswitch2 “ESP Switch2” {mqtt="<[mosquitto:/MyESP/MySwitch2/Switch2:command:ON]"}
(I have two switches on the same board both publishing just fine to Mosquitto)…

Thanks for any further help you can provide!!!

wohoooo…i finally managed to forward the status of the physical switch to openhab…the problem was rather stupid…after another hour of digging i found somebody with a similar problem…

Arduino HW Switches (ESP easy) provides not 0/1 but 1.00 and 0.00 as a value…so either in the map or directly in the item you have to put the right float values…

last question: is there by any chance a way to go both directions…right now I can switch on/off via hw button…but if I press ON in the GUI another press on the HW button would trigger its decoupled state machien and force another 1.00…so it would do nothing…only another press on the HW button would turn things off…i know that you mentioned something about pushing states from GUI back…but to me its unclear where this would go…of course the MQTT broker would have the updated status…but if my decoupled statemachine from Arduino does not use this information to does not really help…i guess.

Glad it’s partly working! To address sending commands via MQTT, you might want to have your device subscribe to a topic for commands that is separate and different from the topic on which it publishes the current state, so your single Switch item will subscribe to the state topic and publish commands to the command topic. Using two separate topics, one for each direction, ought to help.

Watou, can you maybe give me another helping tip. As mentioned now the buttons work fine…and react almost normally with a little higher but acceptable delays in the WebGUI…so the time it takes until the status update is correctly represented there…

But in Android App and Iphone App (both the same way)…no update at all…only if i reload the sitemap from scratch it will then show the correct status…is this normal? or do I have to add anything? I remember that there is some option like "autoupdate=false…did not use this autoupdate at all right now. THANKS Norbert

Hi @norbert_jordan, how did you solve this problem? I have almost the same setup and the same problem and am still struggeling with it …

Thx in advance

Disregard, found a simple solution:

Proxy item:
Switch Alarm_ON_OFF_Proxy "Alarm ON/OFF Proxy"
GUI Switch:
Switch Alarm_ON_OFF_GUI "Alarm ON/OFF"
ESPEasy MQTT Switch:
Switch Alarm_Switch_WLAN_IN {mqtt="<[mosquitto:/esp8266ledbutton/alarm/switch:command:ON:1.00],<[mosquitto:/esp8266ledbutton/alarm/switch:command:OFF:0.00]"}

rule "Toggle Alarm with Wifi"
when   
        Item Alarm_Switch_WLAN_IN changed
        
then
        if (Alarm_ON_OFF_Proxy.state==ON) {
        Alarm_ON_OFF_Proxy.sendCommand(OFF)
        postUpdate(Alarm_ON_OFF_GUI, OFF)
        }
        else
        if (Alarm_ON_OFF_Proxy.state==OFF) {
        Alarm_ON_OFF_Proxy.sendCommand(ON)
        postUpdate(Alarm_ON_OFF_GUI, ON)
        }
end

rule "Switch Alarm ON"
when   
        Item Alarm_ON_OFF_GUI changed from OFF to ON
        
then
        if (Alarm_ON_OFF_Proxy.state==OFF)
        Alarm_ON_OFF_Proxy.sendCommand(ON)
end

rule "Switch Alarm OFF over GUI"
when   
        Item Alarm_ON_OFF_GUI changed from ON to OFF
        
then
        if (Alarm_ON_OFF_Proxy.state==ON) {
        Alarm_ON_OFF_Proxy.sendCommand(OFF)
        }
end

hi…
sorry for the delay…
the thing is that the ESP does provide the status via MQTT towards openhab…but there is no real back channel that updates ESP via mqtt if somebody does press the virtual button in the gui…as discussed with Watou you would need to send some message back via MQTT in order to update the ESP…and of course you would need a code in the ESP for this to update the ESP-button…too complicated for me as its a rare scenario, so i can live with this that ESP will not get an update if few times the GUI button is used…

No problem, sometimes there are more important things than home automation …
But as you can read in my todays post I found a simple solution, you just have to adapt that to your config …

Hi nobert_jordan
I am trying to achieve same config what you have. I currently have ESP8266 with EasyESP and I can turn ON/OFF led with following config

Switch Bed_SW1 "Candle Warmer" <candle> (BR_Master) {mqtt=">[mqttserver:bedroom/gpio/14:command:on:1],>[mqttserver:bedroom/gpio/14:command:off:0]"}

No I have physical (Manual Wall) switch setup as input in EasyESP. I am trying to control led with either from openHAB app or manual switch and want the state to be uploaded to openHAB but i am unsuccessful so far. I will really appreciate somebody’s help as this thing is driving me crazy.

For you reference EasyESP switch is setup as pulldown and send 1.00 for ON and 0.00 for OFF.

As I already posted above, you need a proxy item and a rule. In your case the item Bed_SW1 is my item Alarm_Switch_WLAN_IN, your openhab software switch is my switch Alarm_ON_OFF_GUI, so you have to create the proxy (in my case Alarm_ON_OFF_Proxy) and you have to adapt my rule to your switch names.

… found a simple solution:

Proxy item:

Switch Alarm_ON_OFF_Proxy "Alarm ON/OFF Proxy"

GUI Switch:

Switch Alarm_ON_OFF_GUI "Alarm ON/OFF"

ESPEasy MQTT Switch:

Switch Alarm_Switch_WLAN_IN {mqtt="<[mosquitto:/esp8266ledbutton/alarm/switch:command:ON:1.00],<[mosquitto:/esp8266ledbutton/alarm/switch:command:OFF:0.00]"}

rule "Toggle Alarm with ESP8266"
when   
        Item Alarm_Switch_WLAN_IN changed
        
then
        if (Alarm_ON_OFF_Proxy.state==ON) {
        Alarm_ON_OFF_Proxy.sendCommand(OFF)
        postUpdate(Alarm_ON_OFF_GUI, OFF)
        }
        else
        if (Alarm_ON_OFF_Proxy.state==OFF) {
        Alarm_ON_OFF_Proxy.sendCommand(ON)
        postUpdate(Alarm_ON_OFF_GUI, ON)
        }
end

rule "Switch Alarm ON"
when   
        Item Alarm_ON_OFF_GUI changed from OFF to ON
        
then
        if (Alarm_ON_OFF_Proxy.state==OFF)
        Alarm_ON_OFF_Proxy.sendCommand(ON)
end

Edit: Upps, one part in the rule was missing:

rule "Switch Alarm OFF over GUI"
when   
        Item Alarm_ON_OFF_GUI changed from ON to OFF
        
then
        if (Alarm_ON_OFF_Proxy.state==ON) {
        Alarm_ON_OFF_Proxy.sendCommand(OFF)
        }
end

Thank you for your reply. I will give it a try and will let you know
One more question
Where do I put proxy items with other items in same file or do I have to create somewhere else??
Thank you again

You may use the same file, but it is also possible to use a separate file.
If you don’t have to many items I recommend using the same file …

Good luck.

Why are proxy items needed here? Why can’t a single Switch item with MQTT publish and subscribe sections be used?

@sihui

You may use the same file, but it is also possible to use a separate file.
If you don’t have to many items I recommend using the same file …
Good luck

Thank you

@watou

Why are proxy items needed here? Why can’t a single Switch item with MQTT publish and subscribe sections be used?

I want to achieve the turning OFF/ON of LED from APP as well as Manual Toggle Switch with status synchronized. If you have nay other solution then I will really appreciate your help. Even though I did understand @sihui solution but wanted to achieve simple way possible as I am complexly noob as far as the coding goes.
Thank you,
Andy

  • What is the topic on which the ESP device publishes state changes? What messages does it publish to represent those states?
  • What is the topic on which openHAB should publish commands to make the ESP device turn on and off? What messages should openHAB publish to that topic to represent the ON and OFF commands?

With answers to the above questions, you ought to be able to use a single openHAB Switch item without needing rules or proxy items.

Here is the simple setup i have which is currently working to turn ON/OFF from App only
Switch Bed_SW1 "Candle Warmer" <candle> (BR_Master) {mqtt=">[mqttserver:bedroom/gpio/14:command:on:1],>[mqttserver:bedroom/gpio/14:command:off:0]"}

Now say i want to add a toggle (Wall) switch which sends 0 for OFF and 1 for ON and is connected to same ESP module on GPIO12.
So how can I have both synchronized together means I should be able to turn on/off from APP as well as by flipping the wall switch.
thank you again