Understand what you are saying and the decision about timer was made that it has limited purpose. I would expect from timer as a click option to show remaining time because the database can handle this as nothing when you use it for minutes, once in a while and it should be up to the user to choose from, to help him and save his time. Now to the logic workaround with templates.
You wrote: If you want a countdown, make a template sensor that has the value of the ending time as a state. Wherever you put it in the UI will show it’s countdown, live.
template:
- sensor:
- name: "countdown"
state: >
{% set fin = state_attr('timer.countdown', 'finishes_at') %}
{{ '00' if fin == None else as_datetime(fin).astimezone().time() }}
Above it is, the ending time. Anywhere in card I put it I see ending time what is logical, not remaining time as you wrote. Only timer itself in only one type card of entities showing remaining time. So foreground countdown not much from it.
Working solution as you wrote above with writing to database:
template:
- sensor:
- name: "countdown2"
state: >
{% set fin = state_attr('timer.countdown', 'finishes_at') %}
{{ '00' if fin == None else (as_datetime(fin) - now()).total_seconds() | timestamp_custom('%M:%S', false) }}
Now I can send via mqtt the remaining time via sensor.countdown2 to electronic clock display.
But yes, I can save the writting to database using calculation directly in mqtt payload:
payload: "{{ (as_datetime(state_attr('timer.countdown', 'finishes_at')) - now()).total_seconds() | timestamp_custom('%M:%S', false) }}"
Plus automatisation to set duration from input to timer.
Plus automatisation to update remaining / send via mqtt.
also if anybody will find it usefull:
I can take duration from input_datatime like this:
{% set dur = strptime(states('input_datetime.minutka'), "%H:%M:%S") %}
I can create final time like this:
{% set fin = (now() + timedelta(hours = dur.hour, minutes = dur.minute)) %}
but in template sensor it would be updated as time goes, not once with manual trigger:
template:
- sensor:
- name: "finaltime"
state: >
{% set dur = strptime(states('input_datetime.minutka'), "%H:%M:%S") %}
{{ now() + timedelta(hours = dur.hour, minutes = dur.minute) }}
- sensor:
- name: "countdown"
state: >
{% set fin = states('sensor.finaltime') %}
{{ (as_datetime(fin) - now()).total_seconds() | timestamp_custom('%M:%S', false) }}
So I learned a lot again, which I will forget until next time. Again, I thought I’d use the timer helper and it would save me hours of searching, learning to write, but as every HA user knows, if you want something, carve it out.
Thank you petro for helping.