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

I want to show the remaining time of a timer - as an attribute to a binary sensor. I think I’ve got a solution, but I’m struggling to understand templating (and I must say, I find the HA docs on templating totally baffling!!)…

This does work, but it feels ‘wrong’. The rest of the package work fine - binary sensor turns to on when power meter goes above 1W, automation to start/cancel the timer as/when the dishwasher starts/stops, automation for notification, and displaying this in a glance/markdown card on the frontend… I’m mainly looking to improve my understanding…

template:
    - binary_sensor:
        ...
        attributes:
          remaining_time: >-
            {% set seconds = as_timestamp(state_attr('timer.dishwasher','finishes_at')) - as_timestamp(now()) %}
            {{ seconds | timestamp_custom('%R', 0) }}

I was a bit unsure why this didn’t work, among adding float/int in a few times (which I don’t really understand…):

{{  as_timestamp(state_attr('timer.dishwasher','finishes_at')) - as_timestamp(now()) | timestamp_custom('%R', 0) }}
1 Like

EDIT: Deleted my answer as it contains flaw. See Taras’ explanation below.

The finishes_at attribute contains no value when the timer isn’t running. Attempting to perform arithmetic with a non-existent value isn’t advisable. Therefore the template should check if the attribute’s value exists before using it.

template:
    - binary_sensor:
        ...
        attributes:
          remaining_time: >-
            {% 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) }}
7 Likes

Grand. thanks for the Ardy and Taras. I got it working thanks so much!

2 Likes

Glad to hear it works and resolves the issue.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

2 Likes

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.