Create an efficient flow with multiple sensors

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:

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.

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.

[{"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"]]}]

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

image

Link nodes!

This might be usefull then!

[{"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":[[]]}]

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

I'm using aqara door sensors

link nodes and globals, thanks this is a good idea

1 Like

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