I want to parse a duration string (00:00:00) to minutes/seconds.
I know I can split by ‘:’ char and multiple by the correct units but I was hoping there is something easier
Where is the duration string coming from. Internally time is in Unix time, which is easily manipulated.
Getting the timer remaining time
It looks like the timer component doesn’t kick out Unix format time, so strptime()
, as_timestamp()
etc don’t work on it. You’re left using strip
. Fortunately, the format is consistent so it should work OK.
Created a timer test
like this:
timer:
test:
duration: '01:23:45'
Here are some example templates:
example 1
{{ states.timer.test }}
result
<template state timer.test=idle; duration=1:23:45, remaining=1:23:45 @ 2019-01-18T16:06:40.797499-05:00>
example 2
{{ state_attr('timer.test', 'duration') }}
result
1:23:45
example 3
{{ state_attr('timer.test', 'duration').split(':')[0] }}
{{ state_attr('timer.test', 'duration').split(':')[1] }}
{{ state_attr('timer.test', 'duration').split(':')[2] }}
result
1
23
45
Yes that’s pretty close to what I ended up with:
{% set Hour, Minute, Seconds = remaining.split(‘:’) %}
I thought maybe there was a builtin method for that
Thank you!