I have a fickle zigbee temp sensor that sometimes stops responding. I’d like to create an automation that’ll alert me when the sensor hasn’t reported a temp change for over an hour.
To make this work, I created a custom sensor using the SQL platform. The custom sensor queries the states table to get the most recent last_changed value for my zigbee temp sensor. The custom sensor works great and I’m able to see the state change every time the temp sensor reports in.
The part I’m struggling with is creating an automation that can do a date difference and alert me when the current time is greater than 60 minutes from the date in the SQL sensor’s state.
I understand I’ll need to use a template but I just don’t get how they work. I’d like to stay with the UI as much as possible and not dive into the yaml files.
Is there a solution where I can use the automation UI to let me know when the date in the sensors state is over 60 minutes old?
That last line can be used in a Template Binary Sensor. It would indicate on when the elapsed time since the last state-change exceeds 1 hour (3600 seconds).
BTW, the difference between last_changed and last_updated is explained here. For your application, you may want to use last_updated.
Not sure what zigbee platform you are using, as the answer to your question most likely different for say ZHA vs. zigbee2mqtt. If you are using zigbee2mqtt, the answer is pretty easy I believe as you can add the :
expire_after integer (optional, default: 0)
Defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable.
https://www.home-assistant.io/integrations/sensor.mqtt/
to the mqtt sensor and use automation to raise a signal when this occurs.
For ZHA sensors, I think you have the ‘last updated’ attribute for the entity, you will, unfortunately, need to create another sensor, a template sensor from the ZHA sensor’s ‘last updated’ time minus the current time to get a updated time delta, set the template sensors availability_template based on time delta and then again have a automation raise a signal.
availability_template template (optional, default: true)
Defines a template to get the available state of the component. If the template returns true, the device is available. If the template returns any other value, the device will be unavailable. If availability_template is not configured, the component will always be available.
https://www.home-assistant.io/integrations/template/
I think you can avoid the SQL query sensor, as this is probably a heavier load on HA than is needed, as the ‘last updated’ attribute is available for most entity states without additional work.
This really should be more consistent among state entities, but for now a bit of yaml work probably…