Convert timer-duration into seconds

Hi.
In appdaemon.

Im trying to do a check (if) of remaining duration for a timer and an variable that contains a duration in seconds.

if self.get_state(self.args["light_timer"], attribute="duration") < new_delay:

But this returns following error: TypeError: ‘<’ not supported between instances of ‘str’ and 'int
I guess this is because the timer returns a value like 00:05:00?

How can I convert the timer to return a value in seconds?

Thanks.

Hi

What is this “light_timer”? Is it an entity in home assistant?

What is the result of the function below? 00:05:00?

self.get_state(self.args["light_timer"], attribute="duration")

Solved this by:

 str_duration = self.get_state(self.args["light_timer"], attribute="remaining")
 str_duration = str_duration.replace(".",":")
 remaining_duration_in_seconds = sum(x * int(t) for x, t in zip([3600, 60, 1,0], str_duration.split(":")))

btw, “light_timer” is a timer entity from hassio.

Ah I understand.

Why do you need the second line of code? You replace “.” with “:” and in the next line you split str_duration by “:”, but you could just split by “.” and remove the second line of code.

like this:

str_duration = self.get_state(self.args["light_timer"], attribute="remaining")
remaining_duration_in_seconds = sum(x * int(t) for x, t in zip([3600, 60, 1,0], str_duration.split(".")))
1 Like