How to convert ISO duration to seconds?

tasmota used to return seconds for the uptime. recently the format was changed to iso duration, this is what the tasmota telemetry returns:

{"Time":"2018-06-11T05:19:06","Uptime":"0T22:30:52","Vcc":3.218,"POWER":"OFF","Wifi":{"AP":1,"SSId":"xxx","RSSI":80,"APMac":"34:31:xx:xx:xx:xx"}}

i had hoped i could use a template like this, but it seems the iso8601_to_time filter is not available to the jinja template engine in HA.

{{ "1T22:30:00"|iso8601_to_time }}

any idea how to make the tasmota uptime usable in a sensor (again)?

You could convert it to timestamps and then calculate the diff. below is just an example

{% set uptime = as_timestamp(“2018-06-11T05:19:06”) %} # insert your variable here
{% set date = as_timestamp(now()) %} # get the current timestamp of the date
{%set diff = date - start_date %} # calculate diff in seconds
{{ diff }} # print uptime in seconds

edit: for more complete explanation

thanks, but uptime is not a date, but a duration. therefore as_timestamp("0T22:30:52") does not work with duration.

Is there answer for this?

Hello

Anyone struggling with this, I have written code to convert the duration into number of seconds elapsed.

There may be better or easier ways to do this, but it works at least so allows me to make use of the uptime that Tasmota returns.

{% set duration = state_attr("sensor.light_bathroom_2_status", "Uptime") %}

{% set days = duration.split('T')[0] | int %}

{% set hrs_mins_secs = duration.split('T')[1] %}

{% set hrs = hrs_mins_secs.split(':')[0] | int %}
{% set mins = hrs_mins_secs.split(':')[1] | int %}
{% set secs = hrs_mins_secs.split(':')[2] | int %}

{% set seconds_elapsed = (days * 60 * 60 * 24) + (hrs * 60 * 60) + (mins * 60) + secs %}

{{ seconds_elapsed }}

1 Like