Using MQTT command for Switch Item

Hey there, yet another MQTT-thread. Since I spent a few days without success, I decided to ask for help.

My basic idea: I have a Sonoff-Basic Switch with Tasmota-Firmware. I want to use it for a ceiling lamp. I want to use another GPIO of the same switch to control another Sonoff (S20) in the corner of the room. I set the switchtopic (sonoff-basic-01-switch) to “sonoff-basic-01-switch”.

Since the Tasmota-Firmware does not support customized MQTT-Messages, I want to create an Openhab Rule (using recent Openhab2 Installation with working MQTT-Binding) to switch the both lights. I cannot achieve two important things:

  1. Receiving an Update or anything to trigger the rule in the .rules file. I set up a Switch like
    Switch	Sonoff_Basic_01_Switch	["Lighting"] {mqtt="<[mosquitto:cmnd/sonoff-basic-01-switch/POWER1:command:TOGGLE:1],>[mosquitto:cmnd/sonoff-basic-01-switch/POWER1:command:TOGGLE:1],<[mosquitto:cmnd/sonoff-basic-01-switch/POWER1:state:default]"}

In the rule I try to react on something the Switch does by

	Item Sonoff_Basic_01_Switch received update

When I switch the (phyisical) switch, the command {cmnd/sonoff-basic-01-switch/POWER1:TOGGLE} is being broadcasted by my sonoff. But in my openhab installation I cannot react on that.

2nd problem:

the “publish” method seems to be undefined. I cannot find anything helpful about that, because I have a reicend version of the mqtt binding installed.

Thank you very much for your help! Maybe some people have a similar problem.

  1. You need to define an actual transformation. You can’t just put a “message:value” in the MQTT definition (i.e. the TOGGLE:1 needs to be replaced with a Transformation, see If your message is simple then you should be able to get away with using a MAP transformation.

Use ON and OFF instead of 1 and 0 as the values that the message get transformed to.

The outbound definition is different from the inbound definition. Please see

  1. You need to install the MQTT Action. And once installed be aware that Designer will still not recognize the publish action and mark it as unknown. Also, there was a time that having the Action installed caused the binding to stop working and I don’t know if that has been fixed.

OK, thanks a lot!!! Actually I have but one command sent by the switch: cmnd/sonoff-basic-01-switch/POWER1:TOGGLE

So there is neither 0/1 nor ON/OFF. I have the other switch (the Sonoff S20) sucessfully running as Switch Item. And I would like just to react on the mqtt command from above.

Is it overall the right approach to define another item (Item Sonoff_Basic_01_Switch) in order to react on it in a rule? Or is there any other way to trigger the event (resp. command) “TOGGLE” by the remote switch?

1 Like

OK, given that there is only one message sent I would do the following.

  • Create a String Item that receives the message
String Sonoff_Basic_01_String {mqtt="<[mosquitto:cmd/sonoff-basic-01-switch/POWER1:state:default]"}
  • Create a Switch Proxy Item to represent the state of the physical device (see below)

  • Create a rule that triggers when the String Item receives an update. This rule will update the Switch to the proper state (presumably toggle it’s state?)

rule "Update Switch Proxy Item"
    Item Sonoff_Basic_01_String received update
    if(Sonoff_Basic_01_Switch.state == ON) Sonoff_Basic_01_Switch.postUpdate(OFF)
    else Sonoff_Basic_01_Switch.postUpdate(ON)
  • Add the outbound MQTT configuration to the Switch Item
Switch	Sonoff_Basic_01_Switch	["Lighting"] {mqtt=">[mosquitto:cmnd/sonoff-basic-01-switch/POWER1:command:*:TOGGLE]"}

Notice the modifications to the MQTT binding config in both cases.

I prefer this approach as it is a little easier to avoid infinite loops (i.e. the Switch publishes TOGGLE which causes the Switch Item to change which causes the MQTT binding to publish TOGGLE which causes …). Though you are not completely out of the woods here. Because the same topic is being used for communication both directions you may still be getting into an infinite loop because the client has no way to distinguish between messages it sent itself verses those sent by the device.

Anyway, the key in avoiding the loop is the use of postUpdate which only modifies the state of the Item in OH and does not trigger the binding to send the message to the device.


(Sorry, I changed this commend now to the final version)

I got it running, thanks for your help!

My String Items are defined as follows now

String Sonoff_Basic_01_POWER1_String {mqtt="<[mosquitto:cmnd/sonoff-basic-01-switch/POWER1:command:TOGGLE]"}
String Sonoff_Basic_01_POWER2_String {mqtt="<[mosquitto:cmnd/sonoff-basic-01-switch/POWER2:command:TOGGLE]"}

So the rules can trigger now the command TOGGLE. My switches

Switch Wohnzimmer_Licht_Sofa "Wohnzimmerlicht Eins"	<light>	["Lighting"] {mqtt=">[mosquitto:cmnd/sonoff-basic-01/power:command:*:default],<[mosquitto:stat/sonoff-basic-01/POWER:state:default]"}
Switch Wohnzimmer_Leselampe	"Leselampe"	<light>	["Lighting"] {mqtt=">[mosquitto:cmnd/sonoff-S20-01/power:command:*:default],<[mosquitto:stat/sonoff-S20-01/POWER:state:default]"}

are being switched now by the following rules

rule "sonoff-basic-01-switch POWER1"
	Item Sonoff_Basic_01_POWER1_String received command TOGGLE
    if(Wohnzimmer_Licht_Sofa.state == ON) sendCommand(Wohnzimmer_Licht_Sofa, OFF)
    else sendCommand(Wohnzimmer_Licht_Sofa, ON)

rule "sonoff-basic-01-switch POWER2"
    Item Sonoff_Basic_01_POWER2_String received command TOGGLE
    if(Wohnzimmer_Leselampe.state == ON) sendCommand(Wohnzimmer_Leselampe, OFF)
    else sendCommand(Wohnzimmer_Leselampe, ON)

What still did not work, is sending the mqtt command directly by the “publish” method. Do you have an idea what is wrong with my binding?

You have to have the MQTT Action installed. The publish method is completely independent of Items.

There was a time that having both the binding and action installed at the same time broke the binding. I hope that has been fixed.

1 Like


i hope that you had fixed your issue, i will be grateful if you could share some details