Sensor to show what time someone left/arrived at home

Hi,

I use my UniFi network for presence detection in home assistant. Is it possible to create a template sensor that will show that time that someone was last home or what time they left?

So for example, if bob leaves the house at 9am, the value will be “09:00 - dd/mm/yyyy”
And if Bob came back home at 5pm, the value would be “17:00 - dd/mm/yyyy”

Thanks

Could probably be done with a template sensor that looks for the last updated attribute of your presence detection sensor.

I thought this too, but would there be a way to make it look for the value too, so that if the device tracker was “Home” then the title would be “Last Home at” and if it was “Away” it would be “Left Home at”

I’m not sure if you can alter the title like that but i know you can with the icon.

Yes you can do both.

What is your presence sensor entity id and what two icons do you want to use?

Entity: device_tracker.deans_iphone

Icon when Home: mdi:home-import-outline
Icon when Away: mdi:home-export-outline

Title when Home: Arrived Home at:
Title when Away: Left Home at:

Try this:

sensor:
  - platform: template
    sensors:
      home_away_time:
        friendly_name: "Home / Away Time"
        entity_id: device_tracker.deans_iphone
        value_template: >
          {% if states('device_tracker.deans_iphone') == home %}
            Arrived Home at: {{ states.device_tracker.deans_iphone.last_changed }}
          {% elif states('device_tracker.deans_iphone') == not_home %}
            Left Home at: {{ states.device_tracker.deans_iphone.last_changed }}
          {% else %}
            Unknown
          {% endif %}
        icon_template: >
          {% if states('device_tracker.deans_iphone') == home %}
            mdi:home-import-outline
          {% elif states('device_tracker.deans_iphone') == not_home %}
            mdi:home-export-outline
          {% else %}
            mdi:help-circle
          {% endif %}

You are probably going to have to format the time template to what you want, see here for how to:

Yes that worked, only thing was i had to add apostrophes around home and not_home. So now it looks like this:

sensor:
  - platform: template
    sensors:
      home_away_time:
        friendly_name: "Home / Away Time"
        entity_id: device_tracker.deans_iphone
        value_template: >
          {% if states('device_tracker.deans_iphone') == 'home' %}
            Arrived Home at: {{ states.device_tracker.deans_iphone.last_changed }}
          {% elif states('device_tracker.deans_iphone') == 'not_home' %}
            Left Home at: {{ states.device_tracker.deans_iphone.last_changed }}
          {% else %}
            Unknown
          {% endif %}
        icon_template: >
          {% if states('device_tracker.deans_iphone') == 'home' %}
            mdi:home-import-outline
          {% elif states('device_tracker.deans_iphone') == 'not_home' %}
            mdi:home-export-outline
          {% else %}
            mdi:help-circle
          {% endif %}

Now i will try and format the time as it shows as this:

Left Home at: 2020-02-17 10:29:30.251506+00:00

2 Likes

I have formatting the time how i like by using:

{{ as_timestamp(states.device_tracker.deans_iphone.last_changed) | timestamp_custom(‘%H:%M - %d/%m/%Y’) }}

3 Likes

Great sensor working as a charm, but can I ask you where I have to put the line for formatting the time? Thank you

{{ as_timestamp(states.device_tracker.deans_iphone.last_changed) | timestamp_custom(‘%H:%M - %d/%m/%Y’) }}