Solarflor
(Solarflor)
April 9, 2025, 9:49am
1
Hi,
In my automations, I often use the following condition to prevent the repetition of an action if the event occurred within a specific time frame (20 seconds in this case) since the last occurrence of the event.
conditions:
- condition: template
value_template: >-
{{ (as_timestamp(now()) -
as_timestamp(state_attr('automation.voice_and_video_alert_from_frigate',
'last_triggered') | default(0)) | int > 20)}}
This condition worked for months, but a week ago it stopped working and now returns the following error message:
Errore: In 'template' condition: ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.voice_and_video_alert_from_frigate', 'last_triggered') | default(0)) | int > 20)}}' but no default was specified
I’m not sure what the mistake is, especially since it has always worked before. Could you help identify the issue and suggest a solution?
Hi @Solarflor
Have you tried looking at it in Developer Tools? Try your whole template and then try each component part.
Solarflor
(Solarflor)
April 9, 2025, 10:03am
3
I have just tried to remove the template conditions and the automation works, so the problem is there.
I have not yet tried to modify it as I’m not sure to be able to do it.
UNBELIEVE - I have just reintroduced it and now it works. but I copied exactly the same instructions as before
koying
(Chris B)
April 9, 2025, 11:25am
4
Your template assumes that your “voice_and_video_alert_from_frigate” has been triggered at least once.
It wasn’t the case when you got the error, for whatever reason.
petro
(Petro)
April 9, 2025, 12:03pm
5
I recommend using this template instead
{{ state_attr('automation.voice_and_video_alert_from_frigate', 'last_triggered') | as_datetime(now()) | as_local - now() > timedelta(seconds=20) }}
It does the same thing but isn’t prone to errors.
Solarflor
(Solarflor)
April 9, 2025, 1:01pm
6
This is why it is not working anymore after I restarted HA.
By the way it was working well for a number of months so some chnages have been done in the recent updates
Solarflor
(Solarflor)
April 9, 2025, 1:15pm
8
It doesn’t work.
I have now removed that condition.
petro
(Petro)
April 9, 2025, 1:16pm
9
are there errors in your logs?
Solarflor
(Solarflor)
April 9, 2025, 1:24pm
10
No, but the conditions stopped my automation like the time between the two consecutive events was <20 sec but really it was >20 sec.
petro
(Petro)
April 9, 2025, 1:28pm
11
Then it’s already a datetime, this would be all you need in that case
{{ state_attr('automation.voice_and_video_alert_from_frigate', 'last_triggered') - now() > timedelta(seconds=20) }}
123
(Taras)
April 9, 2025, 2:13pm
13
Copy-paste the following template into the Template Editor (Developer tools → Template) and let us know the two datetimes it reports.
{{ now() }}
{{ state_attr('automation.voice_and_video_alert_from_frigate', 'last_triggered') }}
Solarflor
(Solarflor)
April 9, 2025, 2:17pm
14
2025-04-09 16:16:02.332936+02:00
2025-04-09 13:54:51.614840+00:00
123
(Taras)
April 9, 2025, 2:18pm
15
Why does it “seem wrong”.
Never mind, you edited your post and removed your comment.
Solarflor
(Solarflor)
April 9, 2025, 2:19pm
16
No it isn’t.
I have been confused by the different time zone
123
(Taras)
April 9, 2025, 2:26pm
17
Reverse the two datetimes in the subtraction.
{{ now() - state_attr('automation.voice_and_video_alert_from_frigate', 'last_triggered') > timedelta(seconds=20) }}
last_triggered
contains a value in the past whereas the value of now()
is the present.
1 Like