How to calculate a time difference on automation

I’d like to put a condition in the automation that says: if last execution of the automation was less than 5 sec… ignore.

I have found the attribute last_triggered and then now but I’m not able to continue

"{{ (state_attr('automation.0_a', 'last_triggered') - now())  }}"

how I say less than 5 sec? also it should refer to previous run

{{ now() - state_attr('automation.alarm_triggered_reset','last_triggered') }}
{{ now() - state_attr('automation.alarm_triggered_reset','last_triggered') 
    < timedelta(seconds=5) }}

For short times in automations running in single mode there is a much easier way. Just add this to the end of your actions:

- delay: 5

This will cause the automation to run for 5 seconds. And as only one instance can run at a time it will only trigger at most every 5 seconds.

It will generate an “already running” warning in your logs if re-triggered within that 5 seconds. If you don’t want this you can add:

mode: single
max_exceeded: silent
1 Like

I very much appreciate your knowledge and teaching.

In this, I do not see why @jazzyisj solution, which does not cause errors that need to be suppressed is not not a better answer. Thanks for your input. Maybe it is just semantics. However, as I write scripts and finally get them ‘working’, then come back to the many months later, I need flows that my old brain can ‘restart’ :wink:

If you are only trying to “throttle” the automation for a few seconds so it doesn’t trigger multiple times in a short period (for example if you have many triggers for one automation), Tom’s suggestion is simpler and actually prevents the automation from triggering at all. HASS doesn’t have to evaluate a template to figure out if the automation should trigger or not.

I really wasn’t sure if you were trying to do that or create a condition within the script to only run part of it if was recently triggered, and well, my answer directly answered your question.

But if you are just trying to stop multiple triggers for a small period of time, try the delay method. It works quite well.

And the max_exceeded: silent suppressions aren’t errors - just notifications (warnings) that let you know it tried to run again but was already running. Harmless and it just gives you a way to not see them in the logs.

The automation I’m using is based on this great blueprint: Link Multiple Switches - Blueprints Exchange - Home Assistant Community ( or Link On/Off State of Multiple Devices - Blueprints Exchange - Home Assistant Community (
In it the blueprint the automation is defined as:

mode: queued
max_exceeded: silent

That’s why I was thinking to work on the time difference.
The infinite flip/flop loop happens on a group of 8 switches connected. Sometimes (not always), randomly, they start to go on/off continuously even if the conditions are:

- condition: template
  value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'
- condition: template
  value_template: '{{ trigger.to_state.state != "unknown" }}'
- condition: template
  value_template: '{{ trigger.to_state.state != "unavailable" }}'
- condition: template
  value_template: '{{trigger.to_state.context.parent_id is none or ( != and trigger.to_state.context.parent_id != }}'

the only way to stop the flip/flop is to disable the automation. Wait a little and re-enable… but this is not feasible when it happens to the kids and I’m not home :frowning: