Automation help: Don't run if run in the last 5 minutes

I’m having trouble determining why an automation is constantly saying this condition is true. I don’t want it to run if its been run in the last five minutes. Since last_triggered gets updated immediately, I also check to see if its been run in the last 5 seconds.

However, no matter what I do, it seems to evaluate as true. What am I missing?

alias: New automation test
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.test
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          {{ (now() - state_attr('automation.new_automation_test',
          'last_triggered') > timedelta(minutes = 5)) 
            or (now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)) }}
    then:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: run
    else:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: ignore
mode: single

doesn’t this answer your own question. causing this:

(now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)

to be true?

if you’re not sure, try this to illuminate what’s going on:

alias: New automation test
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.test
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          {{ (now() - state_attr('automation.new_automation_test',
          'last_triggered') > timedelta(minutes = 5)) 
            or (now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)) }}
    then:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: |
            run
            last triggered {{ state_attr('automation.new_automation_test', 'last_triggered') }}
             timedelta {{  now() - state_attr('automation.new_automation_test','last_triggered') }}
            condition1  {{ (now() - state_attr('automation.new_automation_test', 'last_triggered') > timedelta(minutes = 5)) }}
            condition2  {{ (now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)) }}
    else:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: |
            ignore
            last triggered {{ state_attr('automation.new_automation_test', 'last_triggered') }}
             timedelta {{  now() - state_attr('automation.new_automation_test','last_triggered') }}
            condition1  {{ (now() - state_attr('automation.new_automation_test', 'last_triggered') > timedelta(minutes = 5)) }}
            condition2  {{ (now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)) }}
mode: single

but to actually solve your original in tent… if you dont’ want the automation to run for 5 minutes ,you could do this:

alias: New automation test
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.test
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          {{ (now() - state_attr('automation.new_automation_test',
          'last_triggered') > timedelta(minutes = 5)) 
            or (now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)) }}
    then:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: >
            run

            last triggered {{ state_attr('automation.new_automation_test',
            'last_triggered') }}
             timedelta {{  now() - state_attr('automation.new_automation_test','last_triggered') }}
            condition1  {{ (now() - state_attr('automation.new_automation_test',
            'last_triggered') > timedelta(minutes = 5)) }}

            condition2  {{ (now() - state_attr('automation.new_automation_test',
            'last_triggered') < timedelta(seconds = 5)) }}
    else:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: >
            ignore

            last triggered {{ state_attr('automation.new_automation_test',
            'last_triggered') }}
             timedelta {{  now() - state_attr('automation.new_automation_test','last_triggered') }}
            condition1  {{ (now() - state_attr('automation.new_automation_test',
            'last_triggered') > timedelta(minutes = 5)) }}

            condition2  {{ (now() - state_attr('automation.new_automation_test',
            'last_triggered') < timedelta(seconds = 5)) }}
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
mode: single

i put a 5 minute delay at the end… combined with mode:single, this will prevent it from running for 5 minutes. note that it’ll be 5 minutes plus a touch… for whatever code to execute before it gets to that delay. if that’s important, it’s solvable too…

I simplified the example for help, but I can’t use a time delay and Single mode, as there are other parts of the automation that I still want to run.

As an example, when I get home, I have an automation that does a bunch of stuff, but I don’t want it to send a notification if it ran in the last five minutes - but I still want it to do all the other stuff.

as i recall last_triggered is set only after it passes condition (i’m not currently in a position to test it, but i recall this being true). so try someting like this?

note that in this case, it will not get to action if it doesn’t match. so you will get a notification if pass or no notifiation at all. should not get to the “ignore” notification.

would this work for your use case?

alias: New automation test
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.test
condition:
  - condition: template
    value_template: >
      {{ (now() - state_attr('automation.new_automation_test', 'last_triggered')
      > timedelta(minutes = 5)) }}
action:
  - if:
      - condition: template
        value_template: >-
          {{ (now() - state_attr('automation.new_automation_test',
          'last_triggered') > timedelta(minutes = 5)) 
            or (now() - state_attr('automation.new_automation_test', 'last_triggered') < timedelta(seconds = 5)) }}
    then:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: run
    else:
      - service: notify.laptop_zac
        metadata: {}
        data:
          message: ignore
mode: single

Hmmm, okay, that’s interesting. I’ll test that.

I always use the following:

{{ now() > this.attributes.last_triggered | default(as_datetime(0), 1) + timedelta(minutes = 5) }}

You need to use the self referencing variable in your template condition if it is in the Action block but it also works in the Condition block.

3 Likes

ah right! that has a snapshot of what it was coming into the automation. thanks @Didgeridrew

Ahhhh! Interesting. Thanks!

Does this also work in the in the initial Conditions at the top?

Yes it works in both the Condition and Action blocks.