# Create an efficient flow with multiple sensors

**URL:** <https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518>\
**Category:** General\
**Created:** [17 February 2022 08:50 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518 "2022-02-17T08:50:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nawt888](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@nawt888](https://discourse.nodered.org/u/nawt888)\
**Post date:** [17 February 2022 08:50 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/1 "2022-02-17T08:50:23Z")

</div>

Hello,

I currently have sensors on the 3 doors to my home to understand which doors are open/closed, when those doors are opened/closed and whether the entire house is closed or not.

I have a further 20 sensors I have purchased to install on the windows, so that I can see the same open/closed state information for the windows.

My current flow requires me to check the state of the other doors when an event fires, in order to determine whether the entire house is closed or not, and adding an additional 20 sensors will create a messy, complex flow.

For example, if the front door has been closed, I need to check the state of the other 2 doors, in order to determine whether the house is current closed or not:

 ![2022-02-17 19_47_18-Node-RED – Home Assistant](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e76abf6589a175bb7a7a9bafa27f7fb467b58026.png)

I'm very new to nodered - is there a way to optimise this flow that makes it scale more cleanly when I add 20 more similar sensors?

Is there a way for example, to iterate over every sensor when the value of any other sensor changes?

Appreciate any advice or ideas to make this flow more generic/scaleable.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 February 2022 10:28 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/2 "2022-02-17T10:28:18Z")

</div>

This is a flow I wrote to help another user checking doors. It doesn't use HA but may give you ideas of how to achieve what you want to do.

```auto
[{"id":"b946b6d6f1384bbd","type":"inject","z":"397c2f459ec16f88","name":"Front door unlocked","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Front door","payload":"unlocked","payloadType":"str","x":150,"y":200,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"5bdafdb6d195bc3f","type":"inject","z":"397c2f459ec16f88","name":"Front door locked","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Front door","payload":"locked","payloadType":"str","x":140,"y":240,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"093316f2d930fb91","type":"inject","z":"397c2f459ec16f88","name":"gym back door left open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"gym back door left","payload":"open","payloadType":"str","x":170,"y":300,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"0794bc9e30ea9991","type":"inject","z":"397c2f459ec16f88","name":"gym back door left closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"gym back door left","payload":"closed","payloadType":"str","x":170,"y":340,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"a8ae0244601876fb","type":"inject","z":"397c2f459ec16f88","name":"gym back door right open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"gym back door right","payload":"open","payloadType":"str","x":170,"y":400,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"e7886559a21e6046","type":"inject","z":"397c2f459ec16f88","name":"gym back door right closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"gym back door right","payload":"closed","payloadType":"str","x":170,"y":440,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"801517e98c9ba6fa","type":"inject","z":"397c2f459ec16f88","name":" jarvis door unlocked","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":" jarvis door","payload":"unlocked","payloadType":"str","x":150,"y":500,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"153ce5f9350b6398","type":"inject","z":"397c2f459ec16f88","name":" jarvis door locked","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":" jarvis door","payload":"locked","payloadType":"str","x":140,"y":540,"wires":[["ebe70eb3bfe15eeb"]]},{"id":"feb31e1bca38aa94","type":"inject","z":"397c2f459ec16f88","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"doors","payloadType":"global","x":170,"y":120,"wires":[["b554871717464e74"]]},{"id":"ebe70eb3bfe15eeb","type":"link call","z":"397c2f459ec16f88","name":"Set status in global","links":["d40b527dcbde48df"],"timeout":"30","x":530,"y":340,"wires":[["a8f6c839a13a0c28"]]},{"id":"d40b527dcbde48df","type":"link in","z":"397c2f459ec16f88","name":"Set Status","links":[],"x":380,"y":480,"wires":[["252d653f926ccff9"]],"l":true},{"id":"ca2a8928d1fe6ca8","type":"link out","z":"397c2f459ec16f88","name":"return","mode":"return","links":[],"x":830,"y":480,"wires":[],"l":true},{"id":"252d653f926ccff9","type":"function","z":"397c2f459ec16f88","name":"","func":"// get the global 'doors' object or itinilize it if it doesn't exist\nlet doors = global.get('doors') || {}\n\n// get the door name from msg.topic\nlet door_name = msg.topic\n\n// get this doors object of initilize it\n//let this_door = doors.door_name ||{'name': msg.topic, 'status': msg.payload}\nlet this_door = doors.door_name ||{'status': msg.payload}\n\n// check the door status and set it in teh objct\nif ( (msg.payload == 'open') || (msg.payload == 'unlocked') ) {\n this_door.state = 0\n this_door.status = msg.payload\n} else if ( (msg.payload == 'closed') || (msg.payload == 'locked') ) {\n this_door.state = 1 \n this_door.status = msg.payload\n} else {\n // an unknown status has arrived - send a warning\n node.warn('unknown status for '+ msg.topic)\n return null\n}\n\n// update the 'doors' variable\ndoors[door_name] = this_door\n// update the 'doors' global\nglobal.set('doors', doors)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":480,"wires":[["ca2a8928d1fe6ca8"]]},{"id":"a8f6c839a13a0c28","type":"debug","z":"397c2f459ec16f88","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":340,"wires":[]},{"id":"3c5671db2c18c284","type":"debug","z":"397c2f459ec16f88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":120,"wires":[]},{"id":"ddd7971942421d43","type":"comment","z":"397c2f459ec16f88","name":"This flow runs at a set interval and checks to sse that all doors are locked/closed ","info":"","x":430,"y":60,"wires":[]},{"id":"ab37f81aefc3e35f","type":"comment","z":"397c2f459ec16f88","name":"'Set Status' This flow sets the global with the status of each door ","info":"","x":660,"y":420,"wires":[]},{"id":"51b224860c6ee555","type":"comment","z":"397c2f459ec16f88","name":"This flow takes the input status of the doors and calls the 'Set Status' flow","info":"","x":710,"y":260,"wires":[]},{"id":"b554871717464e74","type":"function","z":"397c2f459ec16f88","name":"","func":"function delay(time) {\n return new Promise(resolve => setTimeout(resolve, time));\n}\n\n\nconst doors = msg.payload\nmsg.payload = \"system secure\"\n\nfor (let key in doors) {\n// node.warn(\"processing\" key);\n// node.warn(\"door: \" + key + \" state=\" + doors[key][\"state\"]);\n if (doors[key][\"state\"] == 0) {\n msg.payload = \"security breach: \"+ key;\n node.send(msg);\n }\n}\n\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":120,"wires":[["3c5671db2c18c284"]]}]

```

