But delaying automations more than a few seconds is always a risk that it will fail due to automation reload or restart of HA.
That is why the recommended way is to use a datetime.
Can you use a date/time for 1hour in the future as opposed to a specific time
Yes, look at what Christoph wrote.
Hi, before I did this:
{{ ( as_timestamp(now()) - as_timestamp(state_attr(‘automation.name’, ‘last_triggered’)) |int(0) ) > 300 }}
Now templates have changed and doesn’t work anymore, anyone knows how to fix?
{{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id,'last_triggered'))| int(0) > 300 }}
Yours looks just fine except for the type of quotes you’re using. Plus you can use ‘this’ so you don’t need to hardcode the name of the automation.
Yet another way to do the same thing:
{{ now() - state_attr('automation.name', 'last_triggered') > timedelta(minutes=5) }}
The values of now()
and last_triggered
are datetime objects.
When you subtract datetime objects, the result is a timedelta object. The template simply subtracts the two datetime objects and compares the result to a timedelta object of 5 minutes.
If the template is within the automation it is testing then you can use calisro’s tip and replace automation.name
with this.entity_id
.
{{ now() - state_attr(this.entity_id, 'last_triggered') > timedelta(minutes=5) }}
EDIT
Correction. Removed quotes wrapping this.entity_id
which mistakenly changed its meaning from a variable to a string.
Your template above was creating an error for me!
@calisro’s tip references this.entity_id
not 'this.entity_id'
…
{{ now() - state_attr(this.entity_id, 'last_triggered') > timedelta(minutes=5) }}
which works for me.
Yes it shouldn’t have quotes.
Thank you! I have corrected the mistake.
Hi! I’ve encountered a similar issue and was wondering if this would work with automation IDs? I have some pressure mats I’ve connected to HA via ESP that when detected, open op a doggy door (there are mats on either side of the door, grouped together in HA). However I want to avoid a double activation when the dog steps on both when he goes in or out, so I figured just setting a condition of “don’t activate if the automation was ran within 15 seconds prior.” I figured setting up a custom sensor would do the job.
Using the above provided code I’m getting an error, which I assume is only due to the automation ID. Any help would be appreciated!
{{ now() - state_attr(automation.open_doggy_door_with_mat, 'last_triggered') > timedelta(seconds=15) }}
The quotes are missing around the automation entity:
{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}
or:
{{ now() - state_attr(this.entity_id, 'last_triggered') > timedelta(seconds=15) }}
Thank you! Unfortunately I’m still getting an error for the date.time.
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'
Was the doggy automation ever triggered before?
It has been triggering throughout the day, dog goes in and out constantly.
Wait, are you talking about a automation condition or a template sensor? And what piece of code are you using where?
Yes, it would be a template sensor, that then would be used as an automation condition.
I’m still very new at this so please forgive my errors, but I believe it would look along the lines of
- platform: template
sensors:
doggy_door_pressure_mat_last_trigger:
friendly_name: Doggy Mat Last Trigger
value_template: >-
{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}
icon_template: mdi:timer-alert
The indentation is not correct, it has to be 2 tabs:
- platform: template
sensors:
doggy_door_pressure_mat_last_trigger:
friendly_name: Doggy Mat Last Trigger
value_template: >-
{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}
icon_template: mdi:timer-alert
Thank you. I’ve made the correction to what you had. However I’m still getting an “unavailable” for the State. And looking at the template editor I’m getting an error.
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'
If you are creating new Template Sensors you should consider using the modern format of configuration as opposed to the example you posted which is in legacy format.
template:
- sensor:
- name: Doggy Mat Last Trigger
state: "{{ now() - state_attr('automation.open_doggy_door_with_mat', 'last_triggered') > timedelta(seconds=15) }}"
icon: mdi:timer-alert