I’ve been using the solution posted @TheFes for a few days successfully (thank you!), however I’ve noticed the following error message in Studio Code Server:
I believe this has only started showing since updating to 2023.5.3 and Studio Code Server 5.6.0
That line is how you get the data out of the template sensor again.
You can use it where you want, but if course that place needs to allow template sensors
Can I give this topic another nudge? I have a suggestion that maybe makes this more likely to be implemented:
Add two fields to the states table in the database:
last_communicated_state:
This will normally just reflect the same value that is in state. However:
It can only be unknown upon sensor creation if the sensor has a state of unknown or unavailable.
Once the sensor has any state besides unknown or unavailable, this value will reflect that state.
If the sensor state ever reverts to unknown or unavailable this value will not reflect that. It will not be changed and therefore retain the previous value.
last_communicated_state_change_ts
This will be a timestamp which changes any time the last_communicated_state has its value changed.
Having the last communicated state in the database prevents having to do lookups when a sensor gets a new state and needs to determine whether this is a “real” change or not. It is also useful in scenarios where users want to ignore unknown/unavailable and just use the last known “real” value. Also it makes it clear that there is no request to change the functionality of the existing last_changed_ts and last_updated_ts fields.
The downside is that I have no idea how much extra storage this would require adding to the database.
Also, in regards to naming of these two fields, I don’t think it matters what it is called. Here’s some other ideas for people to chew on:
I can’t believe it, my prayers have been answered and I can finally get rid of my DIY solution! Great news!
Got my hopes up, I should have checked the link! I’m still hoping this is implemented at some point - I still have the same stance that it’s the expected behaviour from your standard HA users view.
Saying that my motion sensors and door/window sensors last changed 1 minute ago because I just restarted Home Assistant is wrong 99.9% of the time.
Persisting the previous “last changed” time would be wrong 0.1% of the time.
This is not a good argument. The current behavior makes no sense, especially considering the extreme frequency of Home Assistant updates that all require restarts. This is a constant annoyance.