Unbelievable nobody else replied yet, but a huge thanks !
Many thanks for posting this solution. One comment: example 3 for the new duration of an active timer covers a case where the timer.finished
event isn’t fired because the timer is not restarted. So any automations that rely on that event will need some work.
For example, I have a vacuum that starts a 1 hour timer when kicked off and uses timer.finished
to stop. For my case I also trigger on the battery level dropping below 20%, so my automation requires no additional work, but anyone using your solution needs to keep this in mind.
True but anyone who does not use this solution faces the same predicament (and then some because none of their timers will be restarted so none will produce a timer.finished
event after startup).
As a workaround start the timer for 1 second ?
Feel free to add it to your instance of the code.
FWIW, a long time ago, someone submitted a PR to enhance timers. In addition to restoring an interrupted timer, it also introduced the concept of a ‘grace period’ where additional time could be appended to a restored timer. In effect, it attempted to address the edge-case you described. Ultimately, the PR was rejected by the development team and the ‘grace period’ was one of the contentious issues.
Hi @123 ,
First off I want to thank you for this solution. This is exactly what I require for my set up.
I would like to add @devastator 's suggestion of resetting the timer to 1 second if on reboot the timers have in fact finished and there is no time value to restore. I get a lot of power outages where I live and this would be very useful.
Could you please help me figure out what lines of code I would need to add to your code in order to make this work?
Thank you for your time and help,
- Kal
Send a PM to devastator and ask if they ever implemented it.
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).
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.
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)?