Help with setting up IF statements based on multiple change node outputs

I have a flow (triggered from a link in ) that locks my front door if the door is closed, and checks to see if the doors to the house are closed. Depending on the outcomes of those, I would receive a message (via telegram and not in this flow yet) with the status. Any doors that are open should be input into a string that can later be used to build the final message.

Everything works up until trying to format the final msg.payload output. I’m 100% sure that I’ve messed up the code in the function node - and that there’s probably a better way to achieve this.

Overview of possible final messages: If door locked and all doors closed = Home Secured If door unlocked and all doors closed = Home Unlocked If door unlocked and all doors closed = Home locked, but [insert door list from earlier node] If door unlocked and all doors open = Home unlocked and [insert door list from earlier node]

Here’s the current code: https://pastebin.com/9AeFx9XR

Any help with getting this to work is GREATLY appreciated, even if it needs to be rebuilt. I’m new to node-red so I’m still learning!

Not tested but might give you some ideas.

[{"id":"de366a74.117718","type":"ha-get-entities","z":"d172fd28.b50b3","server":"","name":"contacts","rules":[{"property":"entity_id","logic":"includes","value":"binary_sensor.door_back_contact, binary_sensor.door_front_contact, binary_sensor.door_garage_contact, binary_sensor.door_mailbox_contact","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"contacts","output_results_count":1,"x":284,"y":512,"wires":[["5cd957b0.532e28"]]},{"id":"5cd957b0.532e28","type":"api-current-state","z":"d172fd28.b50b3","name":"door status","server":"","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.door_front_contact","state_type":"str","state_location":"","override_payload":"none","entity_location":"","override_data":"none","blockInputOverrides":false,"x":454,"y":512,"wires":[["9b92059.ee365f8"],["17fa7cb7.854d23"]]},{"id":"9b92059.ee365f8","type":"api-call-service","z":"d172fd28.b50b3","name":"Lock Door","server":"","version":1,"debugenabled":false,"service_domain":"lock","service":"lock","entityId":"lock.front_door","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":630,"y":464,"wires":[["a10d44e4.867d38"]]},{"id":"17fa7cb7.854d23","type":"function","z":"d172fd28.b50b3","name":"format","func":"let status;\nconst openDoors = msg.contacts.map(door => door.attributes.friend_name).join(\" ,\");\n\nif(msg.frontDoor.attributes.lock_state === \"locked\") {\n    if(openDoors) {\n        status = `Home locked, but the ${openDoors} are open.`;\n    } else {\n        status = \"Home Secured\";    \n    }\n} else {\n    status = `Home unlocked`;\n    if(openDoors) {\n        status = `${status} and the ${openDoors} are open`;\n    }\n}\n\nmsg.payload = status;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1010,"y":512,"wires":[["92b94630.92e118"]]},{"id":"63ff7e9.bf39d8","type":"inject","z":"d172fd28.b50b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":138,"y":512,"wires":[["de366a74.117718"]]},{"id":"a10d44e4.867d38","type":"ha-wait-until","z":"d172fd28.b50b3","name":"Wait for door to lock","server":"","outputs":2,"entityId":"lock.front_door","entityIdFilterType":"exact","property":"attributes.lock_state","comparator":"is_not","value":"unlocked","valueType":"str","timeout":"30","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"frontDoor","entityLocationType":"msg","checkCurrentState":true,"blockInputOverrides":true,"x":820,"y":464,"wires":[["17fa7cb7.854d23"],["17fa7cb7.854d23"]]},{"id":"92b94630.92e118","type":"debug","z":"d172fd28.b50b3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1158,"y":512,"wires":[]}]

without a function node

[{"id":"16cf4c8c.6f8133","type":"ha-get-entities","z":"d172fd28.b50b3","server":"","name":"contacts","rules":[{"property":"entity_id","logic":"includes","value":"binary_sensor.door_back_contact, binary_sensor.door_front_contact, binary_sensor.door_garage_contact, binary_sensor.door_mailbox_contact","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"contacts","output_results_count":1,"x":300,"y":720,"wires":[["1c95b4f0.f2d05b"]]},{"id":"1c95b4f0.f2d05b","type":"api-current-state","z":"d172fd28.b50b3","name":"door status","server":"","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.door_front_contact","state_type":"str","state_location":"","override_payload":"none","entity_location":"","override_data":"none","blockInputOverrides":false,"x":486,"y":720,"wires":[["702744f5.8b4bfc"],["167451f2.e0665e"]]},{"id":"702744f5.8b4bfc","type":"api-call-service","z":"d172fd28.b50b3","name":"Lock Door","server":"","version":1,"debugenabled":false,"service_domain":"lock","service":"lock","entityId":"lock.front_door","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":694,"y":720,"wires":[["40369aac.0d8a94"]]},{"id":"65b73412.a62d6c","type":"inject","z":"d172fd28.b50b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":122,"y":720,"wires":[["16cf4c8c.6f8133"]]},{"id":"40369aac.0d8a94","type":"ha-wait-until","z":"d172fd28.b50b3","name":"Wait for door to lock","server":"","outputs":2,"entityId":"lock.front_door","entityIdFilterType":"exact","property":"attributes.lock_state","comparator":"is_not","value":"unlocked","valueType":"str","timeout":"30","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"frontDoor","entityLocationType":"msg","checkCurrentState":true,"blockInputOverrides":true,"x":948,"y":720,"wires":[["167451f2.e0665e"],["167451f2.e0665e"]]},{"id":"76134262.52c20c","type":"template","z":"d172fd28.b50b3","name":"Format Friendly Name","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":724,"y":768,"wires":[["de05ba8.d4d5548"]]},{"id":"de05ba8.d4d5548","type":"join","z":"d172fd28.b50b3","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":", ","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":898,"y":768,"wires":[["7d2d900e.403ad"]]},{"id":"7d2d900e.403ad","type":"change","z":"d172fd28.b50b3","name":"door open msg","rules":[{"t":"set","p":"openDoors","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1056,"y":768,"wires":[["92b2957.f645168"]]},{"id":"167451f2.e0665e","type":"switch","z":"d172fd28.b50b3","name":"doors open?","property":"contacts.length","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":134,"y":768,"wires":[["37b80f05.b4449"],["f103cb09.3d1508"]]},{"id":"e7d6262a.c24358","type":"split","z":"d172fd28.b50b3","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":466,"y":768,"wires":[["76134262.52c20c"]]},{"id":"92b2957.f645168","type":"switch","z":"d172fd28.b50b3","name":"door locked?","property":"frontDoor.attributes.lock_state","propertyType":"msg","rules":[{"t":"eq","v":"locked","vt":"str"},{"t":"eq","v":"unlocked","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":694,"y":816,"wires":[["e565c834.4b8e28"],["ac090d29.f2f34"]]},{"id":"37b80f05.b4449","type":"change","z":"d172fd28.b50b3","name":"to payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"contacts","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":768,"wires":[["e7d6262a.c24358"]]},{"id":"f103cb09.3d1508","type":"switch","z":"d172fd28.b50b3","name":"door locked?","property":"frontDoor.attributes.lock_state","propertyType":"msg","rules":[{"t":"eq","v":"locked","vt":"str"},{"t":"eq","v":"unlocked","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":816,"wires":[["f5a31293.e15e"],["ab19218.ec783e"]]},{"id":"f5a31293.e15e","type":"change","z":"d172fd28.b50b3","name":"Home Secured","rules":[{"t":"set","p":"payload","pt":"msg","to":"Home Secured","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":496,"y":816,"wires":[["749a3998.eea828"]]},{"id":"e565c834.4b8e28","type":"template","z":"d172fd28.b50b3","name":"Home locked with doors open","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Home locked, but the {{openDoors}} are open.","output":"str","x":930,"y":816,"wires":[["749a3998.eea828"]]},{"id":"ab19218.ec783e","type":"change","z":"d172fd28.b50b3","name":"Home Unlocked","rules":[{"t":"set","p":"payload","pt":"msg","to":"Home unlocked","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":496,"y":864,"wires":[["749a3998.eea828"]]},{"id":"ac090d29.f2f34","type":"template","z":"d172fd28.b50b3","name":"Home unlocked with doors open","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Home unlocked and the {{openDoors}} are open.","output":"str","x":930,"y":864,"wires":[["749a3998.eea828"]]},{"id":"749a3998.eea828","type":"debug","z":"d172fd28.b50b3","name":"do stuff","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1148,"y":912,"wires":[]}]
1 Like

Thanks SO much for the help! I altered the code in the first example to point to the right spot for the lock status but it works perfectly. I’ve been trying to figure this all out for days now. Both of these are amazing examples that I’m going to keep around and study!!!

I also definitely wouldn’t have figured out that second example. It’s kind of what I had in my head but I clearly was struggling with the implementation :smiley:

Did some more testing and made a few other changes (need lock status if door is open), and minor editing to the function code. Anyways, here’s the final code at this time (minus my telegram notification).

[{"id":"d172fd28.b50b3","type":"tab","label":"Doors","disabled":false,"info":""},{"id":"de366a74.117718","type":"ha-get-entities","z":"d172fd28.b50b3","server":"4c7d9772.e29fa8","name":"contacts","rules":[{"property":"entity_id","logic":"includes","value":"binary_sensor.door_back_contact, binary_sensor.door_front_contact, binary_sensor.door_garage_contact, binary_sensor.door_mailbox_contact","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"contacts","output_results_count":1,"x":260,"y":140,"wires":[["5cd957b0.532e28"]]},{"id":"5cd957b0.532e28","type":"api-current-state","z":"d172fd28.b50b3","name":"door status","server":"4c7d9772.e29fa8","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.door_front_contact","state_type":"str","state_location":"","override_payload":"none","entity_location":"","override_data":"none","blockInputOverrides":false,"x":420,"y":140,"wires":[["9b92059.ee365f8"],["c3cce78c.54b9f8"]]},{"id":"9b92059.ee365f8","type":"api-call-service","z":"d172fd28.b50b3","name":"Lock Door","server":"4c7d9772.e29fa8","version":1,"debugenabled":false,"service_domain":"lock","service":"lock","entityId":"lock.front_door","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":90,"wires":[["a10d44e4.867d38"]]},{"id":"17fa7cb7.854d23","type":"function","z":"d172fd28.b50b3","name":"format","func":"let status;\nconst openDoors = msg.contacts.map(door => door.attributes.friendly_name).join(\", \");\n\nif(msg.frontDoor.state === \"locked\") {\n    if(openDoors) {\n        status = `Home locked. ${openDoors} open.`;\n    } else {\n        status = \"Home Secured\";    \n    }\n} else {\n    status = `Home unlocked`;\n    if(openDoors) {\n        status = `${status}. ${openDoors} open`;\n    }\n}\n\nmsg.payload = status;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":950,"y":150,"wires":[[]]},{"id":"63ff7e9.bf39d8","type":"inject","z":"d172fd28.b50b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":114,"y":140,"wires":[["de366a74.117718"]]},{"id":"a10d44e4.867d38","type":"ha-wait-until","z":"d172fd28.b50b3","name":"Wait for door to lock","server":"4c7d9772.e29fa8","outputs":2,"entityId":"lock.front_door","entityIdFilterType":"exact","property":"attributes.lock_state","comparator":"is_not","value":"unlocked","valueType":"str","timeout":"5","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"frontDoor","entityLocationType":"msg","checkCurrentState":true,"blockInputOverrides":true,"x":780,"y":90,"wires":[["17fa7cb7.854d23"],["17fa7cb7.854d23"]]},{"id":"c3cce78c.54b9f8","type":"api-current-state","z":"d172fd28.b50b3","name":"lock status","server":"4c7d9772.e29fa8","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"lock.front_door","state_type":"str","state_location":"","override_payload":"none","entity_location":"frontDoor","override_data":"msg","blockInputOverrides":false,"x":710,"y":150,"wires":[["17fa7cb7.854d23"]]},{"id":"4c7d9772.e29fa8","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]