I’m tearing my hair out - there is an error in this automation condition and I can’t figure out what it is!
If I comment it out then the automation runs, as it does if I manually trigger it, but it does not run the actions when this is uncommented.
The intent is to only run the actions once a minute.
condition:
# only notify once every 1 minutes at most
condition: template
value_template: “{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.alarm_shed','last_triggered')) |int(0) ) > 60 }}"
It also seems correct in the template editor in Developer Tools, resolving to True (or False if it had triggered in the last minute).
It is probably something simple that I’m looking over so any help gratefully received. Thanks.
When automation.alarm_shed is triggered, that’s when its last_triggered attribute is updated (set to now). The condition wantslast_triggered to be more than 60 seconds ago but it just got updated a fraction a second ago when the automation was triggered.
In other words, the condition demands something that cannot ever be fulfilled.
EDIT
Nope, I was wrong.
Should’ve waited until after breakfast before answering …
I just carried out a simple test that disproves what I stated above. Here’s an automation that simply reports (via a persistent_notification) the timestamps for now() and last_triggered.
- alias: 'last triggered test'
trigger:
platform: state
entity_id: input_boolean.toggler
to: 'on'
action:
service: persistent_notification.create
data_template:
message: 'Now: {{ now().timestamp() | int }}'
title: "Last_triggered: {{ as_timestamp(state_attr('automation.last_triggered_test', 'last_triggered')) | int }}"
Feel free to change the input_boolean to something you have and then test this automation for yourself. The first time you execute it, it may return an unexpected result but that’s only because the last_triggered attribute of a freshly created automation is null (which doesn’t produce a useful timestamp). Wait a minute then turn on the the input_boolean again.
Here’s what I saw and it clearly shows that the two timestamps are not identical (~200 seconds difference).
So now that we have established that the principle is correct, we have to figure out why your template isn’t working the way we want.
EDIT
BTW, that first double-quote in your template is a “fancy” double-quote instead of a standard one. Is that just a copy-paste error or does your template really have that fancy quote there?
Your eyes are sharper than mine - that first double-quote did seem to be the problem. I changed it to ‘normal’ double-quote and it seems to be working correctly now! Well spotted.
I have no idea how it got there but it was in the code, too! Thank you!