These snippets help us determine if people are home or away, how long they’ve been away, and what type of away mode the home is currently in. I use these in an upcoming video about controlling your home’s climate in Home Assistant.
The first is a binary sensor helper (binary_sensor.people_home) for determining if people are home. You could extend this to include readings from presence sensors, but this template only relies on the home zone’s persons attribute that lists the people currently within the zone. If your home zone is different from zone.home, make sure you update the template accordingly.
{{ state_attr('zone.home', 'persons') | length > 0 }}
The next is an automation that sets a date/time input helper (input_datetime.away_since) when the sensor above changes to zero.
alias: Set away since timestamp
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.people_home
    to: "0"
conditions: []
actions:
  - action: input_datetime.set_datetime
    metadata: {}
    data:
      datetime: "{{ now() }}"
    target:
      entity_id: input_datetime.away_since
mode: single
Bringing it all together, we create a template sensor helper (sensor.away_mode) that represents how long the zone has been empty with one of three options: home, away, and extended. 
- If the zone hasn’t been occupied for at least the last 30 minutes, awayis used.
- If the zone hasn’t been occupied for at least the last 12 hours, extendedis used.
- Otherwise, homeis used.
{% if states('binary_sensor.people_home') == 'on' %}
  home
{% elif as_local( as_datetime( states('input_datetime.away_since') ) ) < (now() - timedelta(hours=12)) %}
  extended
{% elif as_local( as_datetime( states('input_datetime.away_since') ) ) < (now() - timedelta(minutes=30)) %}
  away
{% else %}
  home
{% endif %}

Leave a Reply