As you’ve discovered last_changed and last_updated aren’t persistent (survive a restart) but are re-initialized after every restart.
Given the manner in which most people want to use those two properties, re-initialization throws a wrench into their plans. The workaround is to ‘persist’ them elsewhere as per the link tom_I posted.
Thanks. Yeah, that guy often barely can relate to use cases except his own.
So at this moment the choice is either to switch to MySQL and use the script or use input_text’s and if possible (guess not) write those saved timestamps back after restart.
I store these timestamps in an input_datetime for this purpose - and use them straight from there for automations, etc.
As you can see below, I have a binary_sensor for the door and an input_datetime with the same name + “_last_opened” - this allows me to use a relatively simple template for all sensors of the same kind.
- alias: Last Door Opened Date and Time
trigger:
platform: state
entity_id: binary_sensor.front_door, binary_sensor.back_door, binary_sensor.balcony_door, binary_sensor.downstairs_door, binary_sensor.living_room_motion
from: 'off'
to: 'on'
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.{{ trigger.entity_id[14:] }}_last_opened
time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
Hi, this looks like what I’m searching for but not sure how to use it.
I have replaced the enties to my own and the automation is triggered.
I have checked developer page and tried to find last_opened data but can’t see any data. How do I present the last_opened for the entity?
I tried {{ states.binary_sensor['maindoor'].last_opened }}
I’m also curious what [14:] stands for?
Thanks
For future reference, an entity_id is composed of a domain and an object_id. Therefore binary_sensor is the domain and front_door is the object_id. We can use that information to modify the template so that it accesses the object_id directly instead of slicing it from the entity_id:
The solution chairstacker has presented relies on the fact that for every binary_sensor you have, you must also manually define an associated input_datetime entity.
@fabs Any chance you could post or point me to a complete example of using the custom component saver to save the last_changed or last_updated attributes? I haven’t been able to get that working. Thanks.
As an example, I’m trying to save the state of a binary_sensor from my alarm panel and restore both the state and the last_modified and last_updated times. After a restart, the sensor is “unknown” until it’s toggled and the times are the restart times. But there is no service call to change the state of the sensor so how does the restore action of the saver component work? I started writing this and got stuck with how to implement the restore portion. I also looked through the saver code and don’t see anywhere that it would actually be able to update the last_modified and last_updated times of a state in the state machine.
What I do is: everytime my entitiy changes, I save the state like your example. In my case with an automation. This can also be done with a template sensor.
What I do to restore my entity data is running an automation on HA start with the trigger ‘Home Assistant start’ as part of a some other inititialization steps: