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
tom_l
February 17, 2020, 9:36am
2
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.
tom_l
February 17, 2020, 9:50am
4
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:
tom_l
February 17, 2020, 10:10am
6
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:
After seeing many threads on the forum asking for help with time conversions and calculations using time and realizing I generally had no real understanding of how time worked in HA I decided to dig in and try to figure it out.
After over a week of internet and forum searches I think I’ve now got a lot of it figured out. I’m sure there is more that I don’t have here because sometimes “you don’t know what you don’t know”.
So, since I didn’t know this stuff and because of the number of other pos…
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’) }}