Restart timer in Automation Trigger FOR time

Hi all

My automations turn off lights when a sensor has been triggered X minutes ago.

However, if the sensor is triggered again during that time it doesn’t restart the FOR time and still waits until the original timeout period.

Is there any way to restart/reset the FOR timer or automation so when the sensor triggers again the automation also restarts?

Current automation example (it’s got two triggers, but they run as an OR so either one works but still doesn’t reset the timer if the sensor triggers again)

- id: '12345'

  alias: Office Lights Off
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.office_motion
    to: 'off'
    for: 00:20:00
  - platform: state
    entity_id: light.office
    to: 'on'
    for: 00:20:00
  action:
  - service: light.turn_off
    entity_id: light.office
  mode: single

TIA

So, you have the trigger here, set to run the automation when the sensor has been off for TWENTY MINUTES. If you activate the sensor you will have to wait another TWENTY MINUTES before it will run the automation again. But if you edit any automation in the meantime, it won’t fire again until it’s gone to the off state again for 20 minutes.

A little bit of context, I run another automation that changes the state of the sensor back to OFF after the sensor (motion) has been triggered. The sensor stays at ON unless I run the other automation to change it back to OFF.

The lights off automation above simply works out when the motion sensor was last triggered (and reset and changed to OFF)

That’s fine with the second automation, because there is no waiting. The problem with the first automation is that if you are still adding new automations or editing existing ones, every time you save - ALL automations are reloaded, and any timers, including ones that are waiting for an entity or attribute to be in a specified state for a period of time - get reset. But they won’t start immediately counting down the timer again after the reload, because the state of the entity has to change from whatever state it is currently in, to the state desired before the timer will start again. Thus if the sensor is already off, and you have done anything that causes all automations to be reloaded, then the sensor first needs to go ON and then back OFF and be OFF for 20 minutes before the automation will run.

This is the reason I prefer to use actual timers, and then I just listen for the timer finished event in my trigger.

trigger:
  - type: opened
    platform: device
    device_id: 1840d0e06367e8bd14385b058e0d7bf7
    entity_id: binary_sensor.bathroom_door_contact
    domain: binary_sensor
    id: door_open
  - platform: device
    type: turned_on
    device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
    entity_id: light.bathroom_light
    domain: light
    id: light_on
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - platform: event
    event_type: timer.finished
    id: timer_finish
    event_data:
      entity_id: timer.bathroom_light
  - platform: device
    type: turned_off
    device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
    entity_id: light.bathroom_light
    domain: light
    id: light_off

As you can see here, I listen for the light being turned on, the light being turned off, the door being opened AND the timer finishing.

Now for the important part:

sequence:
          - type: turn_on
            device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
            entity_id: light.bathroom_light
            domain: light
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
          - service: timer.start
            data:
              duration: '00:20:00'
            target:
              entity_id: timer.bathroom_light

When the door is opened, the timer is first cancelled, in case it was already running, and then started with a 20 minute duration.

      - conditions:
          - condition: trigger
            id: timer_finish
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.bathroom_light

When the timer finishes (matched by the timer_finish id in the trigger) then I turn the light off.

Here is the full automation so you can see the other stuff it does:

alias: 'Light: Bathroom Light'
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 1840d0e06367e8bd14385b058e0d7bf7
    entity_id: binary_sensor.bathroom_door_contact
    domain: binary_sensor
    id: door_open
  - platform: device
    type: turned_on
    device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
    entity_id: light.bathroom_light
    domain: light
    id: light_on
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - platform: event
    event_type: timer.finished
    id: timer_finish
    event_data:
      entity_id: timer.bathroom_light
  - platform: device
    type: turned_off
    device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
    entity_id: light.bathroom_light
    domain: light
    id: light_off
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: door_open
              - condition: or
                conditions:
                  - condition: state
                    entity_id: binary_sensor.day_night_time
                    state: 'off'
                  - condition: numeric_state
                    entity_id: sensor.lux_sensor_illuminance_lux
                    attribute: illuminance_lux
                    below: '1500'
        sequence:
          - type: turn_on
            device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
            entity_id: light.bathroom_light
            domain: light
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
          - service: timer.start
            data:
              duration: '00:20:00'
            target:
              entity_id: timer.bathroom_light
      - conditions:
          - condition: trigger
            id: light_on
          - condition: state
            entity_id: input_boolean.house_in_night_mode
            state: 'off'
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
          - service: light.turn_on
            target:
              entity_id: light.bathroom_light
            data:
              transition: 20
              brightness_pct: 50
          - service: timer.start
            data:
              duration: '00:20:00'
            target:
              entity_id: timer.bathroom_light
      - conditions:
          - condition: trigger
            id: light_on
          - condition: state
            entity_id: input_boolean.house_in_night_mode
            state: 'on'
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
          - service: light.turn_on
            target:
              entity_id: light.bathroom_light
            data:
              transition: 20
              brightness_pct: 8
          - service: timer.start
            data:
              duration: '00:10:00'
            target:
              entity_id: timer.bathroom_light
      - conditions:
          - condition: trigger
            id: timer_finish
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.bathroom_light
      - conditions:
          - condition: trigger
            id: light_off
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
    default: []
mode: restart

Perfect. Will give that a try. Cheers

I had a look at a simpler way for this to work and using the “restart” mode in the automation sorted this for me. So, if the automation triggers during the FOR time period it restarts so exactly what I wanted. Once caveat though, using two triggers (although they’re an OR) means that in my instance it didn’t know which one to start the FOR timer against. So, I’ve removed the light_on state, which is fine as the motion sensor triggers the lights on anyway.

- id: '12345'
  alias: Office Lights Off
  description: ''
  mode: restart
  trigger:
  - platform: state
    entity_id: binary_sensor.office_motion
    to: 'off'
    for: 00:20:00
  action:
  - service: light.turn_off
    entity_id: light.office

@chrisaxe21 Thanks!! was looking for this.
So simpel :slight_smile: