Trigger at time but only if before sunrise

Gday, Ive been woring on getting out light to turn on each morning but only if the trigger time at least 30 mins before sunrise. The result of my code below is that the light comes on at 5:45 no matter what (sunrise is 6:03 am at the moment. Any help appreciated.

- id: 'weekday_bedroom_light'
  alias: Weekday Light Before Sunrise
  trigger: 
    platform: time
    at: '05:45:00'
  condition:
    - condition: sun
      before: sunrise
      before_offset: '00:30:00'
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    service: light.turn_on
    entity_id: light.bedroom

The conditions are combined using OR unless you specify AND.that means tour trigger always fires at the specified time on weekdays.

Use this construct:

    Condition: and
    Conditions:
      Condition:
        Platform: sun
        ...
      Condition:
        Platform: time

Hmmm, not according to this page: https://www.home-assistant.io/docs/scripts/conditions/

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

Actually the default is AND.

@cnschulz, I think the issue is your before_offset is wrong. It should be:

      before_offset: '-00:30:00'

The way you had it it had to be before 30 minutes after sunrise.

@pnbruckner ā€œbefore 30 minutes after sunriseā€ - you broke my brain. :wink: Ill change that setting and see how we go in the morning. Thanks.

Yeah, it is confusing. The ā€œbeforeā€ in before_offset just means it goes with the before option. Or to put that another way, it means the trigger has to happen before the desired time. The desired time is the event (sunrise or sunset) plus the offset. Positive offsets mean after the event, and negative offsets means before the event. So, you want the trigger to let the action run only if it happens before the desired time. So you use before: and before_offset:. But you also want the desired time to be before the event (in this case sunrise), so the offset has to be negative. So, before: sunrise and before_offset: '-00:30:00' means the trigger has to happen before 30 minutes before sunrise.

2 Likes

My bad, I was thinking about triggers.

1 Like