Notice of Deprecation
Thanks to the contribution of raman325, starting from Home Assistant version 2022.4, an active or paused timer
entity can be restored after a restart. Therefore the restoration technique described in this topic is no longer needed.
This topic remains for historical purposes, in case someone is curious to learn more about the scripting techniques it used to restore active/paused timers. However no further support for it will be provided now that timer restoration is a native feature.
Introduction
Active and paused timers do not survive a restart of Home Assistant (EDIT: prior to version 2022.4). On startup, previously active
timers are set to idle
and donāt resume their (interrupted) countdown. Similarly, paused
timers are set to idle
and reset to their original duration so their elapsed time is lost.
The following strategy automatically restores and resumes active
timers upon startup and restores any paused
timers. It is best suited for timers whose duration is several minutes or longer (i.e. timerās duration
is longer than the time it takes Home Assistant to restart).
It consists of:
- Two input_text entities to store the state of
active
andpaused
timers.input_text.timers_active
input_text.timers_paused
- Two automations. One saves the state of
active
andpaused
timers. The other restores them at startup (onlyactive
timers are also resumed) .automation.timers_save
automation.timers_restore
Step 1 - Create Input_Text entities
Create the following two input_text entities in your configuration.yaml
file:
Click to show input_text entities
input_text:
timers_active:
name: Active Timers
max: 255
timers_paused:
name: Paused Timers
max: 255
- Execute Configuration > Server Controls > Check Configuration and ensure there are no syntax errors.
- Execute Configuration > Server Controls > Reload Input Texts
If you prefer to create them via Configuration > Helpers, ensure you set Maximum Size to 255
characters. Do not change the names of the two entities (timers_active
and timers_paused
) because they are referenced by the automations.
Step 2 - Create automations
Add the following two automations to wherever you keep them (configuration.yaml
or automations.yaml
).
automation.timers_save
The first automation is executed whenever a timer changes state and saves the states of all active
and paused
timers. This automation requires customization:
- Replace the State Triggerās entities with the names of your timers.
- Replace the entities in the
timers
variable with the names of your timers. This list should match the one you have in the State Trigger.
Click to show automation.timers_save
- alias: 'Timers Save'
mode: queued
trigger:
platform: state
entity_id:
- timer.timer_1
- timer.timer_2
- timer.timer_3
action:
- variables:
timers:
- timer.timer_1
- timer.timer_2
- timer.timer_3
modes:
- active
- paused
- repeat:
count: 2
sequence:
- variables:
mode: '{{ modes[repeat.index-1] }}'
- service: input_text.set_value
data:
entity_id: 'input_text.timers_{{ mode }}'
value: >
{% set ns = namespace(timers = []) %}
{% for t in expand(timers) | selectattr('state', 'eq', mode) | list %}
{% set d = t.attributes.remaining.split(':') | map('int') | list %}
{% set s = d[0]*3600 + d[1]*60 + d[2] + (t.last_changed.timestamp()|int if mode == 'active' else 0) %}
{% set ns.timers = ns.timers + ['{} {}'.format(t.object_id, s)] %}
{% endfor %}
{{ ns.timers | join(',')}}
automation.timers_restore
The second automation is triggered at startup and resumes/restores all (previously) active
and paused
timers. This automation does not require customization.
Click to show automation.timers_restore
- alias: 'Timers Restore'
mode: single
trigger:
platform: homeassistant
event: start
action:
- variables:
modes:
- active
- paused
- repeat:
count: 2
sequence:
- variables:
mode: '{{ modes[repeat.index-1] }}'
timers_text: "{{ states('input_text.timers_' ~ mode) }}"
- choose:
- conditions: '{{ timers_text | length > 0 }}'
sequence:
- variables:
timers: "{{ timers_text.split(',') }}"
- repeat:
count: '{{ timers | count }}'
sequence:
- variables:
t: '{{ timers[repeat.index-1].split() }}'
id: 'timer.{{t[0]}}'
d: "{{ t[1]|int - (now().timestamp()|int if mode == 'active' else 0) }}"
- condition: template
value_template: '{{ d > 0 }}'
- service: timer.start
data:
entity_id: '{{ id }}'
duration: '{{ d }}'
- condition: template
value_template: "{{ mode == 'paused' }}"
- service: timer.pause
data:
entity_id: '{{ id }}'
- Execute Configuration > Server Controls > Check Configuration and ensure there are no syntax errors.
- Execute Configuration > Server Controls > Reload Automations
Step 3 - Test
Restart Home Assistant and perform a test:
- Start a timer with a
duration
of 2 minutes (or more if it takes a long time for your instance of Home Assistant to restart). - After youāve confirmed the timer is running, restart Home Assistant.
- After it restarts, check the timer in the States page. It should be
active
and have a shorterduration
.
NOTES
When active
and paused
timers are restored, their duration
will not be the same as prior to the interruption/restart.
Click to reveal details of how timers are restored.
New duration for active timers
After a restart, active
timers are restored and resumed with a new, shorter duration, representing the remaining balance of their original duration. This shorter time is also reduced by the time Home Assistant was off (during a restart or a power-failure).
-
Example 1:
If the timerās duration was originally 10 minutes and it was interrupted at the 5 minute mark, it will resume at 5 minutes minus whatever time it took for Home Assistant to restart. If it takes Home Assistant a full 2 minutes to restart, the timerās new default duration will be 5 - 2 = 3 minutes. The next time you start this timer, you will have to set its duration back to 10 minutes otherwise it will use 3 minutes. -
Example 2:
Assume a timerās duration is 20 minutes and it counts down to 15 minutes when Home Assistant stops due to a power-failure. Home Assistant restarts 5 minutes later. Upon startup the timer is started with aduration
of 10 minutes (not 15) because it takes into account the 5 minutes expended during the power-failure. -
Example 3:
Assume a timerās duration is 20 minutes and it counts down to 10 minutes when Home Assistant stops due to a power-failure. Home Assistant restarts 15 minutes later. Upon startup the timer is not restarted because, if there was no power-failure, it would have expired 5 minutes ago.
New duration for paused timers
After a restart, active
timers are restored (but not resumed) with a new, shorter duration, representing the remaining balance of their original duration. This shorter time is not reduced by the time Home Assistant is off.
- Example 1:
If a timerās duration was originally 10 minutes and it was paused at the 5 minute mark, it will be restored with a duration of 5 minutes (regardless of how much time it takes for Home Assistant to restart, even including a long power-failure). When you start the paused timer, it will complete counting down its remaining 5 minutes. However, its default duration will now be 5 minutes (not its original 10 minutes). You will have to set its duration back to 10 minutes whenever you start it again.