Determining if people are home or away in Home Assistant

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, away is used.
  • If the zone hasn’t been occupied for at least the last 12 hours, extended is used.
  • Otherwise, home is 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 %}

Comments

4 responses to “Determining if people are home or away in Home Assistant”

  1. Claus Avatar
    Claus

    I don’t get i use these settings
    “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”

    1. Logan Avatar

      Hi Claus! This is an automation that you can add to Home Assistant. I included the YAML configuration to make it easier to add without having to click each option.

      You can go to your automations using this link: https://my.home-assistant.io/redirect/automations

      Create a new automation, click on the three dots in the upper-right corner to open the menu, and then Edit in YAML.

  2. Claus Avatar
    Claus

    Where to put the script for input_datetime?

    1. Logan Avatar

      Hi Claus! `input_datetime.away_since` is a helper in Home Assistant. You can go to your Helper’s UI using this link: https://my.home-assistant.io/redirect/helpers/

Leave a Reply

Your email address will not be published. Required fields are marked *