Automation condition based on time of last presence

How can I trigger an event such as a light turning on based on the time between two people arriving together or separately (prefer Node Red)?

Example: If person one arrives at a location, turn on light 1. If person 2 arrives at a location, turn on light 2. If person 1 and 2 arrive at the same time (30 sec +/-) only turn on the light that was last turned off.

TIA

You can try this, but you have to change the entity_ids.

[{"id":"a4ad8741.afbff8","type":"server-state-changed","z":"51045e07.bbf87","name":"Device 1","server":"d177950.2a5aa68","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.tracker1","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,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":220,"y":180,"wires":[["6a96680b.fb9d98"],[]]},{"id":"6a96680b.fb9d98","type":"change","z":"51045e07.bbf87","name":"Device1","rules":[{"t":"set","p":"device","pt":"msg","to":"device1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":180,"wires":[["f3541184.ec6ca"]]},{"id":"f3541184.ec6ca","type":"api-render-template","z":"51045e07.bbf87","name":"Difference","server":"d177950.2a5aa68","template":"{{ states('device_tracker.device1') == \"home\" and states('device_tracker.device2') == \"home\" and(as_timestamp(states.device_tracker.device1.last_changed)|round - as_timestamp(states.device_tracker.device2.last_changed)|round)|abs < 30  }}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":510,"y":200,"wires":[["66626a01.699324"]]},{"id":"66626a01.699324","type":"switch","z":"51045e07.bbf87","name":"True Check","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"},{"t":"eq","v":"False","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":200,"wires":[["f6ed5bb9.3ae308"],["b7670c38.f621"]]},{"id":"373b3355.def62c","type":"change","z":"51045e07.bbf87","name":"Device2","rules":[{"t":"set","p":"device","pt":"msg","to":"device2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":220,"wires":[["f3541184.ec6ca"]]},{"id":"b7670c38.f621","type":"function","z":"51045e07.bbf87","name":"","func":"if (msg.device == 'device1'){\nmsg.payload = \"switch.light1\"    \nreturn msg;\n}\nif (msg.device == 'device2'){\nmsg.payload = \"switch.light2\" \nreturn msg;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":220,"wires":[["3eaf93b7.dc958c"]]},{"id":"f6ed5bb9.3ae308","type":"api-render-template","z":"51045e07.bbf87","name":"Light1","server":"d177950.2a5aa68","template":"{{ as_timestamp(states.switch.light1.last_changed)|round}}","resultsLocation":"light1","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":810,"y":180,"wires":[["28ffd768.07b418"]]},{"id":"559ce9d7.801138","type":"server-state-changed","z":"51045e07.bbf87","name":"Device 2","server":"d177950.2a5aa68","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.device2","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,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":220,"y":220,"wires":[["373b3355.def62c"],[]]},{"id":"3eaf93b7.dc958c","type":"api-call-service","z":"51045e07.bbf87","name":"Turn on","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"{{payload}}","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":960,"y":220,"wires":[[]]},{"id":"28ffd768.07b418","type":"api-render-template","z":"51045e07.bbf87","name":"Light2","server":"d177950.2a5aa68","template":"{{ as_timestamp(states.switch.light2.last_changed)|round}}","resultsLocation":"light2","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":930,"y":180,"wires":[["b5e141e7.40f99"]]},{"id":"b5e141e7.40f99","type":"function","z":"51045e07.bbf87","name":"","func":"var x = msg.light1\nvar y = msg.light2\nif (x > y){\nmsg.payload = \"switch.light2\"\nreturn msg;\n}\nif (x < y){\nmsg.payload = \"switch.light1\"\nreturn msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1060,"y":180,"wires":[["9dbeb5c.1bfc548"]]},{"id":"9dbeb5c.1bfc548","type":"api-call-service","z":"51045e07.bbf87","name":"Turn on","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"{{payload}}","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1200,"y":180,"wires":[[]]},{"id":"d177950.2a5aa68","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

That looks really good. Just have to test it. Thanks!

1 Like