Last changed/Last updated

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?

Thanks for reading!

1 Like

It’s not possible. Last updated and last changed are maintained by the state machine and not the entity.

Also, restarting Home Assistant is not a common thing so I wouldn’t worry about it

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.

Hi, inspired by this topic i made a python script for getting the real last_changed timestamp.
Maybe someone can use it.

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.

Completely agree. Seems like an excellent feature to have
@rhodges, have you looked into how difficult this would be to implement?

Not yet. I was thinking of playing with AppDaemon first and see what I can do there.

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.

1 Like

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.

It been 2 year, we really hope this being implemented.
This just need to be loaded from DB when HASS started

I believe we already retain state after reboot why cant we get last_changed stat retained ?

Based on this, it is possible right ?

yes that would be nice indeed, and much wanted… Any news on this yet?

I back this also, i am constantly adding things to home assistant and restart it every few days so when everything is reset its annoying.

+1 here yes

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.

1 Like

I am also looking forward to this feature!