Hey all,
I’m trying to figure out this time and duration templating business on HA. I have the following template in my automation yaml:
value_template: >-
{{ (trigger.to_state.last_changed - trigger.from_state.last_changed) }}
which returns a duration that looks like this : 0:03.37.750373
Now I want to check if this duration is greater than 15 mins. aka 0:15:00.0. Is my attempt below correct :
value_template: >-
{{ (trigger.to_state.last_changed - trigger.from_state.last_changed) >
'0:15:00.0' }}
Please don’t ask me to fully transform timestamps to minutes only becuase I have no idea how to.
Thanks.
Is there anything you could use here:
Easy Time calculations for Home Assistant templates
Relative Time Macro with additional options
Thanks but I’m not sure if that helps.
I would much rather familiarize myself with in-built HA time based templating first. Then I guess I’ll be better positioned to know if I require the integration.
Subtraction or addition of datetime objects returns a timedelta object so you need to use as_timedelta() so you are comparing like types:
{{ trigger.to_state.last_changed - trigger.from_state.last_changed >
as_timedelta('0:15:00.0') }}
It could also be done as follows, using timedelta():
{{ trigger.to_state.last_changed - trigger.from_state.last_changed >
timedelta(minutes=15) }}
Both of these filters can be found in the Time section of the Building Templates page in the docs.
1 Like
That was easy - works! Thank you!