History Stats: How to track 'not at home' when using zones?

Just wondering how to track the real “not at home” times using History Stats. I’m currently using state: 'not_home' but that doesn’t work if using zones—whenever I’m at work or in a friend’s zone, I’m still “not at home” but this time wouldn’t add to the sensor.

sensor:

  # Not home hours today
  - platform: history_stats
    name: Unterwegs heute
    entity_id: person.matthias
    state: 'not_home'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

  # Not home hours this week
  - platform: history_stats
    name: Unterwegs diese Woche
    entity_id: person.matthias
    state: 'not_home'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0, minute=0, second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'

Now do I really have to add an extra template sensor for each person I want to track not being at home or is there an easier way to do this?

To make it clear, I wish to track {{ not is_state('person.matthias', 'home') }} (for me and some others), so even if my state would be “Work” or “Home Friend’s Place” it would count towards “not at home”.

Hi Matthias @Moonbase59 - apologies for necro posting but have you ever found a solution for this? :slight_smile:

The template he wrote is almost correct.

{{ states('person.matthias') != 'home' }}

I believe this could work too:

 {{ is_state('person.matthias', 'home') == false }}
1 Like

Also to point out, if the zones are only being used for automation, simply set them as passive so that the state doesn’t change to the name of the zone when the device is in them…

EDIT:
as would:

{{ 'home' not in states('person.matthias') }}
1 Like

Thanks both! I’ve indeed managed to add binary sensors for everyone in the house:

binary_sensor: 
# People Home Occupancy
  - platform: template
    sensors:
      matteo_home:
        friendly_name: Matteo Home
        device_class: occupancy
        value_template: "{% if is_state('person.matteo, 'home') %}on{% else %}off{% endif %}"

to then change my history_stats away sensors to:

sensor:

    # MATTEO

  - platform: history_stats
    name: Matteo AWAY today
    entity_id: binary_sensor.matteo_home
    state: 'off'
    type: time
    end: '{{ now() }}'
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'

  - platform: history_stats
    name: Matteo AWAY 7 days
    entity_id: binary_sensor.matteo_home
    state: 'off'
    type: time
    end: '{{ now() }}'
    duration:
      days: 7

  - platform: history_stats
    name: Matteo AWAY 30 days
    entity_id: binary_sensor.matteo_home
    state: 'off'
    type: time
    end: '{{ now() }}'
    duration:
      days: 30

This works 100% fine :slight_smile:

The curiosity stays if you could template this into an history_stats sensor without one of the many people sensors (person, device trackers, home occupancy now, etc. etc.), but it seems we cannot, right?

No, sadly the docs say it is a string or a list -
however the fact that it accepts a list means you could in fact list all the states that are not home, eg all your zones.

state:
  - "away"
  - "not_home"
  - "friends house"
  - "shopping"
  - "work"

etc

1 Like