Error in 'template' condition

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.

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 :hushed:

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.

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.

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

I will try soon!

It doesn’t work.
I have now removed that condition.

are there errors in your logs?

No, but the conditions stopped my automation like the time between the two consecutive events was <20 sec but really it was >20 sec.

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) }}

It doesn’t work

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') }}

2025-04-09 16:16:02.332936+02:00
2025-04-09 13:54:51.614840+00:00

Why does it “seem wrong”.

Never mind, you edited your post and removed your comment.

No it isn’t.
I have been confused by the different time zone

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

YES!
It works now