Light off automation with new "on" events resetting the countdown

Hi everyone,

I hope you can help me with the following.

I’ve got an automatioon that turns off garage lights (via Sonoff 2-channel light switch) after a certain period of time, however current logic is flawed.

There are two triggers in the automation that turns on the garage lights:

  1. Via contact sensor on the garage back door (binary_sensor.garage_back_door)
  2. Via motion sensor in the garage (binary_sensor.garage_motion)

Code below for the automation that turns on the lights (ID values scrapped just in case, not sure if those should be shared):

- id: '123123123123123'
  alias: Garage Lights On
  description: ''
  trigger:
  - entity_id: binary_sensor.garage_back_door
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.garage_motion
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - entity_id: switch.sonoff_1000884e8a_1
    service: switch.turn_on
  - delay: 00:00:01
  - entity_id: switch.sonoff_1000884e8a_2
    service: switch.turn_on

And this is the automation that turns the lights off:

- id: '123123123123123'
  alias: Garage Lights Off
  description: ''
  trigger:
  - entity_id: binary_sensor.garage_motion
    from: 'on'
    platform: state
    to: 'off'
    for: 0:00:30
  condition: []
  action:
  - entity_id: switch.sonoff_1000884e8a_1
    service: switch.turn_off
  - delay: 00:00:01
  - entity_id: switch.sonoff_1000884e8a_2
    service: switch.turn_off

The problem is that this automation is only triggered when the motion sensor’s (binary_sensor.garage_motion) status changes from “on” to “off” (I have another automation that switches it to “off” state in 5 seconds from coming to “on”). So if the lights had been turned on via sensor on the garage back door (binary_sensor.garage_back_door), or manually via the switch without ever setting off the motion sensor, then the 30 second countdown would never start, and the lights would remain on indefinitely.

Ideally, I’d like any time the lights are turned on to trigger a 30-second coundown, and for every subsequent “turn on” even to reset that countdown back to 30 seconds every time. However, as far as I know, automations can only be triggered by a changed state (“on” to “off”, or “off” to “on”), so sending a new “on” request to a device that’s already “on” would effectively do nothing.

What’s would be the most efficient way of achieving the above?

Thanks in advance for your help!

So there are a number of ways to do this. In my opinion this is the easiest way. Create a script with a delay. After the delay turn off the lights. In your automation, turn off the script (resetting the script if it’s on), then turn on your lights. Lastly, turn on the script. This effectively gives you a timer that resets each time the automation is fired.

- alias: Garage Lights Motioin
  trigger:
  - entity_id: binary_sensor.garage_back_door
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.garage_motion
    from: 'off'
    platform: state
    to: 'on'
  action:
  - service: script.turn_off
    entity_id: script.garage_door_motion
  - entity_id: switch.sonoff_1000884e8a_1
    service: switch.turn_on
  - delay: 00:00:01
  - entity_id: switch.sonoff_1000884e8a_2
    service: switch.turn_on
  - service: script.garage_door_motion
    data:
      delay: "00:00:30"

script

garage_door_motion:
  sequence:
  - delay: "{{ delay }}"
  - service: switch.turn_off
    entity_id: switch.sonoff_1000884e8a_1
  - delay: 00:00:01
  - service: switch.turn_off
    entity_id: switch.sonoff_1000884e8a_2

The beauty if this is that it will be independent of your other automation. But you can add that other device into the automation as well.

1 Like

This is brilliant, I’ll integrate the change tonight and report back, but using a delayed script that’s reset every time the automation is triggered sounds like a great solution. Thanks for taking the time to propose the idea and even the code!

1 Like

Hi petro, just confirming that your solution has worked, thanks again for your help!

Hi, I’m in the same situation and tried to apply pedro’s solution but it didn’t work for me. Am I missing something?

automations.yaml

- alias: Kitchen lights automation
  trigger:
    - platform: state
      from: "off"
      entity_id: binary_sensor.kitchen_motion_detected
      to: "on"
  action:
    - service: script.turn_off
      entity_id: script.kitchen_motion_detected
    - entity_id: switch.kitchen_lights
      service: switch.turn_on
    - delay: 00:00:01
    - service: script.timer_kitchen
      data:
        delay: "00:00:30"

scripts.yaml

timer_kitchen:
  alias: Timer for kitchen lights
  sequence:
    - delay: "{{ delay }}"
    - service: switch.turn_off
      entity_id: switch.kitchen_lights

Script for the turn off service is the wrong entity id, should be script.timer_kitchen

Thanks petro for pointing me in the right direction. When checking Trace Timeline I saw that I was also getting an error saying “Stopped because only a single execution is allowed”. What I did was adding “mode: restart” to the end of the automation. So my final code looks like this:

automations.yaml

- alias: Kitchen lights automation
  trigger:
    - platform: state
      from: "off"
      entity_id: binary_sensor.kitchen_motion_detected
      to: "on"
  action:
    - service: script.turn_off
      entity_id: script.timer_kitchen
    - entity_id: switch.kitchen_lights
      service: switch.turn_on
    - delay: 00:00:01
    - service: script.timer_kitchen
      data:
        delay: "00:00:30"
  mode: restart

scripts.yaml

timer_kitchen:
  alias: Timer for kitchen lights
  sequence:
    - delay: "{{ delay }}"
    - service: switch.turn_off
      entity_id: switch.kitchen_lights