Analyze State of Multiple MQTT Devices

I have Node-RED installed on my Home Assistant instance and I am trying to do the following. (Note that this is my first attempt at a bit of logic here, so my knowledge of all the nodes is certainly not complete.)

When a Tasmota device is long pressed (and produces a specific MQTT message), I would like to check the state of 2 switches, then...

  • If both are on, then turn off.
  • If both are off, then turn on.
  • Else turn off.

This is what I have come up with, and it works so far, but if I have to expand this, it is going to be a nightmare.

I have tried the OR / AND node to try and get the MQTT status by pasting in the cmnd/switch_name/POWER command, but not too sure if that is the intent of these nodes.

I have also tried this, with the current state node...

But it ends up sending 2 messages at the end; one false and one true. I suspect since there is a lag in getting the MQTT states back and produces 2 output flows.

msg : Object
object
topic: null
payload: "off"
bool: false
_msgid: "688f91a1.5fe65"

msg : Object
object
topic: null
payload: "off"
bool: true
_msgid: "57953389.fabcdc"

Node-RED operates asynchronously. That means that your incoming messages will never happen together and so you cannot do simple AND/OR logic without storing values somewhere and retrieving them on each msg so that you can do the comparison.

Lots of ways to do that. Possibly the simplest is a function node that receives both msgs. Initially it should retrieve the previous states, update the appropriate state from the incoming msg, send out the required output based on the comparison then store the updated states.

Try the join node...

See this article in the cookbook for an example of how to join messages into one object.

You don't need the first test, just if both are off then turn on, else turn off.

See this article in the cookbook for an example of how to join messages into one object. If you join the two states and the mqtt message into one message as described in that article then you will have them all available in one message so can apply the logic to decide what to do.