Help configuring 2 way light switching in Node Red

Hi, I'm new to Node Red and I'm stuck with what seems the simplest of problems. I've changed the light switches in my house to Tasmota flashed smart switches. I've followed Peter Scargill's advice and avoided "rules" within the tasmota flashed devices relying on Node Red to handle the logic. I'm not using Home Assistant and just have a Raspberry Pi running NR and a MQTT broker. All communication is done by MQTT.
I've installed a double gang light switch in the hall that is hard wired to the house electrics. This switch also operates the landing light. Electrically this all works fine. Upstairs I've fitted a single light switch (a dummy that's not connected to the switching circuit) that I'd hoped to link with the hall switch to provide 2 way switching. I thought it would be straightforward in that I'd make the dummy subscribe to the "state" of the master and vise versa. But, if I do that, all that happens is the switches operate rapidly in a loop making the light flash like a strobe. Obviously this isn't the way to do it.

Here is the flow when I just subscribe the landing (dummy) to the hall switch. This works in that the landing switch follows the state of the hall switch.

[{"id":"e2bbc2fc.09d1","type":"mqtt in","z":"c4180017.527bc","name":"Hall (2 gang)","topic":"stat/TWS-43/POWER1","qos":"2","datatype":"auto","broker":"7668ef17.d8043","nl":false,"rap":true,"rh":0,"x":150,"y":2680,"wires":[["14b93dc9.c20892"]]},{"id":"14b93dc9.c20892","type":"mqtt out","z":"c4180017.527bc","name":"Landing (1 gang)","topic":"cmnd/TWS-17/POWER1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"7668ef17.d8043","x":570,"y":2680,"wires":[]},{"id":"7668ef17.d8043","type":"mqtt-broker","name":"","broker":"","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

But when I subscribe the hall switch to the state of the landing switch it causes an infinite loop.
I'm told I need to use a Function node to add some logic but at this stage of my knowledge it's beyond me.
I'd really appreciate if someone could point me in the right direction.

You have only posted 2 MQTT nodes that link stat/TWS-43/POWER1 to cmnd/TWS-17/POWER1

Difficult to tell exactly what is happening with so little info.

What is TWS-43 and what is TWS-17?

I am guessing there is more logic in the flows related to these switches and light?

Hi, thanks for the reply.
TWS-43 is the name of the hall light switch (as named in the Tasmota set up of the device.
TWS-17 is the landing (dummy) switch.
I've not shown the second flow because I had to delete it to stop the infinite loop. It's just the same as above but with the SUB and PUB reversed.
Does that make sense?

Yes, and the reason for your loop.

Try putting a filter node between the pub+sub this will prevent any PUB if the value has not changed.

PS the filter node (formerly known as the RBE or Report By Exception node) basically inhibits message flow unless the value has changed.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

I'm really grateful for your advice. I'll try out the FILTER node later on. Would you mind if I message you if I'm unable to make it work?
I'll certainly look at the video you've recommended
Many thanks

That works !
Thank you so much for the advice.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.