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:
Receiving an Update or anything to trigger the rule in the .rules file. I set up a Switch like
In the rule I try to react on something the Switch does by
when
Item Sonoff_Basic_01_Switch received update
then
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.
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?
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"
when
Item Sonoff_Basic_01_String received update
then
if(Sonoff_Basic_01_Switch.state == ON) Sonoff_Basic_01_Switch.postUpdate(OFF)
else Sonoff_Basic_01_Switch.postUpdate(ON)
end
Add the outbound MQTT configuration to the Switch Item
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.
rule "sonoff-basic-01-switch POWER1"
when
Item Sonoff_Basic_01_POWER1_String received command TOGGLE
then
if(Wohnzimmer_Licht_Sofa.state == ON) sendCommand(Wohnzimmer_Licht_Sofa, OFF)
else sendCommand(Wohnzimmer_Licht_Sofa, ON)
end
rule "sonoff-basic-01-switch POWER2"
when
Item Sonoff_Basic_01_POWER2_String received command TOGGLE
then
if(Wohnzimmer_Leselampe.state == ON) sendCommand(Wohnzimmer_Leselampe, OFF)
else sendCommand(Wohnzimmer_Leselampe, ON)
end
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?