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
This is an old thread but though I’d share how I went about converting a ISO8601 Duration string, e.g “P1DT2H5M34S” or “PT5M0S”, or even “PT0S” when null.
My use case is the PSA API for my EV returns charging remaining time until full/threshold reached in that format but wanted a more helpful display in HA.
Python is not my strength, so may be a better way around it:
# Get duration in ISO8601.
{%- set duration = state_attr("sensor.zafi", "energy")[0]["charging"]["remaining_time"] -%}
# Remove the P prefix and split into days and time.
{%- set duration = duration.replace('P','').split('T') -%}
# Only set Days if present in duration.
{%- set days = duration[0].split('D')[0] if 'D' in duration[0] else '' -%}
{%- set time = duration[1] -%}
# Get hours, minutes & seconds if set, otherwise empty string.
# Update variable at each stage, removing already extracted incremement.
{%- set hrs = time.split('H')[0] if 'H' in time else '' -%}
{%- set time = time.split('H')[1] if 'H' in time else time -%}
# Minutes
{%- set mins = time.split('M')[0] if 'M' in time else '' -%}
{%- set time = time.split('M')[1] if 'M' in time else time -%}
# Seconds
{%- set secs = time.split('S')[0] if time.split('S')[0]|int > 0 else '' -%}
# Create list to iterate over and generate human readable string value.
{%- set values = [('day', days),('hour', hrs),('minute', mins), ('second', secs)] -%}
{%- set remaining = namespace(time=[]) -%}
{%- for increment,value in values -%}
# check for empty string (duration incremement not available)
{%- set value = value | int if value else 0 -%}
{%- if value > 0 -%}
# Human readable string: "3 hours 1 minute" (with pluralization).
{%- set value = value ~ ' ' ~ increment ~ 's' if value > 1 else value ~ ' ' ~ increment -%}
# Add available time increments to list.
{%- set remaining.time = remaining.time + [value] -%}
{%- endif -%}
{%- endfor -%}
{{- remaining.time|join(' ') -}}