Automation for when last person leaves home not working

looking for some help on an automation.
i’ve set it up so that, when the last person leaves the house, it turns off everything, adjusts the heating temp etc etc

I’ve set up what i thought would be correct, but the automation never triggers, even though HA knows that there is no-one “home”.

alias: When The Last Person Leaves Home
description: Automation that turns off all non-required devices when no one is home
triggers:
  - trigger: numeric_state
    entity_id:
      - zone.home
    attribute: persons
    below: 1
    for:
      hours: 0
      minutes: 0
      seconds: 10
    alias: When no-one is at home
conditions: []
actions:
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      area_id:
        - bedroom
        - living_room
        - kitchen
        - hall
        - garden
        - office
    alias: Turn off lights in all rooms
  - action: climate.set_temperature
    metadata: {}
    data:
      temperature: 15
    target:
      entity_id:
        - climate.office
        - climate.bedroom
        - climate.living_room
    alias: Set temperature in all rooms to 15 degrees
  - type: turn_off
    device_id: c366ceaef0c95a2e4d8c98d5255f18fc
    entity_id: 4ed714acbd203f1e9eeb1057be1d9922
    domain: switch
    alias: Turn off bedroom de-humidifier
  - type: turn_off
    device_id: 8406611c0b8878e3c427f28db334558d
    entity_id: 730beb7699275c4ccf0a8aca5f9fb08b
    domain: switch
    alias: Turn off bedroom fan
  - action: media_player.turn_off
    metadata: {}
    data: {}
    target:
      device_id:
        - a96f23dc8f9ce8d2439eecb5fcdf6b76
        - 7549bd55ee54b23e2061a796c7228a54
        - 1b58b03343cc3ac38df5c2616ef0b206
    enabled: true
    alias: Turn off media players
  - action: notify.mobile_app_simons_iphone
    metadata: {}
    data:
      message: test no-one is home
      title: My Home
    alias: Send notification to Simons phone
mode: single

and this is what HA was reporting when no-one was home (i had left the house about 5 minutes earlier - other person had left much earlier than me)

i’m guessing the automation isn’t picking if the change from 1 to 0 in zone.home or something like that, as it appears all the other triggers are correct

Your template editor code references states('zone.home') to get the 0, but your trigger is paying attention to the ‘persons’ attribute of that entity rather than its state.

1 Like

right, i thought they would both essentially do the same thing.

so persons being below 1 isn’t the same as state being zero?

this is what i had set in the UI

It is the same thing.
Just remove the attribute.
The attribute holds the persons that is currently home.

1 Like

If you are open for a different solution:

As triggers I use the moment a person changes from home to away
In the conditions I check if all persons are away.

I have a yaml created group called family.
And in our family group we have a boolean and all family members.
That means we have an option to place someone home using a simple flip of a boolean.

cool, so just remove that attribute and it should just start working?

1 Like

Yes, the second “line” in the GUI says attribute: persons. Click on the X there

i shall do that.

You should give the solution to Dominic.
His post says the same thing I did but in a different way.

managed to finally test this today in a real world environment and it worked correctly.