Garage Lights after Dark 2.0

Here is my code for making my House Garage Door turn the garage lights on and off when I open and close the door from the garage to the house.

The Close part of it works, and the open part did work until I switched it to only be 15 minutes before sunset and while its dark.

- id: '1592264830396'
  alias: HGD Open
  description: ''
  trigger:
  - entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_9ef73602_on_off
    platform: state
    to: 'on'
  - event: sunset
    offset: -00:15:00
    platform: sun
  condition:
  - condition: state
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_9ef73602_on_off
    state: 'on'
  - after: sunset
    after_offset: -00:15:00
    condition: sun
  action:
  - data: {}
    entity_id: switch.garage
    service: switch.turn_on
- id: '1592264922582'
  alias: HGD Close
  description: ''
  trigger:
  - device_id: 7b7b2f3a834e493fb503e80f490f89f8
    domain: binary_sensor
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_9ef73602_on_off
    platform: device
    type: not_opened
  condition: []
  action:
  - device_id: bcd5830564b84a25814151cef5d83a29
    domain: switch
    entity_id: switch.garage
    type: turn_off
1 Like

Is there a question here somewhere? (besides mine… :wink:)

Whats wrong in my above code that it wouldn’t turn on during sunset and after dark?

In lazy pseudo-code, I’m reading your first automation as:
When (trigger) the light sensor turns on, OR it is exactly 15 min before sunset, then (conditions), if the light is on AND it is exactly 15 min before sunset, then (actions) turn on the garage light.

I’m guessing you want to say:
When I open the door (trigger), then, if it is between 15 min before sunset AND sunrise the next day (conditions), then (actions) turn on the light.

Also, there are some important pointers in the docs that would help you:

Unlike a trigger, which is always or , conditions are and by default - all conditions have to be true.

Note that if only before key is used, the condition will be true from midnight until sunrise/sunset. If only after key is used, the condition will be true from sunset/sunrise until midnight . Therefore, to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or .

So what would my code need to look like to account for this change?

The examples always show the offset as a string.

- event: sunset
    offset: "-00:15:00"
    platform: sun
- after: sunset
    after_offset: "-00:15:00"
    condition: sun


I believe the following single automation meets your requirements. It is triggered by the binary_sensor and will turn your garage light on/off when the garage door opens/closes but only between sunset (-15 minutes) and sunrise.

- id: '1592264830396'
  alias: HGD Open-Close
  trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_9ef73602_on_off
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:15:00'
      - condition: sun
        before: sunrise
  action:
  - service_template: 'switch.turn_{{trigger.to_state.state}}'
    entity_id: switch.garage

Using the automtions builder GUI, I don’t see an option for “OR” conditions…

The Automation Editor has several limitations and you just encountered one of them.

To use the automation I suggested, you will need to create it using a text editor and save it to the automations.yaml file.

–
EDIT
The purpose of having two sun-related conditions logically ORed is explained in the documentation.

Note that if only before key is used, the condition will be true from midnight until sunrise/sunset. If only after key is used, the condition will be true from sunset/sunrise until midnight . Therefore, to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or .

Sorry, I misread something in your original post. I thought that sensor was a light sensor and not a magnetic sensor, so I was confused, because I didn’t see where you control your door. That’s why I posted the textual description to hear if I’m understanding correctly. I think 123 has given a good answer.