Check State of Multiple Devices

I have a good number of complex flows that check the states of multiple entities. Right now I am doing this simply by stringing together current state nodes. Most of the time this works but recently I have noticed that on occasion the message is not making it through the second (or third, or fourth, etc) current state node to my call service node. Here is an example. Let me know if there is a better way to call a service based on multiple entities states.

[{"id":"c26117c1.22d9c8","type":"server-state-changed","z":"726d4ff.9337ab","name":"Matt Home","server":"9c6d9a8c.1f52f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.matt_s_iphone","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":180,"y":220,"wires":[["d064da2a.769228"],["2f5abb55.e96bc4"]]},{"id":"d064da2a.769228","type":"api-call-service","z":"726d4ff.9337ab","name":"Guest On","server":"9c6d9a8c.1f52f8","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.guests","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":780,"y":220,"wires":[[]]},{"id":"2eae0e7a.28e732","type":"api-call-service","z":"726d4ff.9337ab","name":"Guest Off","server":"9c6d9a8c.1f52f8","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.guests","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":780,"y":380,"wires":[[]]},{"id":"ab7894ab.b90688","type":"server-state-changed","z":"726d4ff.9337ab","name":"Jan Home","server":"9c6d9a8c.1f52f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.jan_iphone","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":180,"y":180,"wires":[["d064da2a.769228"],["38a81f79.dc3d1"]]},{"id":"2f5abb55.e96bc4","type":"api-current-state","z":"726d4ff.9337ab","name":"Jan Away","server":"9c6d9a8c.1f52f8","version":1,"outputs":2,"halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"device_tracker.jan_iphone","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":340,"y":360,"wires":[["a0488a2d.7441b8"],[]]},{"id":"38a81f79.dc3d1","type":"api-current-state","z":"726d4ff.9337ab","name":"Matt Away","server":"9c6d9a8c.1f52f8","version":1,"outputs":2,"halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"device_tracker.matt_s_iphone","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":350,"y":420,"wires":[["9e15af60.d15cd"],[]]},{"id":"8e6a8b33.a1df48","type":"server-state-changed","z":"726d4ff.9337ab","name":"Bob Home","server":"9c6d9a8c.1f52f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.bob_iphone","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":180,"y":260,"wires":[["d064da2a.769228"],["5753e2d2.c65d3c"]]},{"id":"a0488a2d.7441b8","type":"api-current-state","z":"726d4ff.9337ab","name":"Bob Away","server":"9c6d9a8c.1f52f8","version":1,"outputs":2,"halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"device_tracker.bob_iphone","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":520,"y":360,"wires":[["2eae0e7a.28e732"],[]]},{"id":"9e15af60.d15cd","type":"api-current-state","z":"726d4ff.9337ab","name":"Bob Away","server":"9c6d9a8c.1f52f8","version":1,"outputs":2,"halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"device_tracker.bob_iphone","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":520,"y":420,"wires":[["2eae0e7a.28e732"],[]]},{"id":"5753e2d2.c65d3c","type":"api-current-state","z":"726d4ff.9337ab","name":"Matt Away","server":"9c6d9a8c.1f52f8","version":1,"outputs":2,"halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"device_tracker.matt_s_iphone","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":350,"y":480,"wires":[["18fff5a4.5ab94a"],[]]},{"id":"18fff5a4.5ab94a","type":"api-current-state","z":"726d4ff.9337ab","name":"Jan Away","server":"9c6d9a8c.1f52f8","version":1,"outputs":2,"halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"device_tracker.jan_iphone","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":520,"y":480,"wires":[["2eae0e7a.28e732"],[]]},{"id":"9c6d9a8c.1f52f8","type":"server","z":"","name":"Home Assistant"}]

I typically use multiple state nodes feed into a function node and then just write code to figure out what to do:

Another trick that I use is to have an event states node tied to a particular device and then have it update a global variable that I use in other flows using either switch nodes or anything else that can read the global namespace.

This captures all my motion sensors and creates/updates global variables out of them:

Finally, my global state (sorry, I just restarted Node-Red, so not everything has populated yet):

1 Like

Hello, you didn’t say how you work further with those states. But it seems to me like good candidate for Get Entities node

you can do this “simply” with a render template node, also. make the template output JSON, then use a function node to parse the string to JSON.

3 Likes

I can’t find the ‘render template node’. Also do you have a flow examaple?

This will do it:

[{"id":"e763820b.d835a","type":"server-state-changed","z":"6cd0b2f8.4bb6fc","name":"Anybody home?","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.jan_iphone, device_tracker.matt_s_iphone, device_tracker.bob_iphone","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":300,"y":900,"wires":[["223f2161.45baae"],["86232ce0.2f2e5"]]},{"id":"223f2161.45baae","type":"api-call-service","z":"6cd0b2f8.4bb6fc","name":"Guest On","server":"9c6d9a8c.1f52f8","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.guests","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":480,"y":880,"wires":[[]]},{"id":"86232ce0.2f2e5","type":"ha-get-entities","z":"6cd0b2f8.4bb6fc","server":"9405c3fe.d0a6c","name":"not_home?","rules":[{"property":"entity_id","logic":"includes","value":"device_tracker.jan_iphone, device_tracker.matt_s_iphone, device_tracker.bob_iphone","valueType":"str"},{"property":"state","logic":"is","value":"not_home","valueType":"str"}],"output_type":"count","output_empty_results":false,"output_location_type":"msg","output_location":"_count","output_results_count":1,"x":490,"y":940,"wires":[["3128e5e6.d7494a"]]},{"id":"3128e5e6.d7494a","type":"switch","z":"6cd0b2f8.4bb6fc","name":"all 3?","property":"_count","propertyType":"msg","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":630,"y":940,"wires":[["1fc2c86f.984fe8"]]},{"id":"1fc2c86f.984fe8","type":"api-call-service","z":"6cd0b2f8.4bb6fc","name":"Guest Off","server":"9c6d9a8c.1f52f8","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.guests","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":760,"y":940,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true},{"id":"9c6d9a8c.1f52f8","type":"server","name":"Home Assistant"}]
1 Like