---

<div class="post-metadata">

**Author:** ![Surefire01](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/surefire01/32/71490_2.png) [@Surefire01](https://discourse.nodered.org/u/Surefire01)\
**Post date:** [17 February 2022 10:36 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/3 "2022-02-17T10:36:05Z")

</div>

Not a solution but more of inquiry. What kind of sensors are you using? as I would be interested in doing something like this.

---

<div class="post-metadata">

**Author:** ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)\
**Post date:** [18 February 2022 15:31 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/4 "2022-02-18T15:31:29Z")

</div>

> [@nawt888](#):
>

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

Link nodes!

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4eedf89204074d569f5255896e387311586e8d00.jpeg "Node-Red Link Nodes Overview") ](https://www.youtube.com/watch?v=0SU_Tlf5l14)

---

<div class="post-metadata">

**Author:** ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)\
**Post date:** [18 February 2022 15:37 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/5 "2022-02-18T15:37:09Z")

</div>

> [@nawt888](#):
>
> I'm very new to nodered

This might be usefull then!

```auto
[{"id":"544a325ff2a412cb","type":"display property","z":"3ab61f50.f9635","name":"Display msg.topic","property":"msg.topic","showDate":0,"showTime":0,"x":830,"y":2180,"wires":[["645bc627141d09bd"]]},{"id":"e365ba111b38c120","type":"display property","z":"3ab61f50.f9635","name":"Display msg.payload","property":"Display msg.payload","showDate":0,"showTime":0,"x":840,"y":2260,"wires":[["645bc627141d09bd"]]},{"id":"3126bd0e3529dc53","type":"display property","z":"3ab61f50.f9635","name":"Display msg.object","property":"Display msg.object","showDate":0,"showTime":0,"x":830,"y":2340,"wires":[["645bc627141d09bd"]]},{"id":"33ba34918f0d1376","type":"inject","z":"3ab61f50.f9635","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":2260,"wires":[["544a325ff2a412cb","e365ba111b38c120","3126bd0e3529dc53"]]},{"id":"645bc627141d09bd","type":"switch","z":"3ab61f50.f9635","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1150,"y":2260,"wires":[[]]}]

```

> **[node-red-contrib-display-property](https://flows.nodered.org/node/node-red-contrib-display-property)**
>
> A simple node that displays as its status the value of a message property that passes the node and optional the date/time when the message passed.

It helped me a lot with understanding the flows. You can stick the nodes between everything as it does not change the message.

---

<div class="post-metadata">

**Author:** ![nawt888](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@nawt888](https://discourse.nodered.org/u/nawt888)\
**Post date:** [21 February 2022 08:41 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/6 "2022-02-21T08:41:12Z")

</div>

I'm using aqara door sensors

---

<div class="post-metadata">

**Author:** ![nawt888](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@nawt888](https://discourse.nodered.org/u/nawt888)\
**Post date:** [21 February 2022 08:49 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/7 "2022-02-21T08:49:51Z")

</div>

link nodes and globals, thanks this is a good idea

---

<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:** [22 April 2022 08:50 UTC](https://discourse.nodered.org/t/create-an-efficient-flow-with-multiple-sensors/58518/8 "2022-04-22T08:50:15Z")

</div>

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