Automation triggered on time difference

Hi All,

I am trying to build an automation, which I am sure is possible, but even with the help of this forum and the documentation don’t know how to do it.

I have a sensor entity, which has a time in it’s state (HH:mm:ss format), and a input_datetime, which also has a time in it’s state in the same format.

What I want, is that an automation gets triggered, when the sensor’s time, and the input_datetime’s time difference becomes more than 5 minutes. It doesn’t matter if it’s 5 minutes later or earlier, I want this automation to run either way.

If anyone knows how to do this please help me out!

trigger:
- platform: template
  value_template: >
    {% set sensor = today_at(states('sensor.abc')) %}
    {% set datetime = today_at(states('input_datetime.xyz')) %}
    {{ (sensor - datetime).seconds / 60 > 5 }}

Thanks! You’re the best, you helped me out so many times. :slight_smile: it works perfectly!

1 Like