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: