Turn on lights as sunset, but turn off temporarily if garage door is open

I have some Christmas lights connected to a smart plug (switch.christmas_lights). I’d like them to turn on at sunset and turn off at sunrise.

However, if at any time between sunset and sunrise the garage door (binary_sensor.garage_door) opens I want to turn the smart plug off, but turn it back on when the garage door closes.

I tried searching through a few posts such as HA and turning lights on when a door is opened, and Lights Before Dark and Garage Open but couldn’t really wrap my head around applying them to my situation.

Using 0.118.5.

Thank you!

I’d do two automations, 1 for turning on the lights and one for cycling with the door.

Door automation triggers when the door is opened/closed and condition on lights being on (or sunset to sunrise) and a choose options for turning the lights on or off depending on open/closed

I would split it to two automations: One for the sunset/sunrise and second for garage door:

  - alias: 'Switch light based on sun'
    trigger:
      - platform: sun
        event: sunrise
      - platform: sun
        event: sunset
    action:
      - service_template: >
          {% (if trigger.event == 'sunset') %}
            switch.turn_on
          {% else %}
            switch.turn_off
          {% endif %}
        entity_id: switch.christmas_lights
  - alias: 'Switch light based on garage door'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_door
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
      - service_template: >
          {% if trigger.to_state.state == 'on' %}
            switch.turn_off
          {% else %}
            switch.turn_on
          {% endif %}
        entity_id: switch.christmas_lights

In situations like this (where I want something on or off based on a number of environmental factors) all I do is create a binary sensor to monitor the factors, and then mirror the state of the sensor to the device in an automation.

In this case…

binary_sensor:
  - platform: template
    sensors:
      outside_light_control:
        value_template: >
          {{ is_state('binary_sensor.garage_door', 'off') and
             is_state('sun.sun', 'below_horizon') }}

automation:
  trigger:
    platform: state
    entity_id: binary_sensor.outside_light_control
  action:
    service: "switch.turn_{{ trigger.to_state.state }}"
    entity_id: switch.christmas_lights

@haaghala - Thank you! That definitely helps!

@anon43302295 - The trick of creating a binary_sensor based on multiple states is pretty slick. Thank you so much!

1 Like