How to find out if a motion sensor or a door sensor is 'dead'?

how to find out if a motion sensor or a door sensor is ‘dead’ or totally out of battery ?

I found out that I could monitor when was the last_updated via something like :

value_template: '{{ ( as_timestamp(now()) - as_timestamp(state.attributes.last_updated | default(0) ) ) }}'
above: '25200'

This (I think) would tell me if there’s no update for a week.

I’ve read on State objects - Home Assistant

state.last_updated : Time the state was written to the state machine. Note that writing the exact same state including attributes will not result in this field being updated.

Problem : maybe this motion sensor or door sensor hasn’t seen anyone for a week, in this case it would alert (incorrectly!)
Also, if I restart HA, it would ‘reset’ the 7 days, as the last_updated would be the HA startup time.

Is there another way to find out about dead motion/door sensors ?

I’m using the Xiaomi Aqara ones, if that makes any difference.

Update to the latest version of hass. It has a built-in detection of when was the last time xiaomi sensors reported. If certain amount has passed without any reports the status changes to “unavailable”, so you just need to check whether a sensor has been “unavailable” for X seconds.

Here’s the relevant pull:


Thank you, amazing answer !
I’ll take one of my sensors out of the house in order to test that.

It would be nice to get that new feature listed on

I tried to put the door sensor away, the status changed to ‘unavailable’ after around 2h 15mn

So it’s all good, I just need to add an automation to alert on it.