ashscott
(Ash)
December 12, 2022, 9:17am
1
I have a sensor:
sensor.generator_outage_log_duration
,
that returns a string in the format 03:07:08
as h:m:s.
I need to divide the time duration by a float to calculate l/h fuel consumption.
Using this:
{{ strptime(states('sensor.generator_outage_log_duration'), "%H:%M:%S")}}
,
I get
1900-01-01 03:37:08
. Dev tools reports that this is returned as a string also.
How can I get a float or integer returned instead that I can then calculate on?
koying
(Chris B)
December 12, 2022, 12:24pm
2
{% set seconds = as_timestamp(strptime(states('sensor.generator_outage_log_duration'), "%H:%M:%S")) - as_timestamp("1900-01-01") %}
{{ seconds / 3600 | float }} hours
Troon
(Troon)
December 12, 2022, 12:34pm
3
If the format is rigidly hh:mm:ss
for all values and doesn’t ever exceed 100 hours:
{% set ts = states('sensor.generator_outage_log_duration') %}
{{ ts[0:2]|int + ts[3:5]|int/60 + ts[6:8]|int/3600 }}
petro
(Petro)
December 12, 2022, 12:44pm
4
It’s a duration sensor. It will go above that.
petro
(Petro)
December 12, 2022, 12:47pm
5
To use duration sensors, use as_timedelta
{% set fuel = states('sensor.fuel') | float(0) %}
{% set duration = states('sensor.generator_outage_log_duration') | as_timedelta %}
{% if duration %}
{# duration is a timedelta, to get hours divide the objects seconds by 3600 #}
{{ fuel / (dur.seconds / 3600) }}
{% else %}
unknown
{% endif %}
1 Like
ashscott
(Ash)
December 12, 2022, 1:36pm
6
Thanks all for the contributions.
Here’s where I’m at:
- sensor:
- name: "Last outage estimated fuel used per hour"
state: >
{% set fuel = states('sensor.generator_outage_log_estimated_fuel') | float(0) %}
{% set duration = states('sensor.generator_outage_log_duration') | as_timedelta %}
{% if duration %}
{# duration is a timedelta, to get hours divide the objects seconds by 3600 #}
{{ (fuel / ((duration.seconds) /3600)) | round(2) }}
{% else %}
unknown
{% endif %}
From the last log, the outage was 3h 37m 8s in duration, which should equate to 3.618 hours, and a fuel usage of 7.07 litres.
Therefore, 7.07/3.618 is 1.95 litres/hour.
The sensor yields exactly the same.
Thank you all so much.