3-way switch with openHab 2.4 and Sonoff devices (using Tasmota)

Follow profile can’t be used (been there seen that recently) because it will create a loop.
It can be solved by several approaches:

1. in OH

rule "Switch A"
when
    Item Switch_A changed
then
    if(Switch_B.state != Switch_A.state) Switch_B.sendCommand(Switch_A.state.toString)
    Switch_B.postUpdate(Switch_A.state.toString)
    //logInfo("Switch A", Switch_A.state.toString)

end

rule "Switch B"
when
    Item Switch_B changed
then
    if(Switch_B.state != Switch_A.state) Switch_A.sendCommand(Switch_B.state.toString)
    Switch_A.postUpdate(Switch_B.state.toString)
    //logInfo("Switch B", Switch_B.state.toString)
end

Simple rule, but you are reusing quite a bit of same code for every switch here and there

2. Node red (using OH node) and group topic in Tasmota

[{"id":"c4a5544a.56c7e8","type":"mqtt out","z":"591acf63.f864a","name":"qqq","topic":"testswitch/cmnd/POWER","qos":"","retain":"","broker":"83323ac5.ab4df8","x":310,"y":500,"wires":[]},{"id":"c821fbee.b17468","type":"openhab2-in","z":"591acf63.f864a","name":"A","controller":"1a82237b.8ca69d","itemname":"Switch_TA","x":120,"y":460,"wires":[[],["c4a5544a.56c7e8"]]},{"id":"676af932.7ef478","type":"openhab2-in","z":"591acf63.f864a","name":"B","controller":"1a82237b.8ca69d","itemname":"Switch_TB","x":120,"y":540,"wires":[[],["c4a5544a.56c7e8"]]},{"id":"83323ac5.ab4df8","type":"mqtt-broker","z":"","name":"MQTT","broker":"IP","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1a82237b.8ca69d","type":"openhab2-controller","z":"","name":"OH","protocol":"http","host":"localhost","port":"8080","path":"","username":"","password":""}]

This needs bit of love as it causes mqtt spam, so more check on states needed.

3. only Node red without OH/HA etc.

[{"id":"a7769c75.cda58","type":"mqtt in","z":"9d128c8a.994b5","name":"Switch 08","topic":"sonoff-touch-08/stat/POWER","qos":"0","datatype":"utf8","broker":"83323ac5.ab4df8","x":300,"y":4120,"wires":[["134b5b18.61d3e5","88b82873.07df98"]]},{"id":"3a4af89a.3d3d48","type":"mqtt in","z":"9d128c8a.994b5","name":"Switch 09","topic":"sonoff-touch-09/stat/POWER","qos":"0","datatype":"utf8","broker":"83323ac5.ab4df8","x":300,"y":4220,"wires":[["88b82873.07df98","134b5b18.61d3e5"]]},{"id":"a2bb607f.6bd8a","type":"change","z":"9d128c8a.994b5","name":"08","rules":[{"t":"set","p":"status08","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":4120,"wires":[[]]},{"id":"134b5b18.61d3e5","type":"switch","z":"9d128c8a.994b5","name":"08s","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"status08","vt":"flow"},{"t":"neq","v":"status08","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":4120,"wires":[["a2bb607f.6bd8a"],["a2bb607f.6bd8a","48747ed4.c73b2"]]},{"id":"88b82873.07df98","type":"switch","z":"9d128c8a.994b5","name":"09s","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"status09","vt":"flow"},{"t":"neq","v":"status09","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":4220,"wires":[["aacb9062.c6922"],["aacb9062.c6922","27d97c2.8192d84"]]},{"id":"aacb9062.c6922","type":"change","z":"9d128c8a.994b5","name":"09","rules":[{"t":"set","p":"status09","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":4220,"wires":[[]]},{"id":"48747ed4.c73b2","type":"mqtt out","z":"9d128c8a.994b5","name":"to the 08","topic":"sonoff-touch-08/cmnd/POWER","qos":"","retain":"","broker":"83323ac5.ab4df8","x":700,"y":4160,"wires":[]},{"id":"27d97c2.8192d84","type":"mqtt out","z":"9d128c8a.994b5","name":"to the 09","topic":"sonoff-touch-09/cmnd/POWER","qos":"","retain":"","broker":"83323ac5.ab4df8","x":700,"y":4260,"wires":[]},{"id":"83323ac5.ab4df8","type":"mqtt-broker","z":"","name":"MQTT","broker":"IP","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

This is my prefered solution for now, as it is completely independend on OH or whatever automatization system you are using => so your switches will be working even when OH is taking a break, restarting etc.
And because it’s using purely mqtt it’s quite reliable in terms of responses.

  • your tasmota group topic can be used for updates as it’s not used in here
1 Like