Automation. Trigger if sensor NOT updated AFTER input_boolean. Surely there's a better way than this!

I have a really simple requirement so I probably can’t see the woods for the trees…

When an input_boolean changes state I need to be notified if a sensor doesn’t also change state after, but within, a very small (almost zero) number of seconds.

I have this which I can’t believe is the best way to achieve it (and to be honest I haven’t tested it anyway so it might not even work!)

What am I missing to make this better?

  - alias: Warning
    trigger: 
      - platform: state
        entity_id:
          - input_boolean.some_boolean
          - input_boolean.some_other_boolean

    action:
      - wait_template: >
          {{ as_timestamp(states.sensor.my_sensor.last_changed) - 
             as_timestamp(trigger.now) < 10 }}
        timeout:
          seconds: 12

      - condition: template >
          {{ as_timestamp(states.sensor.my_sensor.last_changed) - 
             as_timestamp(trigger.now) > 10 }}

      - service: notify.blah_blah

I believe this won’t work as the entity’s state won’t change.
You’ll probably need to track the entity’s last_change value in a template sensor for this to work.

I think this should work… but you need to change the conditions you are testing for the “wait_template” and the “condition”… The timestamp now() will always be bigger than your sensor last change so the condition “< 10” will always be met (and therefore trigger) as the number (timestamp last changed - timestamp now) will be negative (always)… For the same reason, the “condition” will never be met as the result is negative again and will never be bigger than 10…
So for me it should be:

    trigger: 
      - platform: state
        entity_id:
          - input_boolean.some_boolean
          - input_boolean.some_other_boolean

    action:
      - wait_template: >
          {{ as_timestamp(now) -
             as_timestamp(states.sensor.my_sensor.last_changed) < 10 }}
        timeout:
          seconds: 12

      - condition: template >
          {{ as_timestamp(now) -
             as_timestamp(states.sensor.my_sensor.last_changed) > 10 }}

      - service: notify --> sensor doesn't change state on time

I guess I’m not sure what you don’t find OK with your solution?

It’s a trigger and three lines of code in the action section. I’m not sure it could be much simpler.

You do have to fix the templates as noted above (swapping now & last changed) and you left out the “value_template” section in your condition:

    action:
      - wait_template: "{{ as_timestamp(now()) - as_timestamp(states.sensor.my_sensor.last_changed) < 10 }}"
        timeout:
          seconds: 12
      - condition: template 
        value_template: "{{ as_timestamp(now()) - as_timestamp(states.sensor.my_sensor.last_changed) > 10 }}"
      - service: notify.blah_blah

Thanks everyone.

Well… It just seemed a bit er, clunky. But clearly at least three people don’t seem to think it is that bad :slight_smile:

That was a copy and paste error :wink:

And yes, you are all right about the time templates.

I completely misread the docs to say that trigger.now returned the time the automation was triggered. Which would be true if I was using a time to trigger it!!

Thanks again.