How to get a pushnotification when last person leaves home

hey guys,

so i use a group called group.home to check if someone is home.
lets say there are 3 people in it:
person.1
person.2
person.3

i use it in an automation to turn lights off and some other stuff when the last person leaves my house. (when group.homechanges to not_home)

now i´d like to get a push-notifiaction on my phone when i´m not home and the last person leaves the house, also i´d like to know what person left.

is there a jinja-template to show who triggered the automation or do i need to set up a seperar tigger for every person in the automation?

Trigger on the state of zone.home turning to 0.

The state of a zone is a number, which represents the number of persons that are currently in a zone.

For the action you will use a service call to whatever notifier you are using. For the person’s name you can use the following in the message:

{{ states.person | selectattr('state', 'eq', 'not_home')
| sort(attribute='last_changed', reverse=1) | map(attribute='attributes.friendly_name') | first }}
2 Likes

one person isn´t using the app/gps. it´s only a device_tracker checking if the person is connected to the wlan. so i guess zone.home won´t be working.

triggering isn´t the problem, it works great using the group.
i´m trying to find a good way to get a notification for the last person leaving (if i´m not home and i´m not the last person leaving).

If you tie the non-gps device tracker to the person it should still work for zone.home despite gps coordinates being unknown.

The last person leaving I would try by triggering on all persons individually leaving home, in the conditions check if zone.home is 0. Then the entity triggering it is the entity that made zone.home go to 0. You can do that with one state trigger listing all persons, because trigger.entity_id should then be able to tell you which entity it was.

Do note that gps trackers may have different response times than e.g. wifi based trackers, so when two people leave together, the one with the slowest responding location tracking will be in the notification.

that´s the way i have it now. trigger is every single person, condition is group.home is 0 as action it selects the person that left and sends the notification.

was hoping i can use the group.home as trigger and use a jinja-template that shows the person that triggered group.home to 0

You might be able to iterate members and check which one last updated, but it would be quite difficult. So unless you have a very fast growing family, I’d leave it as it is :wink:

currently there aren´t any plans to grow the family :smiley: so ye, you might be right :slight_smile:

Your orignal post alluded that the group was composed of a number of person entities… if that is the case you can use the template I posted earlier.

If, instead, it is actually a mixture of person and device_tracker entities you can use the group

trigger:
  - platform: state
    entity_id: group.home
    to: not_home
    from: home
    variables:
      last: |
        {{ expand( trigger.to_state.attributes.entity_id )
        | sort(attribute='last_changed', reverse=1) 
        | map(attribute='attributes.friendly_name') | first }}
condition:
  - alias: mase is not last
    condition: template
    value_template: "{{ last != 'Mase' }}"
action:
  - service: notify.YOUR_NOTIFIER
    data:
      message: "{{ last }} was the last to leave"
1 Like

will give this a try, thanks!

EDIT: works, thanks for your help!