Garage lights automation

Here is my code that is currently working for following:

  1. Opening of any doors or windows leading to garage triggers the automation
  2. The two lights are turned on when automation is triggered
  3. Then monitor the inside movement using motion sensor on two cameras and two other sensors, keep the lights on if motion detected
  4. If no motion detected then wait 2 minutes
  5. Turn off the lights
alias: 01- Garage
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage01_door_sensor
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_a10
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.garagecover_state
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: input_boolean.garage_lights_automation
    state: "on"
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l1
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l2
    domain: light
  - wait_template: >-
      {{ is_state('sensor.garage01_occupancy_status', 'Clearance') or
      is_state('binary_sensor.garage_motion_hue_motion', 'off') or
      is_state('binary_sensor.amcrest_01_motion_alarm', 'off') or
      is_state('binary_sensor.amcrest_02_motion_alarm_motion_alarm', 'off')}}
    continue_on_timeout: true
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l1
    domain: light
  - type: turn_off
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l2
    domain: light
mode: single

The problem is that inside the garage if I stand still for few seconds then the automation moves from step-3 to step-4 and turns off the lights after 2 minutes even if I’m inside the garage and moving.

Is there a way to create a “while loop” for steps 3 & 4 or to send automation back to step-3 if movement is detected during time window of step-4?

I think it would be more simple to just use two automations - one for on and one for off - than to try to do it all in one and jump thru hoops (or loops :wink:) to get it to work.

I think your wait template needs to “and” all the individual motion sensor tests instead of "or"ing them. I.e.:

  - wait_template: >-
      {{ is_state('sensor.garage01_occupancy_status', 'Clearance') and
      is_state('binary_sensor.garage_motion_hue_motion', 'off') and
      is_state('binary_sensor.amcrest_01_motion_alarm', 'off') and
      is_state('binary_sensor.amcrest_02_motion_alarm_motion_alarm', 'off')}}

This will work, however, only if at least one of the sensors has detected motion immediately after the automation triggers. If not, then add a wait at the beginning until at least one is sensing motion. It will probably need a time out and a check afterwards that it completed (i.e., saw at least one sensor detecting motion.)

Hi Phil,

Yes the template needs to be “and” and not “or”, thanks for the catch! I modified the code:

alias: 01- Garage
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage01_door_sensor
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.window_a10
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.garagecover_state
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: input_boolean.garage_lights_automation
    state: "on"
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l1
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l2
    domain: light
  - wait_template: >-
      {{ is_state('sensor.garage01_occupancy_status', 'Clearance') and
      is_state('binary_sensor.garage_motion_hue_motion', 'off') and
      is_state('binary_sensor.amcrest_01_motion_alarm', 'off') and
      is_state('binary_sensor.amcrest_02_motion_alarm_motion_alarm', 'off')}}
    continue_on_timeout: true
    timeout: "120"
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - type: turn_off
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l1
    domain: light
  - type: turn_off
    device_id: d6349d6f6e2950d8088f019a5144ffee
    entity_id: light.t2_l2
    domain: light
mode: single

I went inside the garage and everything worked, I barely stopped pacing for two seconds and guess the wait_template tested “true” and lights went off after the specified time delay.

1 Like

This looks like the same thing I’m trying to accomplish! Please help me understand what the Condition does…

condition:

  • condition: state
    entity_id: input_boolean.garage_lights_automation
    state: “on”

I’ve created an input boolean that acts like a kill switch to easily enable or disable automation functionality. For example, if I am doing some work in garage that will take over an hour then I would not want the lights to turn off as per this automation.

So in the above case, I’d toggle the switch to off and the automation will not operate garage lights. But in this case I’ll have to operate lights manually.

This is how my UI looks like:

garage

So all the family members can enable/disable the functionality of this automation without needing admin privileges.

Kill switches like these are very handy when you need to suspend the functionality of an automation temporarily.

Very interesting! Thanks for the explanation.
I use an Aqara presence sensor in the garage for the same reason because I’m too lazy to toggle the automation. :O)

Why do you have a 1 second delay before turning the lights on?

Just late enough for me to watch the second light come on when I step into the garage and admire my automation :sunglasses:

1 Like

Thanks again for the help. My garage light automation is now working flawlessly! I have been battling this forever.
I wasn’t having any luck with the wait template so I used an if/then instead.
My automation includes a presence sensor which helps when I am working in a stationary position but my “kill switch” is the shop lights. If they are on that means I turned them on manually and that keeps the other lights on.

alias: Lights Shop NEW
description: “”
trigger:

  • alias: When ANY shop group sensor changes to ON
    platform: state
    entity_id:
    • binary_sensor.all_shop_sensors
      to: null
  • platform: device
    type: changed_states
    device_id: fd1aec9ed8569e3494b0e695842a321c
    entity_id: 3665fec6053b856b27aa416edaa689f8
    domain: switch
    condition:
    action:
  • type: turn_on
    device_id: 68bc1b1e83f132a36d822bbc27fb6173
    entity_id: 3e207355df73c11da5a148c045abf3f5
    domain: switch
  • delay:
    hours: 0
    minutes: 0
    seconds: 30
    milliseconds: 0
  • alias: If all shop sensors and shop lights are off, turn off the bench lights
    if:
    • condition: template
      value_template: |-
      {{ is_state(‘binary_sensor.all_shop_sensors’, ‘off’) and
      is_state(‘switch.lights_shop’, ‘off’)}}
      then:
    • type: turn_off
      device_id: 68bc1b1e83f132a36d822bbc27fb6173
      entity_id: 3e207355df73c11da5a148c045abf3f5
      domain: switch
      mode: single