Generic way to display sensor last updated time

I have a bunch of various sensors, some MQTT smartthings, some from a company called wireless tags that come in via the HTTP sensor component, and a few other things. I am looking to keep a display of the last time each sensor was updated. I have tried something like what is below and keep getting errors. Obviously HA stores a date/time with each sensor read regardless of the sensor type since I can click on each and see a time graph of values. How do I get the last date/time updated so I can display it?

  • platform: template
    sensors:
    garage_time:
    value_template: ‘{{relative_time(states.sensor.Garage_Temperature.attributes.last_updated)}}’
    entity_id: sensor.Garage_Temperature

2018-08-19 13:37:49 ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template garage_time: UndefinedError: ‘mappingproxy object’ has no attribute ‘last_updated’

1 Like

last_updated is not under attributes. So, at the very least, you should use states.sensor.Garage_Temperature.last_updated. Note that last_updated is the last time either the state or any attributes changed. If you want the time the actual state was last changed, then use last_changed instead of last_updated.

Having said that, note that these times don’t go past the last time HA was restarted. So, if the entities last changed during a previous run, last_updated and last_changed won’t show that. For that you’d have to get history from the database, which is what the data shown in the UI comes from. And if so, I can’t help with that since I don’t know how to do that. :wink:

4 Likes

That works, thought I tried that but obviously I didn’t tyvm!!

1 Like