Need help with automation template date diff

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?


I’m not sure why that was necessary given that entities have last_changed and last_updated properties.

If your temperature sensor is called sensor.lennox_2_temp_sensor paste this into the Template Editor and examine the results:

{{ states.sensor.lennox_2_temp_sensor.last_changed }}

{{ states.sensor.lennox_2_temp_sensor.last_updated }}

{{ now() - states.sensor.lennox_2_temp_sensor.last_changed }}

{{ (now() - states.sensor.lennox_2_temp_sensor.last_changed).seconds > 3600 }}

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.

  - platform: template
        value_template: >
          {{ (now() - states.sensor.lennox_2_temp_sensor.last_updated).seconds > 3600 }}

The Template Binary Sensor can trigger an Alert sensor which can notify you repeatedly.

Alternatively, you can use that template directly in an automation as a Template Trigger.

- alias: example 123
  - platform: template
    value_template: >
      {{ (now() - states.sensor.lennox_2_temp_sensor.last_updated).seconds > 3600 }}
  - service: notify.notify
  .. etc ..

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.

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.

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…

I’m not sure why that was necessary given that entities have last_changed and last_updated properties.

I had no idea I could do that. I even posted a few months ago asking if there was a way to get at that value How can I read the "Last Seen" value of Zigbee device?

Thanks for helping me grab the value and create the template. I’m using it directly in the trigger, like this:

platform: template
value_template: "{{ (((now() - states.sensor.lumi_lumi_sens_57884f03_temperature.last_updated).seconds / 60) | round(0, 'floor')) > 30 }}"

When it triggers, I’m having a push notification sent to me. Thanks for your help!

EDIT: and I had no idea about the Template dev tool. It’s so helpful!

1 Like