How to set a countdown timer to trigger an automation?

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?

You can use the “Wait” action.

The timers fire an event you can listen for, if it’s the timers you want to use.

I would probably make sure some announcement, sound or light is warning before closing.

Alternative, use a State trigger that fires if the garage door‘s state is „open“ for a given time.

platform: state
entity_id: cover.garage_door
to: 'open'
for:
  minutes: 30

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:

- alias: camara_tour_when_family_away
  trigger:
  - platform: state
    entity_id: group.family
    from: ‘home’
    to: ‘not_home’
      for: ‘06:00:00’
    condition: “{{ is_state(‘group.family’, ‘not_home’) }}”
    action: 
    - repeat:
      while: “{{ is_state(‘group.family’, ‘not_home’) }}”
      sequence:
      - service: script.camera_ptz_tour
    - service: camera.go_to 
      preset: ‘1’
      entity_id: camera.camera_1

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?

  1. group.family = not home
  2. wait for 6 hours from time group.family left home
  3. trigger script ptz tour.
  4. repeat #2 while family is away / 6 hours from last run of script ptz tour
  5. return to ptz preset 1 when family arrives home

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).

So it will something like this:

- alias: camara_tour_when_family_away
  trigger:
  - platform: state
    entity_id: group.family
    from: ‘home’
    to: ‘not_home’
      for: ‘06:00:00’
    condition: “{{ is_state(‘group.family’, ‘not_home’) }}”
    action: 
    - repeat:
      while: “{{ is_state(‘group.family’, ‘not_home’) }}”
      sequence:
      - service: script.camera_ptz_tour
      - delay: ‘06:00:00’
    - service: camera.go_to 
      preset: ‘1’
      entity_id: camera.camera_1

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?

I doubt that would help because this:

to: 'not_home'

means it must change to not_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.

Got it. Thanks!

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.

This is new to me. I’ll read about how to use this. Thanks!

1 Like

But even that can be accomplished with if now() >= input_datetime

yes, that’s right.

As long as that still meets the requirements - sometimes you really do want something to only happen at that time but not after.

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.

That was just a fast example.
In reality you would need a boolean to make sure it only runs once as condition.
Or some other method.

Can you explain how the “boolean” is set or was that another “fast example”?


EDIT

Never mind. I think I know what you’re suggesting.