Timer attribute "Duration" check if greater than

Hello

I want to use the below as a condition so actions of start.timer with a duration are not overwriting a timer if the duration being sent is less that the current timer duration. However, this always returns true no matter what the attribute is set too.

{{ state_attr('timer.secuirty_lights', 'duration') > '00:04:00' }}

The hours part of the string is not zero-padded. Try '0:04:00'.

Also, “secuirty”?

I don’t think string comparisons using greater than and less than are going to return the expected results. You should really convert the duration to an int and then compare them.

Try:

{{ (state_attr('timer.secuirty_lights', 'duration') | int) > 4 }}

The only thing is that I have no idea what format that attribute is in, and if it really is a string with the colons and zeros, that probably won’t convert to an int without some more work. This might work (assuming the duration is only minutes only every time):

{{ (state_attr('timer.secuirty_lights', 'duration').split(":")[1] | int) > 4 }}

They will if it’s in a fixed, most-significant-first format (ISO8601, for example).

You could convert to seconds:

{% set h,m,s = state_attr('timer.secuirty_lights', 'duration').split(":")|map('int')|list %}
{{ h*3600 + m*60 + s > 240 }}

All of the suggestions here so far are not great :stuck_out_tongue:

Use the built-in functions/filters. Much less chance for the unintended to happen that way.

One thing I certainly did not expect is that according to the documentation for the Timer integration, the duration attribute may contain both a timestring or an int specifying seconds. WTF?

If a template needs to take that into account, things quickly turn messy:

{{ (state_attr('timer.secuirty_lights', 'duration') | string
if state_attr('timer.secuirty_lights', 'duration') is number else
state_attr('timer.secuirty_lights', 'duration')) | as_timedelta
> '00:04:00' | as_timedelta }}

And pretty please fix the typo in timer.secuirty_lights…

ok wow that worked perfect! Thank you

security is just the name of my entity.

No it isn’t. It is, according to your code, secuirty.

this worked as well and yes I will only use minutes

haha I see that now thank you!

Here’s another way to compare duration to a time value.

Convert duration’s string value to a timedelta then compare it to a 4 minute timedelta.

{{ state_attr('timer.security_lights', 'duration') | as_timedelta > timedelta(minutes=4) }}

EDIT

Originally I thought I accidentally duplicated Mayhem_SWE’s example but, upon closer inspection, it’s different.

There’s no need to use an if to check the duration value’s format prior to conversion. The duration value is stored as a time string in H:M:S format, even when it’s initially specified as a number in seconds.

1 Like

You can specify it as a string or a number of seconds. The actual attribute will be a string though.

this worked too very nice! Thank you so much everyone