This works except I want this date/time to survive a reboot. Currently the date and time is updated after each HA restart. Is this possible and how do I revise this template to capture an actual ON event rather than an update_event which occurs at each HA restart?
`
The issue is that the binary_sensorâs attribute is last_updated which, as youâve discovered, isnât the same as last_triggered.
Given that last_updated will refresh after Home Assistant restarts, I canât see how a template can get around it. The template can only work with available data and a binary_sensor doesnât have a last_triggered attribute.
I canât think of an elegant way to do it, short of making an automation that triggers on any binary_sensorâs state-change and then stores the trigger-time ⊠somewhere.
EDIT
Seems to me this might be a worthwhile Feature Request. last_triggered is a useful bit of information for binary_sensors used to detect opening/closing of doors, windows, etc.
Yep, like I said, somewhere, meaning a whole pile of additional infrastructure (in this example, automation and a custom component) to save an attribute that, arguably, ought to be part of binary_sensor.
FWIW, in the home automation software Iâve used for over a decade, âLastTimeTriggeredâ is a standard property (attribute) of a DoorSensor (binary_sensor).
After reading VDRainerâs post (above), obviously my âdiscoveryâ was something already well known, namely last_changed = last_updated after a restart. Clearly, you have to store the attribute somewhere (to survive the restart) and VDRainerâs solution is yet another way to do it (i.e. with a python script into a database).
Not sure itâs worthwhile to add a Feature Request because this is just one small part of the much larger issue of Home Assistant lacking a system-wide persistence feature. FWIW, I used openHAB for a few months and it has persistence that, in its most basic form, can store and restore entity states. My long-used home automation software (Premise) can do this as well. Without it, youâre obliged to improvise with a custom persistence solution (as demonstrated by the two examples above).
Though I agree we would need the persistence feature natively in HA, I am not sure I understand what you mean in your first reply indicating the difference between last_updated and last_triggered. I think in HA language triggered is only applicable for automations, and last_updated or _changed would be identical in the case of binary_sensors?
Youâre right, in Home Assistant the word âtriggerâ is associated with automations. So when I used the term last_triggered for a binary_sensor, that was an incorrect use of Home Assistantâs terminology. I shouldâve used last_changed when referring to the time of a binary_sensorâs last state-change.
FWIW, I have years of experience with Premise and sometimes its terminology seeps into the words I use to describe Home Assistantâs operations. As you can see in the screenshot above, in Premise the term is LastTimeTriggered ⊠and that stuck in my mind when I wrote about Home Assistantâs binary_sensor.