My gut reaction was along the same lines as @finity’s suggestion. However, I’d use an input_number and have an automation that triggers when the person leaves (i.e., the device_tracker changes from: 'home'
) and stores {{ as_timestamp(now()) }}
to the input_number (because input_datetime’s, IMHO, have a fatal flaw in the way you set them – you can’t set both date & time in one template, so there’s always the possibility that the date gets set right before midnight, and the time gets set right after midnight.
) Then when the person comes home you can use {{ states('input_number.xxx_left_home')|float|timestamp_local }}
, for example, in your message.
But this whole technique has its own flaw. I.e., if the person leaves while HA is down, then the saved value would be wrong at best.
Probably a better way is to query the recorder database. I’ve been wanting an excuse to try that via the SQL Sensor. This presented a fun exercise!
Anyway, I came up with the following:
sensor:
- platform: sql
db_url: !secret db_url
queries:
- name: Xyz left home last
query: "SELECT last_changed FROM states WHERE entity_id = 'device_tracker.xyz' and state_id > (SELECT state_id FROM states WHERE entity_id = 'device_tracker.xyz' and state = 'home' and state_id < (SELECT state_id FROM states WHERE entity_id = 'device_tracker.xyz' and state != 'home' ORDER BY state_id DESC LIMIT 1) ORDER BY state_id DESC LIMIT 1) ORDER BY state_id LIMIT 1;"
column: last_changed
This is really three queries in one. First it searches backwards looking for the most recent state that isn’t ‘home’. (This represents the time right before the person came home.) Then it looks backwards from there for the first state that is ‘home’. (That represents the last time the person was last home.) Lastly it gets the next state after that, which is the first state that is not ‘home’ after the last time the person was ‘home’, which contains the time the person last left home before coming home. ![:slight_smile: :slight_smile:](https://community.home-assistant.io/images/emoji/twitter/slight_smile.png?v=9)
Of course this also isn’t perfect and wouldn’t work if the times being queried for aren’t in the database (e.g., person last left home longer ago than the db purge period.) But, I would think for most cases it might work well. Also, unfortunately the query can’t be calculated dynamically via a template; it has to be fully hard-coded. So, if you want to do this for multiple people, you’d have to have one per person.
I tried it on myself, and sure enough, it found the time I left home this morning before getting back this afternoon.