Can't get action to be limited to between sunset and sunrise

Hi (again),

So I searched and found this thread that explains how to do a condition that limit the action to between sunset and sunrise. However, I can’t get it to work :frowning: The automation fires even in the middle of the day!

This is what I have:

- id: '1577892099604'
  alias: Extérieur - Ouverture de la porte patio la nuit
  description: ''
  trigger:
  - entity_id: binary_sensor.porte_arriere
    platform: state
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: light.lumierecours
      state: 'off'
    - condition: or
      conditions:
      - condition: sun
        after: sunset
        after_offset: 0:30:00
      - condition: sun
        before: sunrise
        before_offset: 0:30:00
  action:
  - entity_id: light.lumierecours
    service: light.turn_on
  - entity_id: timer.porte_cours
    service: timer.start

Do you see anything wrong compared to what was posted in that thread? I want the outside light to turn on if it’s off (so it doesn’t fire the timer and turned it off when it expires if the light was already on) and only from 30 minutes after sunset and 30 minutes before sunrise.

Thanks.

Try putting the offset times in quotes, like this:

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

You also need to change the before offset for sunrise to “-00:30:00” otherwise it will trigger be before 30 min after the sun rises.

I think @Burningstone is right. But if it does not fix your problem you can also try it with a
Times of the Day Binary Sensor You can replace your “or” condition with a simple if condition (value of sensor is true) to check if its time for your lights to go on.

Maybe something like this:

binary_sensor:
  - platform: tod
    name: dark-outside
    after: sunset
    after_offset: '00:30:00'
    before: sunrise
    before_offset: '-00:30:00'
1 Like

I’ll give it a try. thanks.

The +0:30:00 for sunset is what I want. When the sun sets, it’s still relatively well lit outside so I want the light to turn on half an hour after sunset.

I’ll give this a try too. Might actually be cleaner than the or/and combo.

Yes, the sunset one is correct, only the sunrise offset must be negative to achieve what you want.

True, I have it ok in my code, don’t know why it’s missing the ‘-’ here. I’ve probably copied it while it was wrong and corrected it after :slight_smile: