Help with a timer remaining template

Hey people

Really need help with this time remaining template. I would really like to display the hours in HH:MM if HH exceeds 24hrs.

Currently if a value is input into a timer of >24 the template ends up displaying HH:MM-24 although the timer actually uses the >24hrs value

eg: timer.t1 is set to 47hrs


  - sensor:
      - name: time_remaining
        state: >
          {% set t = 'timer.t1' %}
          {% set state = states(t) %}
          {% set finish = state_attr(t, 'finishes_at') %}
          {% set remain = state_attr(t, 'remaining') %}
          {% set seconds = (as_timestamp(finish,0)-as_timestamp(now()))|int %}
          {% set left = iif(finish == None,iif(remain == None,0,as_timedelta('0 ' ~remain).seconds),seconds) %}
          {{ left| timestamp_custom('%H:%M', 0) }}


The template renders 23:00 but does respect 47hrs (when it gets less than 24hrs it renders 23:59) I really want it to render 47:00

Many thanks peeps who can help

It likely won’t work with time functions.
Something like the below might fit your bill

{% set seconds = 164258 %}
{% set hours = (seconds/3600) | int %}
{% set minutes = ((seconds - (hours*3600)) / 60) | int %}
{{ '%d:%d' | format(hours, minutes) }}

Displays

45:37

I mean if you’re super lazy, remaining is just the value that you want but it has seconds.

{{ state_attr('timer.t1', 'remaining') }}

If you want it to countdown…

{% set s = (state_attr('timer.t1', 'finishes_at') | as_datetime - now()).total_seconds() %}
{{ '%02d:%02d' | format(s // 3600, (s // 60) % 60) }}

Keep in mind that these templates will error when the timer is not running. To account for that…

{% set t = 'timer.t1' %}
{% if is_state(t, 'active') %}
  {% set s = (state_attr(t, 'finishes_at') | as_datetime - now()).total_seconds() %}
  {{ '%02d:%02d' | format(s // 3600, (s // 60) % 60) }}
{% else %}
  00:00
{% endif %}
1 Like

Hey @petro

Thanks for this - your way of doing this is far better than mine and I am v grateful for the input.

Tq