How to create "when last person leaves" & "when first person arrives" automation

Hi, I currently have two automation in Apple Homekit for the househould, where lights turns off and on with “when last person leaves” & “when first person arrives” automation. I would like to tranfer this automation to my Home assistant. But I am not sure how. I currently have two users with iPhones set up as tracking devices. But I am uncertain how to configure the trigger. Any suggestions?

2 Likes

create a template sensor (Template - Home Assistant) which counts the device trackers at home and use it as numeric_state trigger (Automation Trigger - Home Assistant) for your automation:

template:
  - sensor:
      - name: persons_at_home
        icon: mdi:account-supervisor-circle
        state: "{{ states.device_tracker | selectattr('state', 'eq', 'home') | list | count }}"

And the automation trigger example:

  - id: nobody_somebody_home
    alias: nobody_somebody_home
    mode: queued
    trigger:                
      - id: 'not_home'
        platform: state
        entity_id: sensor.persons_at_home
        to: '0'
        for: '00:30:00'
      - id: 'home'
        platform: numeric_state
        entity_id: sensor.persons_at_home
        above: 0
        for: '00:30:00'
2 Likes

Groups

EDIT / UPDATE:

As of 2022.5 the way zones work have changed and many groups (though, not person groups) can now be set up in the GUI. With these changes, it is now easier to use a numeric-state trigger based on the zone.home entity.

trigger:
  - platform: numeric_state
    entity_id: zone.home
    below: 1
  action:
  - service: light.turn_off
    target:
6 Likes

You can create a group

EDIT : @Didgeridrew you was faster than me :laughing:

1 Like

Will this work?

Yes, as long as your configuration.yaml includes the new groups.yaml. If the device trackers are already assigned to a person, you don’t need to include them in the group.

Yes, like this:

I noticed today that the automation did not trigger. I can read from the log that group family have been detected away, but the automation wont start:

This is how my group looks like; tracker device is one iPhone each.


I notice there is an error in the automation;

Group family is set as away, but no further action to my automation;

When binary goes on, at least 1 person is home. If it goes off, everybody is away.

binary_sensor:
  - platform: template
    sensors:
      somebody_home:
        friendly_name: Somebody home?
        value_template: >
          {% if (states('person.1') and states('person.2') and ...... and states('person.n')) != 'home'%}
            off
          {% else %}
            on
          {% endif %}

Thanks for reply. I tried this one. Created the sensor, but I can´t get that numeric state trigger configured. Struggeling to close the code. Could you please paste the full code in code format?

Just use a State trigger, not a Zone trigger:

  trigger:
  - platform: state
    entity_id: group.family
    to: not_home
  action:
  - service: light.turn_off
    target:

Even though it is displayed in the frontend as Home or Away, the group’s state is actually ‘home’ or ‘not_home’. It will be ‘home’ when the first person comes home/as long as 1 person is home and ‘not_home’ after the last person leaves.

EDIT / UPDATE:

As of 2022.5 the way zones work have changed and many groups (though, not person groups) can now be set up in the GUI. With these changes, it is now easier to use a numeric-state trigger based on the zone.home entity to determine whether the house is occupied or not.

trigger:
  - platform: numeric_state
    entity_id: zone.home
    below: 1
  action:
  - service: light.turn_off
    target:
4 Likes

Thanks a lot, I will give this a try.

alias: Turn All Lights off When We Leave
trigger:
  - entity_id: group.family
    platform: state
    from: home
    to: not_home
condition: []
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.alle_lys

Hi,

I’m struggling with the same issue. I used a helper switch (input_boolean) instead of the device tracker because the update of the device tracker is way later then when someone arrives home, so I adjusted the code a bit to.

"{{ states.input_boolean | selectattr('state', 'eq', 'on') | list | count }}"

This works but it counts all my boolean and I want just two specific ones (input_boolean.person1 and input_boolean.person2. I tried to change the code but I cant get it to work. hope someone can help me with this.

{{ expand('input_boolean.person1', 'input_boolean.person2')
| selectattr('state', 'eq', 'on') | list | count }}

Wow you are good. it works. Wish I had a list with all possible commands and how to use it, or if you start typing is shows what it expects, a bit like you have with VBA or PowerApps.
Screenshot 2022-10-21 155625

Any way many thanks!!!

Hi! I was looking for a solution for my case and I found yours. How do you think that I can make it work your automation but only once a day? Because I want to automate the vacuum cleaner. I want to start it when everyone is out of home, but, obviously, I only want it to make it once a day.

Thanks in advance.

You can add a Template condition to throttle how often the automation fires.

triggers:
  - trigger: numeric_state
    entity_id: zone.home
    below: 1
conditions:
  - alias: Run only if today's date is not the same as the last date this automation ran
    condition: template
    value_template: |-
      {{today_at().date() != (this.attributes.last_triggered | default(as_datetime(0),1)).date() }}
actions:
  - actions: light.turn_off
    target:
      entity_id: light.example

Wht do you think about this solution? It would be optimal if I want to trigger the automation between certain hours and once a day:

triggers:
  - trigger: numeric_state
    entity_id: zone.home
    below: 1
conditions:
  - alias: Run only if today's date is not the same as the last date this automation ran
    condition: template
    value_template: >
    {% set nw = now() %}
    {% set last = state_attr('automation.ventilate_game_room', 'last_triggered') %}
    {{ 10 <= nw.hour < 20 and
       (last is none or (nw - last).total_seconds() > 10 * 60 * 60) }}
actions:
  - actions: light.turn_off
    target:
      entity_id: light.example

Or maybe this one:

triggers:
  - trigger: numeric_state
    entity_id: zone.home
    below: 1
condition:
  - condition: time
    after: '10:00:00'
    before: '20:00:00'
  - "{{ state_attr(this.entity_id, 'last_triggered') < today_at('10:00') }}"
actions:
  - actions: light.turn_off
    target:
      entity_id: light.example

If it meets your needs, it’s fine.

The reason I used the this variable instead of the state_attr() function is habit. In the actions block, (like in conditions of an If/Then or Choose action) this.attributes.last_triggered and state_attr('automation.example', 'last_triggered') will have different values, so you have to be careful to use the right one for the circumstance… the this version is almost always the value that is actual wanted, so I use that version in the conditions block as well.

EDIT: Added explanation for use of this

This seems to be simpler if I want to add a time window, isnt’ it?