Last person home

I’ve googled a lot, found nodes to count persons at home using Template

{{ [states.person.mum,
    states.person.mickey,
    states.person.mini, 
    states.person.donald, 
    states.person.me]
    | rejectattr('state','equalto','not_home')
    |list
    | length
 }}

found nodes to know who’s home using template

{{ [states.person.mum,
    states.person.mickey,
    states.person.mini, 
    states.person.donald, 
    states.person.me]
    | rejectattr('state','equalto','not_home')
    | map(attribute='entity_id' )
    | join(', ')}}

I also have a group for these 5 persons and can trigger when the group is not_home using the Trigger state node.

I tried the trigger state node with 5 constraints going from home to not_home.

My problem is if I count persons I’m looking for zero, and if there’s nobody’s home I can’t ask for who’s home as it will be an empty string.

I’m still banging my head over the wall to get the last person at home.
After that I’m planning to check if alarm is on and if not, to send a notification to arm the alarm to this last person.
I don’t want it to be armed automatically as it happens that people not from the 5 stays at home while the 5 are away.

If ever someone has a flow to get the last person…

Thanks for help

V.

1 Like

I made an input_number.
Whenever one person leaves subtract one.
When someone comes home add one.
I do a sanity check when everyone has left to make sure it’s 0, but you’re amount of people to count is higher.
Exponents are not in your favor I’m seeing.
Luckily not a problem for me until my kids are much older. :stuck_out_tongue_winking_eye:

Not really all that tested but might give you some ideas.

