Automation for turning on a light when door opens, but turning off when motion stops and door closed

Typical Garage door light turn on/off automation, Does my logic look right. I want to turn on the light as soon as I open the door, then I want to keep the light on if the door is open OR if the door is closed and there is motion. Most the time I walk out to the garage, grab something ,and then go back in to the house. But if I’m working in the garage with the door closed, I want the lights to stay on. I’m still learning, but… I think this should work? Am I missing anything or is there a better way to do this.
binary_sensor.centralite_3320l_0cceb5d4_1_1280 is open close
binary_sensor.centralite_3305s_02f13445_1_1280 is motion

- alias: Turn on Garage Light and then off with no motion and door closed
  trigger:
  - platform: state  
    entity_id: binary_sensor.centralite_3320l_0cceb5d4_1_1280
    from: 'off'
    to: 'on'
   - platform: state  
    entity_id: binary_sensor.centralite_3305s_02f13445_1_1280
    from: 'off'
    to: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.switch_4
    - condition:
        condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.centralite_3305s_02f13445_1_1280
            to: 'off'
            for:
            minutes: 2
          - condition: state
            entity_id: binary_sensor.centralite_3320l_0cceb5d4_1_1280
            to: 'off'
    - service: switch.turn_off
      entity_id: switch.switch_4  

I would make two seperate automations… one for the turning on… and one for the turning off and have the condition of motion detected For the off automation

1 Like

Separation just as @Bartem is suggesting is the best way to go.

automation:
  - alias: 'Motion Garage Light - Turn on Light'
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.centralite_3320l_0cceb5d4_1_1280
        to: 'on'
        from: 'off'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.switch_4

  - alias: 'Motion Garage Light - Turn off with no motion and door closed'
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.centralite_3320l_0cceb5d4_1_1280
        to: 'off'
        from: 'on'
        for:
          minutes: 2
    condition:
      - condition: state
        entity_id: binary_sensor.centralite_3320l_0cceb5d4_1_1280
        state: 'off'
    action:
      - service: homeassistant.turn_off
        entity_id: switch.switch_4

Haven’t tested it in a live environment but the logic the same as I use myself.

1 Like

I have a similar situation, but my problem is a little unique. I have two automations. I open my basement door and the basement lights turn on. No problem. When no motion is detected for 10 minutes, turn off the lights. Also no problem.

My problem is the lights turning off when there was never any motion to begin with. It’s common for us to open the basement door to grab something at the top of the stairs, then close the door. The lights turn on but the automation to turn off the lights is set to trigger when the motion is a 0 for 10 minutes. This won’t actually trigger until someone goes down there.

This is what I use now, You should be able to tweak it for your needs. Basically If I open the garage and close it and no motion was detected the lights turn off right away. You may need to adjust the times based on how far your motion sensor is from your open close sensor

- alias: 'Motion Lighting Garage turn light on'
  trigger:
  - platform: state
    entity_id: binary_sensor.open_close_sensor_garage_door
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_sensor_garage_motion
    from: 'off'
    to: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.garage_light_indoor

- alias: 'Motion Lighting Garage turn light off'
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_garage_motion
    from: 'on'
    to: 'off'
    for:
      minutes: 1
  - platform: state
    entity_id: binary_sensor.open_close_sensor_garage_door
    from: 'on'
    to: 'off'
    for:
      seconds: 5
  condition:
  - condition: state
    entity_id: binary_sensor.motion_sensor_garage_motion
    state: 'off'
    for:
      minutes: 1
  action:
  - service: switch.turn_off
    entity_id: switch.garage_light_indoor
1 Like

Yup, this is what worked for me. Just needed your sample code to wrap my head around how this needed to work and I’m good to go. The key for me was having multiple automations. One automation to turn the lights off after 10 minutes of no motion. Then another automation to turn the lights off when the basement door shuts IF there hasn’t been any motion in the last minute.

edit: I lied. Apparently I had a couple cocktails and didn’t remember how I did this. Here’s the code below that worked for me:

alias: Garage - Turn off lights
  trigger:
  - entity_id: sensor.basement_multisensor_burglar
    for: 00:10
    from: '8'
    platform: state
    to: '0'
  - entity_id: binary_sensor.basement_door_sensor
    platform: state
    to: 'off'
  condition: []
  action:
  - delay: 00:01
  - condition: state
    entity_id: sensor.basement_multisensor_burglar
    state: '0'
    for: 00:01
  - data:
      entity_id: switch.workbench_lights
    service: homeassistant.turn_off
  - data:
      entity_id: switch.garage_lights
    service: homeassistant.turn_off

So the automation is triggered if no motion is detected for 10 (+1) minutes. OR… if the basement door is closed, the automation will wait 1 minute and if no motion detected kill the lights.

1 Like