Hi there. I recently configured a garage door automation. I was thinking of automating it to close on its own after a period of time (if forgot to close).
How do you set a timer countdown to trigger a script or automation? Like starting a 30 minute countdown when the garage is opened, and closes when it hits zero?
Hi thanks for all the ideas. I think the best option is adding a “for” in the automation.
But let’s say I want to trigger an automation for a camera PTZ tour every 6 hours? Do you think “for” is the best option with the “repeat while”? Or “wait_template” is better for this? Like:
This is just a sample automation I was thinking about. Sorry for some mistakes. I’m writing this on the phone on the fly.
So in this example, I want to automate camera to do a ptz tour every 6 hours starting from the time when the group leaves home. Is my automation correct for this?
group.family = not home
wait for 6 hours from time group.family left home
trigger script ptz tour.
repeat #2 while family is away / 6 hours from last run of script ptz tour
The while loop will call script.camera_ptz_tour repeatedly. In other words, if it takes one second to execute script.camera_ptz_tour, it will be called every second. I imagine the camera needs more than a second to complete a single tour so a delay may be needed within the while's sequence.
Be aware that Home Assistant has several ways to “countdown” including for, delay, timer, etc but one shortcoming they all have is they do not survive a restart. For example, if you restart Home Assistant during the 6-hour for period, upon startup the countdown is reset to zero (if the countdown was 4 hours into the 6-hour period, it is set back to zero hours).
Or just adding a delay in the camera_ptz_tour script?
About the restart, so if Home Assistant restarted for some reason, like power interruption. The camera_tour_when_family_away automation will still trigger (while the family is away), right? It’s just that the new countdown timer will start when Home Assistant triggers this camera ptz automation.
I think I would need to add a notify service to know if Home Assistant restarted.
If it restarts while group.family is not_home the automation will not be triggered until group.family first changes state to home and then to not_home.
In other words, if the family leaves and one hour later Home Assistant restarts, the automation will never be triggered. The family has to return first and then leave before the automation is triggered again.
Oh I see. So what would be the best solution to get Home Assistant to trigger this automation in case it restarted while no one is home? Remove the from: ‘home’ in platform?
means it must change tonot_home but, after the restart, group.family will already be not_home (i.e. its not changing to that state, it’s already in that state).
You can add a second trigger:
- platform: homeassistant
event: start
When Home Assistant restarts, the automation will be triggered, the condition will be evaluated and confirmed to be true (group.family is not_home) and the action will be executed. The only drawback is that you can’t use for with this type of trigger.
For any “mission critical” time events that need to survive restarts I use an automation to set an input_datetime to the time that thing needs to happen.
Then it doesn’t matter how many times HA restarts or you reload things. as long as HA isn’t restarted at the same time that the input_datetime is set for the event will trigger.
If used in a Template Trigger, that can permit the automation to trigger more than once.
Let’s say the input_datetime is set to 08:00. If it triggers at that time and then later (say at 13:15) you either restart Home Assistant or even just Reload Automations, the automation will trigger again. For some applications that might be desirable behavior but not if the goal is to trigger exclusively at 08:00.