Last Changed did not update properly, how to fix?

Hello,

I have setup my last_changed in my HA config, here is my home:

And the code:

But it doesn’t work all the time, sometimes working, sometimes not, any way to debug or check if last_changed was written properly to the database by Home Assistant?

Thank you.

Use any sql client and look at the states table.

You will see that these rows are updated on every restart of HA.

A while ago i had the same problem and created a python shell script.

It fetches the last few states from an entity and returns the last_changed value if the state realy changed.

Thanks @VDRainer

I checked the db, data is there, but it seems HA can’t query it, I ran the query manually inside sqlite3 and I am able to see the last_changed value.

sqlite> select last_changed from states where entity_id = 'binary_sensor.0x00158d000273bb1d_contact' and state = 'on' order by last_changed desc;
2020-05-14 17:55:50.469958
2020-05-14 17:55:50.469958
2020-05-14 16:27:23.455198
2020-05-14 16:27:23.455198
2020-05-14 16:15:27.194371
2020-05-14 16:15:27.194371
2020-05-14 16:14:58.551793
2020-05-14 16:14:58.551793
sqlite>

This is one example.

I need to restart HA all the time to see last_changed updated on lovelace :frowning:

I think I will need to subscribe to zigbee2mqtt directly, post the notification via firebase and update my own app, for the best update/stability