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…
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) }}
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.
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.
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’.
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.