Restore active/paused timers after a restart

Thanks for the reply @123 . I’ll try that.

@devastator , have you by chance already implemented your suggestion of resetting the timer to 1 second?

Thanks folks,

  • Kal

No net yet …

Please forgive my ignorance as I am completely guessing here:

Would changing the 0 to a 1 in the this line of the automation.timers_restore code be a good start?

d: ‘{{ t[1]|int - (now().timestamp()|int if mode == ‘‘active’’ else
0) }}’

I tried running a test with this change but it didn’t seem to be successful. Either I’m completely off track, running my test wrong, or there could be another line of code needed. Thoughts?

  • Kal

This is untested but I think it will do what you want. If an active timer has negative remaining time (meaning there is no remaining duration left), it is restarted with a duration set to 1 second so that it can be allowed to quickly run and finish. This allows the timer’s completion to trigger any automations you may have that are listening for the timer’s finished event

                   d: >
                     {% set ts = t[1]|int %}
                     {% if mode == 'active' %}
                       {% set ts = ts - now().timestamp()|int %}
                       {{ ts if ts > 0 else 1 }}
                     {% else %}
                       {{ ts }}
                     {% endif %}

I would advise using this judiciously because briefly running a timer beyond its expiration time may have unforeseen consequences.

Hi, thanks a lot for this. I’ve tried to create the first automation ‘Timers save’ and I get a ‘Message malformed: expected dictionary’ when I tried to save it using the yaml editor from the Automations menu on the UI.
Thank you all very much for your help.

I don’t use the Automation Editor because it has a few quirks that can negatively impact an automation’s syntax (i.e. it can change things and not always for the better). That might be the cause of the error you encountered. I created both automations with a text editor, confirmed they were valid with Check Configuration, and they have been operating properly on my system (for many months).

1 Like

Whouaou, this is great, thank you for this solution. I just implemented it (including the 1sec negative time) on my swimming pool pump timers and it works great so far.

@123 Simply fantastic. Exactly what I needed for some timers on a remote (not-in-my-house), unattended Home Assistant project. That is some special Jinja-fu you have going on there, including the 1 second duration for expired timers. Thanks for sharing.

Thanks for sharing this. I always forget, however, to add new timers to the automation. Is it possible to enumerate them rather than have to input them rather than having to add each one each time?

EDIT: I think I found my answer. :neutral_face:

Hey Taras. This solution works like a charm for home assistant restart. But it doesn’t work for reloads e.g. homeassistant.reload_config_entry. Any idea how we could make it work?

It was designed exclusively for restoring active/paused timers after a restart.

If you want it to perform that function for other events, feel free to add whatever additional triggers you want to automation.timers_restore.

Hey Taras. Actually it does work for the entity reloads as well. I had forgotten to add that specific timer that I was testing to the timer automations you asked us to create. Upon adding it there the timer kept running even after an entity reload. Sorry to bother you. Great work with this concept. It solved so many of my automation problems!!

To my knowledge, homeassistant.reload_config_entry is for reloading items listed in the config_entry registry.

Are you using that service call to explicitly reload the timer integration (and why)?

Un grand merci pour ça !

1 Like

FYI I have opened a PR to support this natively: Allow timer state to be restored on restart by raman325 · Pull Request #67658 · home-assistant/core · GitHub

The implementation differs slightly from your solution but I think it’s a sensible choice (obviously, since I wrote it :grinning_face_with_smiling_eyes: )

EDIT: Merged and part of 2022.4!

3 Likes

Can anyone help me understand this please? I’m a rookie in this input_text automation.
I have this code:

input_text:
  timers_active:
  timers_paused:

This is in automation:

- id: '1678043263387'
  alias: timers_save
  description: ''
  trigger:
  - platform: state
    entity_id:
    - timer.rodi_water
  condition: []
  action: []
  mode: single
- id: '1678043335545'
  alias: timers_restore
  description: ''
  trigger:
  - platform: state
    entity_id:
    - timer.rodi_water
  condition: []
  action: []
  mode: single

The first post in this topic indicates the presented technique has been deprecated and no further support will be provided.

This is the solution I’ve been looking for a long time, great!

All of my lighting automations use timers. If HA is restarted while the timers are less than 40 seconds, all lights stayed on and the timers were not renewed.

This solution fixes the problem. I think this is a great work around for timer.finished event cannot be catched at startup of Home Assistant if timer ended while Home Assistant was down. · Issue #79145 · home-assistant/core · GitHub
But I think the comment (an active or paused timer entity can be restored after a restart) in the first post regarding the timer restart might be misleading.

The comment was based on how the a timer’s restore option was originally described in the Pull Request that implemented it and in its documentation.

image

If it fails to behave as per its PR and documentation then it’s a bug. Your link indicates it was reported almost 7 months ago but, unfortunately, it never attracted the attention of anyone willing to fix it. That’s a shame because it’s a very useful feature.

hi,
i still using active timers and getting errors after a restart. (the old version)
do i need to change anything (from the beginning i saw that there is no need)
and just disable it ?
Thanks