Device tracker "home since" time calculating

Hello everyone.
How can i calculate time since some persons come home? In motion sensor it called “No motion since”, in door/window sensor “Open since”. Is it possible to make custom sensor for that?

Try:

sensor:
  - platform: template
      last_time_someone_left_home:
        entity_id:
          - person.01
          - person.02
          - person.03
        friendly_name: "When was the last time that someone left home?"
        value_template: >-
          {{ as_timestamp(states.person | selectattr('state', 'eq', 'not_home') | list | map(attribute='last_changed') | max) | timestamp_custom('%d.%m.%Y %H:%M') }}
      last_time_someone_came_home:
        entity_id:
          - person.01
          - person.02
          - person.03
        friendly_name: "When was the last time that someone came home?"
        value_template: >-
          {{ as_timestamp(states.person | selectattr('state', 'eq', 'home') | list | map(attribute='last_changed') | max) | timestamp_custom('%d.%m.%Y %H:%M') }}
      last_indoor_movement:

You can replace the person with the device tracker. A person can combine several device trackers.

If you want the time difference between now and someone left the house then you can work with something like this:

  time_since:
    value_template: >-
      {% set minutes = ( as_timestamp(states.entity_id.state) - as_timestamp(now()) ) | timestamp_utc %}
          {{ strptime(minutes, '%Y-%m-%d %H:%M:%S').strftime('%H:%M') }} hrs

However, you can also use the format “relative” when showing entities in the font end:

Thank you for reply, but i use Mikrotik device_tracker, that has no “last_changed” option.

Every entity has a last_changed attribute.

1 Like