Yet another.. Lights On Between Sunset and Sunrise

Overview:
If the automation turns the lights on… the automation is free to turn the lights off… But If I manually turn the lights on then the automation is shunted. I’m using a boolean flag set in the “On Automation” to tell the “Off Automation” whether the current state was set by an automation.

In this snippet… this is the “On Automation”…

When my motion sensor sees movement
And
The East light is off
And
Its between Sunrise and Sunset
turn my two lights on and set the boolean flag

It works without the sunset check so I know where the problem is… but I cant make HA happy
With respect to the offset times I’ve tried 00:00:00 and 00:00:01… and several more

Below is HA generated YAML

- id: East_Patio_Lights_On
  alias: East Patio Lights On
  trigger:
  - entity_id: binary_sensor.east_patio_motion_detector_sensor
    platform: state
    to: 'on'
  condition:
  - condition: device
    type: is_off
    device_id: 6f52a149b901d5d60b44ba94f2796a40
    entity_id: switch.east_patio_east_light
    domain: switch
  - condition: and
    conditions:
    - condition: sun
      before: sunrise
      after: sunset
      after_offset: -00:30
      before_offset: -00:30
  action:
  - data:
      entity_id: switch.east_patio_east_light
    service: switch.turn_on
  - data:
      entity_id: switch.east_patio_west_light
    service: switch.turn_on
  - data: {}
    entity_id: input_boolean.east_patio_automation_started
    service: input_boolean.turn_on
  mode: single

According to the sunset/sunrise 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.

condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise

Thank you sir…
I discovered that after more experimentation.

The UI tool allows you to enter BOTH which results in…

 conditions:
    - condition: sun
      before: sunrise
      after: sunset
      after_offset: -00:30
      before_offset: -00:30

as one condition without throwing an error.

Each check sunrise and sunset is a SEPARATE condition!

Thanks again!

1 Like