Templating help - Show remaining time in HH:MM of timer

Hi,

I put this in “dev tools → Templates” in Home Assistant, and show the remaining time of a timer. Ok.

{% set f = state_attr('timer.dishwasher', 'finishes_at') %}
            {{ '00:00:00' if f == None else 
              (as_datetime(f) - now()).total_seconds() | timestamp_custom('%H:%M:%S', false) }}

But, this remaining time does not decrease every second.
Is this possible?
How I show a countdown in UI?

Thanks

2 Likes

It won’t a template can’t update once per second without using a timepattern trigger, which can only be done in a template sensor. The template you’re using will only update on the minute.

1 Like

Thank you. Working fine.

But, I don’t understand why the timer remaining time attribute can’t be used for this.

because it doesn’t update. It’s only available when running and it just tells you the duration of the current time. You can change the duration and it won’t affect the current run. So remaining is the attribute that you use to get the ‘current duration’.

Allow me one last question,

This sensor works fine, but the trigger runs every second, how do you control it to only run when the timer is active?

template:
  - trigger:
      - platform: time_pattern
        seconds: "*"
    sensor:
      - name: "Countdown"
        state: >-
            {% set f = state_attr('timer.countdown', 'finishes_at') %}
            {{ '00' if f == None else
              (as_datetime(f) - now()).total_seconds() | timestamp_custom('%S', false) }} 

You can’t in the template itself. You’d have to make the template differently and pair it with an automation.

template:
  - trigger:
      - platform: time
        at: '00:00'
    sensor:
      - name: "Countdown"
        state: >-
            {% set f = state_attr('timer.countdown', 'finishes_at') %}
            {{ '00' if f == None else  (as_datetime(f) - now()).total_seconds() | timestamp_custom('%S', false) }}
- alias: update countdown
  trigger:
  - platform: time_pattern
    seconds: '*'
  condition:
  - condition: state
    entity_id: timer.countdown
    state: active
  action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.countdown

The template will only update at midnight. The automation forces it to update every second when the timer is active.

2 Likes

If you view timer.countdown in an Entities card, it will show the timer’s status. When the timer is active, it displays its countdown in seconds.

Example:

An inactive 10-minute timer.
Screenshot_20221129-075753~2

An active 10-minute a few seconds after starting its countdown.
Screenshot_20221129-075904~2

Yes, but this is not so in other cards, this is the problem.

Ok, thanks.

Many lines of code for something that could have the timer.

I don’t know why you’re telling me this. I didn’t make it and I didn’t take part in the decision to have it this way.

In which “other cards”?

In this case, with a custom:config-template-card.

It was not my intention to upset you, many of us do not know who has done what. Thanks for the help.

It’s a custom card. Contact the card’s author.

well, it’s a little more complicated than that. The template card has direct access to states via JS. So contacting them will do little to nothing as you’re getting out the raw state from the state machine. Not to mention, he’s been MIA for some time IIRC.

Full disclosure: I know nothing about that particular custom card.

My guess is that the Entities card calculates the remaining time exclusively in the frontend. I would imagine that any card can be designed to replicate that behavior; it only requires the finishes_at value and the current time (with one-second resolution). Again, just a guess because I don’t currently have the time to inspect the card’s code.

It uses templates, but with javascript. It just outputs the result of the template. He’s most likely grabbing the state from the state machine. I.e. he’d have to build the HTML that displays the information properly but he most likely doesn’t have access to it in the templating machine built into the card.

Ah! Understood. It’s a generalist and not really within its mission to give any particular entity special handling (like “show timer’s remaining time”).

right, you probably can do it though if you had access to the frontend toolkit within the template itself. Not sure if that’s a thing for that card or not.

Unfortunately it doesn’t show me the remaining time in the entity card, only “active”.