[{"id":"6a58bfa9.c7d69","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"person\\..*","entityidfiltertype":"regex","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":318,"y":800,"wires":[["3232fccb.ec0e44"]]},{"id":"3232fccb.ec0e44","type":"ha-get-entities","z":"ffbd7f06.4a014","name":"","rules":[{"property":"entity_id","logic":"starts_with","value":"person.","valueType":"str"},{"property":"state","logic":"is","value":"home","valueType":"str"}],"output_type":"array","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":532,"y":800,"wires":[["cec8112d.7634d"]]},{"id":"cec8112d.7634d","type":"function","z":"ffbd7f06.4a014","name":"","func":"lastPerson = context.get(\"lastPerson\") || [];\nif(msg.payload.length === 0) {\n    if(lastPerson.length === 0) {\n        msg.payload = \"No home now but we don't know who the last person was.\";\n        return [null, msg];\n    }\n\n    const friendlyNames = lastPerson.map(p => p.attributes.friendly_name).join(\",\");\n    msg.payload = `${friendlyNames} was the last to leave`;\n    // contains a array of the last people to be home\n    msg.data = lastPerson;\n    return [msg, null];\n}\ncontext.set(\"lastPerson\", msg.payload);\n","outputs":2,"noerr":0,"x":672,"y":800,"wires":[["45162574.94578c"],["465f559b.bbe91c"]]},{"id":"45162574.94578c","type":"debug","z":"ffbd7f06.4a014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":836,"y":784,"wires":[]},{"id":"465f559b.bbe91c","type":"debug","z":"ffbd7f06.4a014","name":"unknown","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":826,"y":832,"wires":[]}]
1 Like

this is how i do it…

few input booleans and this:

[{"id":"55d0252a.44c9fc","type":"tab","label":"Flow 13","disabled":false,"info":""},{"id":"f5a44351.71108","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Jim Home?","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.jimstatus","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":120,"y":240,"wires":[[],["b796eb45.509158"]]},{"id":"e0b6b681.a44b38","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Danelle Home?","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.danellestatus","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":130,"y":300,"wires":[[],["98f5a1a0.12ba2"]]},{"id":"b796eb45.509158","type":"api-current-state","z":"55d0252a.44c9fc","name":"Nelle Home?","server":"663af70e.88b558","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.danellestatus","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":480,"y":240,"wires":[["2279a6dc.25b92a"],["c009b280.e5c0c"]]},{"id":"98f5a1a0.12ba2","type":"api-current-state","z":"55d0252a.44c9fc","name":"Jim Home?","server":"663af70e.88b558","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.jimstatus","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":480,"y":300,"wires":[["2279a6dc.25b92a"],["e601fc18.22216"]]},{"id":"d72839f0.bfa138","type":"api-call-service","z":"55d0252a.44c9fc","name":"House-Away-mode","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.ha_mode_away","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":910,"y":280,"wires":[[]]},{"id":"2279a6dc.25b92a","type":"api-current-state","z":"55d0252a.44c9fc","name":"Any Guests?","server":"663af70e.88b558","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_boolean.house_guests","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":720,"y":280,"wires":[["d72839f0.bfa138"],[]]},{"id":"c2158e6e.c6c23","type":"api-call-service","z":"55d0252a.44c9fc","name":"Away-jimstatus","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.jimstatus","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":320,"y":190,"wires":[["b796eb45.509158"]]},{"id":"7726f72f.1e3838","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Jim Leaving","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"person.jim","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":120,"y":190,"wires":[[],["c2158e6e.c6c23"]]},{"id":"91214848.93b5c8","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Danelle Leaving","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"person.danelle","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":130,"y":360,"wires":[[],["693bf11f.0f3ec"]]},{"id":"693bf11f.0f3ec","type":"api-call-service","z":"55d0252a.44c9fc","name":"Away-danstatus","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.danellestatus","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":320,"y":360,"wires":[["98f5a1a0.12ba2","e5411f0b.b7cd3"]]},{"id":"26184aa9.bbb086","type":"api-call-service","z":"55d0252a.44c9fc","name":"turn lights off/etc","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.front_door_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":960,"y":200,"wires":[[]]},{"id":"c009b280.e5c0c","type":"time-range-switch","z":"55d0252a.44c9fc","name":"sunsetStart","lat":"33.5598613","lon":"-112.2798811","startTime":"sunsetStart","endTime":"23:59","startOffset":"","endOffset":"","x":770,"y":200,"wires":[["26184aa9.bbb086"],[]]},{"id":"6c8bccce.5b1454","type":"api-call-service","z":"55d0252a.44c9fc","name":"turn lights off/on/etc..","server":"663af70e.88b558","version":1,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.front_door_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":990,"y":350,"wires":[[]]},{"id":"e601fc18.22216","type":"time-range-switch","z":"55d0252a.44c9fc","name":"sunsetStart","lat":"33.5598613","lon":"-112.2798811","startTime":"sunsetStart","endTime":"23:59","startOffset":"","endOffset":"","x":760,"y":360,"wires":[["6c8bccce.5b1454"],[]]},{"id":"e5411f0b.b7cd3","type":"api-call-service","z":"55d0252a.44c9fc","name":"Notify Danelle Left","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"notify","service":"habot","entityId":"","data":"{\"message\":\"Danelle has left\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":540,"y":400,"wires":[[]]},{"id":"ebdeb3cf.dca03","type":"comment","z":"55d0252a.44c9fc","name":"1. Check who is leaving and whos home - set house state.","info":"Check who is leaving and whos home - set house state...","x":260,"y":120,"wires":[]},{"id":"6abbb442.467eec","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Jim Home","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.jimstatus","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":100,"y":570,"wires":[[],["ecca001f.24759"]]},{"id":"89f95a11.ae4108","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Danelle Home","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.danellestatus","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":110,"y":630,"wires":[[],["ecca001f.24759"]]},{"id":"ecca001f.24759","type":"api-call-service","z":"55d0252a.44c9fc","name":"house-home-mode","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.ha_mode_home","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":530,"y":600,"wires":[[]]},{"id":"da9d017f.8236a","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Jim Arriving","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"person.jim","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":110,"y":510,"wires":[["218fc88f.984378"],[]]},{"id":"bc4e1314.75c4c","type":"server-state-changed","z":"55d0252a.44c9fc","name":"Danelle Arriving","server":"663af70e.88b558","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"person.danelle","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":120,"y":690,"wires":[["8d6ad021.c91fd"],[]]},{"id":"218fc88f.984378","type":"api-call-service","z":"55d0252a.44c9fc","name":"home-jimstatus","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.jimstatus","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":310,"y":510,"wires":[["ecca001f.24759"]]},{"id":"8d6ad021.c91fd","type":"api-call-service","z":"55d0252a.44c9fc","name":"home-danstatus","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.danellestatus","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":310,"y":700,"wires":[["ecca001f.24759","910e1f43.2ef0f"]]},{"id":"910e1f43.2ef0f","type":"api-call-service","z":"55d0252a.44c9fc","name":"Notify Danelle Home","server":"663af70e.88b558","version":1,"debugenabled":false,"service_domain":"notify","service":"habot","entityId":"","data":"{\"message\":\"Danelle has arrived\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":550,"y":700,"wires":[[]]},{"id":"52d7ab09.d17514","type":"comment","z":"55d0252a.44c9fc","name":"2. Check who is arrving and - set house state.","info":"","x":210,"y":450,"wires":[]},{"id":"663af70e.88b558","type":"server","z":"","name":"Home Assistant-mine","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

its not exactly last person home because im only tracking myself and the wife, but youu should be able to go from these ideas… maybe check the last-changed attributes on the person./etc. to late to think about it right now … good luck

That was what I was thinking of, using a context variable to store person at home when there’s still someone home and then use this variable when there’s no one.

I’ll try and let you know…

Thanks

I did test it simulating away by modifying persons states. It works, first output of the function sends the Friendly name of the person as payload. After that I put a switch with 5 outputs and a Notify node on each output of the switch. I only tested one for one person so far.

I’m sure there’s a more elegant way to do it, I mean getting the person from the flow and having only one notify node at the end but it works for me.

Thanks a lot.

What is this for? Looks fine to me… elegant is kind of a subjective thing… if you can clearly see what’s going on it’s elegant enough.

I just have nodered notify everyone if anyone left the house empty with a door unlocked or window open. That way I can keep tabs if anyone in my family is starting to loose their short term memory, LOL!

Have you looked at the zone entity? Its state reflects the number of people it detects.
For example, I have a “zone.home” and it’s state is “1” when just me there, its persons attribute is set to “person.kev”.

If you DON’T want to use Node Red then you can use a simple Automation of:

Triggers
Person A leaves Home Zone
Person B
Person C
Person D
Person E

Conditions
Person A is NOT in home zone
Person B
Person C
Person D
Person E

Action
Set Alarm to Away (Or whatever you want such as change temps on thermostat)