# Analyze State of Multiple MQTT Devices

**URL:** https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027
**Category:** General
**Created:** [15 February 2021 14:15 UTC](https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027 "2021-02-15T14:15:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![TheJoeFletch](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@TheJoeFletch](https://discourse.nodered.org/u/TheJoeFletch)
#### Post date: [15 February 2021 14:15 UTC](https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027/1 "2021-02-15T14:15:10Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c9663de9c4bc1d316e0dae14495a66e1b6fca4b.png)

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...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80eb19a62a9bacb6b2ddf7abdb457c7a86b869ac.png)

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.

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

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

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [15 February 2021 14:30 UTC](https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027/2 "2021-02-15T14:30:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [15 February 2021 15:24 UTC](https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027/3 "2021-02-15T15:24:10Z")

</div>

> [@TheJoeFletch](#):
>
> 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.

Try the join node...

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [15 February 2021 15:25 UTC](https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027/4 "2021-02-15T15:25:43Z")

</div>

> [@TheJoeFletch](#):
>
> - If both are on, then turn off.
> - If both are off, then turn on.
> - Else turn off.

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

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) 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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [16 April 2021 15:26 UTC](https://discourse.nodered.org/t/analyze-state-of-multiple-mqtt-devices/41027/5 "2021-04-16T15:26:32Z")

</div>

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