I discovered earlier today that one of my Aqara sensors hadn’t updated for six hours - so had to refresh it manually. To keep track of any lost connections, I’d like binary sensors showing my the timestamp for last sensor update.
I’ve followed a couple of guides, but none seem to work. My sensor only displays “off”.
This gives me the time in seconds. Tested under developertools -> templates.
{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.motion_sensor_hallway.last_changed)) }}
It’s future-proof (0.115 will bring some changes) and updates every minute (as_timestamp(now()) also doesn’t update the sensor regularly btw). Additionally you need to add the following to your sensor part:
Had to add “platform: template” and “sensors” to your template sensor, but it works. Thank you!
This only shows time of last update, right and not doing any refreshing of the sensor itself? Just wondering with regards to sensor battery.
It only works inside an entities card. It will show '5 minutes ago` if the timestamp was 5 minutes ago. So the sensor barely updates but the UI will constantly look like it’s updating.
ahh, so I can’t use it for automations, for example?
Just so I understand your UI/update comment; the template sensor will just update with whatever timestamps it gets when the sensor updates, but won’t do any active refreshing of the humidity sensor?
Boy, just when I think I’m starting to understand Home Assistant
(top is Petro’s second suggestion, bottom is my second)
Did you wait a few minutes to check whether the value changes?
Addition:
If you don’t want to create a separate sensor for every single device you want to observe, you could also create such a binary sensor as a base for notifications (5 hours inactive --> on):
then put all above mentioned sensors in an entities card with secondary-info: last-changed, so you can check which one is not reporting. I’m pretty sure there are simpler ways to achieve this - but it works
It’s not a timestamp, so device_class: timestamp should be removed. Also, an incoming change in 0.115 makes the entity_id field no longer needed. It’s best if you remove it now.
If you use an external DB - it’s much easier to create an sql server. Hidden benefit is that you can check last update even after restart of HA. This is very important for me for some kind of events like motion detection and etc.
I know that this post is pretty old by now, however, I wanted to share a slightly cleaner template. This allows you to pass a threshold in hours for any of the sensors.