Automation turn off lights when no motion AND contact sensor time period

I’m pretty new to home assistant and trying to figure this one one. I’ve got a light switch in a garage with both a motion sensor, and contact sensor on the door.

I want to turn off the garage lights when both conditions are true; no motion has been detected for 5 minutes AND the contact sensor has been closed for 5 minutes. The issue is we sometimes don’t go into the side of the garage where the motion sensor is and vice versa with using the door that has the contact sensor on it. Both of them would need to be true before turning the light off.

I tried putting the trigger as the garage lights turned on and both conditions for the motion and contact but that wasn’t doing it. Below is what I currently have going. Maybe I need to duplicate it in a second automation and flip around the trigger and condition to satisfy both? I do plan on having a second contact sensor in the garage for a different door so that likely wouldn’t work when a third is added.

alias: 'Garage Light - Turn off with no motion and door closed for 5 minutes'
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.motion_sensor_garage_occupancy
      to: 'off'
      from: 'on'
      for:
        minutes: 5
  condition:
    - condition: state
      entity_id: binary_sensor.door_sensor_garage_contact
      state: 'off'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.garage_lights
alias: 'Garage Light - Turn off with no motion and door closed for 5 minutes'
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.motion_sensor_garage_occupancy
      to: 'off'
      from: 'on'
      for:
        minutes: 5
    - platform: state
      entity_id: binary_sensor.door_sensor_garage_contact
      state: 'off'
  condition:
    - condition: state
      entity_id: binary_sensor.door_sensor_garage_contact
      state: 'off'
    - condition: state
      entity_id:
        - binary_sensor.motion_sensor_garage_occupancy
      state: 'off'
      for:
        minutes: 5
  action:
    - service: homeassistant.turn_off
      entity_id: switch.garage_lights
1 Like

Awesome, thank you let me give this a try! How would you add a third contact sensor to this where all three would have met the condition of no motion for 5 minutes and both contact sensors closed for 5 minutes as well?

Just add them to the trigger and the condition block. It’s just one more state trigger, and one more state condition.

If you’re writing them purely in YAML you can shorten that a little, but there’s no need to.

    - condition: state
      entity_id: binary_sensor.door_sensor_garage_contact
      state: 'off'
    - condition: state
      entity_id: binary_sensor.trapdoor_sensor_garage_contact
      state: 'off'

is the same as

    - condition: state
      entity_id: 
        - binary_sensor.door_sensor_garage_contact
        - binary_sensor.trapdoor_sensor_garage_contact
      state: 'off'