Help with automation for timout

I was switching from IoBroker to HA several months ago. I nearly migrated all scripts from there to HA automations beside of one case.

In blockly I realized the following script:

The idea is to switch a light on after a trigger.
Then there starts a timeout of 5 min and switches the light off.
Everytime the trigger is newly set, the old timeout is canceled and it starts again.

So the light keeps being on unless no trigger is startet and the 5 min timebox is done.

Do you guys have an idea to realize this with HA automations?

Greetings

For example

alias: Neue Automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.flur_eg_bewegung
    to:
      - "on"
conditions: []
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.balkon
    data: {}
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - action: light.turn_off
    metadata: {}
    target:
      entity_id: light.balkon
    data: {}
mode: restart

The easiest would be a turn on then delay, turn off, and set the mode of the automation to restart.
But the better option is to use a timer since that is more resilient

Cool! thanks. I will try this :slight_smile:

One question: When the trigger is set a second time, the timer also restarts with the delay/timer?

In the automation that Johannes posted, yes. (take not of the last line, that is the important one).
With a timer, you need to tell it to do so, but most likely the automation is written in such a way anyways

Hi folks!

Thanks for your help - It works! :tada:

For my couriosity: What exactly is the factor of resilience with the timer solution instead of the delay?

The status of an automation doesn’t survive a restart of HA.

So you trigger a walkway light at dawn and want it to stay on for say 4 h. After a delay of four hours the light is switched off. Now imagine HA is restarted (power failure, updates, …). The automation is then also restarted, however, your light is on and the automation with “trigger at dawn” is not executed. Timers can be set up in such way that they survive a restart and get restored.

In your case that’s not an issue, at worst you would suddenly be in the dark, but any movement will switch on the light.

Speaking of restart, it is good practice to also trigger automations with “HA start” and handle this accordingly.

1 Like