Converting seconds from a sensor to hours and minutes

I have a sensor that provides the remaining remaining cook time in seconds; I can convert it to hours and minutes by setting up a sensor like this, but there must be an easier way, right?

{{ (states('sensor.cook_time_remaining') | round(0) / 3600) | round(0) }}:{{ ((states('sensor.cook_time_remaining') | round(0) - (((states('sensor.cook_time_remaining') | round(0) / 3600) | round(0))*3600)) / 60) | round(0) }}h

This will result in displaying, e.g. 1:9h, which is fine for a hack.

But maybe there’s a way that provides a leading 0 for the 1 - 9 minutes when the cook time is still more than an hour, i.e. 1:09h

And, to get completely carried away, it would show something like 59min … 6min … 48sec when the countdown gets to these values :stuck_out_tongue_winking_eye:

This falls into the “carried away” category…

{% set seconds = states('sensor.cook_time_remaining') | float %}
{%  set periods = { 3600: 'hr', 60: 'min', 1:'sec' } %}

{% set ns = namespace(seconds=seconds)%}
{%- for p_sec in periods.keys() %}
  {%- if seconds >= p_sec -%}
    {% set p_name = periods.get(p_sec) -%}
    {%  set period_value = ns.seconds // p_sec -%}
    {{- period_value ~" "~ p_name ~" "-}}
    {% set dif = period_value * p_sec %}
    {%-  set ns.seconds = (ns.seconds - dif) % p_sec %} 
  {%- endif%}
{%- endfor %}

This should work but only if the remaining time is 24 hours or less (anything longer produces an incorrect value).

{{ states('sensor.cook_time_remaining') | int(0) | timestamp_custom('%H:%M', false) }}

Thanks, guys, I like them both - I adjusted @Didgeridrew’s version to this (again: probably NOT the most elegant way to adjust it, but some of us are more limited in their skills than others):

{% set seconds = states('sensor.cook_time_remaining') | float %}
{%  set periods = { 3600: 'h', 60: 'min', 1: 'sec' } %}

{% set ns = namespace(seconds=seconds)%}
{%- for p_sec in periods.keys() %}
  {%- if seconds >= p_sec -%}
    {% set p_name = periods.get(p_sec) -%}
    {% set period_value = ns.seconds | round(0) // p_sec -%}
    {% set has_s = "" if period_value != 1 %}
    {{- period_value ~""~ p_name ~ has_s~" "-}}
    {% set dif = period_value * p_sec %}
    {%- set ns.seconds = (ns.seconds - dif) | round(0) % p_sec %} 
  {%- endif%}
{%- endfor %}

Now I need to ask SWMBO which one better meets the WAF :wink:

FWIW, if you don’t mind seeing zero values like this:

0h 1min 47sec

then this can do it. It’s the same template but with a different pattern in timestamp_custom.

{{ states('sensor.cook_time_remaining') | int(0) | timestamp_custom('%-Hh %-Mmin %-Ssec', false) }}

As far as I know, there’s no way for the pattern to completely omit a zero-value.

1 Like

Not quite as concise as I wanted but it does display the remaining time in the format you requested.

{{ states('sensor.cook_time_remaining') | int(0)
  | timestamp_custom('%-Hh %-Mmin %-Ssec', false)
  | regex_replace('(^0h| 0min| 0sec$)', '')
  | regex_replace('(^ )', '') }}

This looks great - thanks again @123

I have a similar issue and I’m not able to get it to work as the OP did here. I’m trying to convert the seconds to minutes for my automation notification:

service: notify.mobile_app_my_phone
data:
  message: >-
    ‼️ 🪫 UPS is {{ states("sensor.ups_status") }} with a load of
    {{ states("sensor.ups_load") }}% and runtime of {{
    states("sensor.ups_battery_runtime") }} minutes 🪫 ‼️

I’d like to convert the sensor.ups_battery_runtime to minutes

How about dividing your sensor value by 60 and taking the integer of it?

{{ int(states(‘sensor.ups_battery_runtime’) | round(0) / 60) }}

That did the trick. Thanks!

You can also have a sensor showing runtime in minutes which will show as an entity within HA.

In configuration.yaml:

- platform: template
  sensors:
    myups_runtime_minutes:
      friendly_name: 'My UPS - Runtime Remaining'
      unit_of_measurement: "minutes"
      value_template: >-
        {{ ( states('sensor.ups_battery_runtime') | int / 60 ) | round(0) }}

Restart HA after adding the sensor.