my mqtt sensor returns a string in the following time format hh:mm:ss.xxxxx
How can I get the total seconds with templating?
got:
{% set list = states.sensor.mysensor.state.split(':') %}
{% set seconds = (list[0] | int(0) * 3660) + (list[1] | int(0) * 60 ) + (list[2] | int(0)) %}
this is working but seems odd. Is there a better way?
If you are on 2022.6 you can use the newly added as_timedelta
function… followed by total_seconds()
.
{{ as_timedelta( states("sensor.my_sensor") ).total_seconds() }}
For older versions you can use the old timestamp-based hack:
{{ as_timestamp("1970-01-01 " ~ states("sensor.my_sensor") ~ "+00:00", 0) }}
FWIW, the value in your original template should be 3600, not 3660.
1 Like
Thank you very much! Missed that on the release Notes.