I find it extremely annoying that, upon restarting HASS, last_updated and last_changed get set to the current date/time, even in components that use the new state restoration feature. I use last_updated in a new React Native application that I am developing for the HASS community.
I would like to go about possibly fixing this and sharing my changes on Github, however I am not entirely sure how to start. I know that with restore_state we get the previous state object which has last_updated and last_changed. How would I go about setting those properties for an entity?
I could probably figure this out with trial and error (I’m not too experienced in the HASS system nor in Python), however I figured that I might as well ask the development community first! Can somebody help me out with this issue I’m having?
I want to use those values for automation/front end so I think I’ll need to write a script to get them from the api and return them to a command line sensor. If I do that, I think that command line sensor would retain its value on restart which might help your needs.
I ended up not moving forward with the app, but did figure out how to fix this problem for myself and my own personal use. Maybe this will be helpful to anyone that finds this post:
Essentially, I wrote a small AppDaemon “app” that listens to HASS “EVENT_STATE_CHANGED” events and stores the “new_state” object, that is sent with each event, in a Redis database under a key which is just the entity_id. The “app” also publishes a message (using Redis pub/sub) with the same “new_state” object.
I then wrote a simple little Node server that exposes an endpoint that returns data (pulled from Redis) in an identical manner to the HASS api endpoint “/api/states”. It also listens for Redis pub/sub messages, and posts those to an EventSource stream that it creates upon load. The stream exposes data in an identical way to the HASS endpoint “/api/stream”.
With this solution, I am essentially just maintaining my own copy of the HASS state machine and exposing two endpoints that mimic the two HASS api endpoints that I use information from, and it works perfectly.
I would like to see this restored upon startup as well. We have the history, not sure why it can’t be read into the state machine when HA is restarted.
I would like to add my support for the history timestamp to be restored upon a HASS restart. Because of other issues with serial devices just bombing occasionally, I have my HASS instance scheduled to restart every morning to prevent complications with not being able to communicate with those serial devices.
This makes the last_updated dates always off and makes some functionality in a React app I’m writing useless.
I know this is a very old thread, but I would love to see this implemented. I’ve been building out a security use case and while there are workarounds to not having the state accurately recorded for a device, they are very manual and a bit tedious.
This would be pretty useful, it annoys me how it’s currently setup. It doesn’t make sense to me as an end-user to see all my last-changed states refreshed when I restart HA. I want to see the last time it properly changed state not just became unavailable.
As can be read, the change will only be considered when a Python expert prepares the necessary code for this. Since that won’t be me (programming skills near absent), the issue is closed.
So there is no hope of this being implemented soon? I think I will try it with another user’s idea of making a datetime entity but I dont really want to clog up my entities with these. Maybe it can be a custom attribute…