Correct condition using sun?

HI!

I have an automation I want to run in the mornings at 07:00 but only if it is somewhere between 1 h. before sunrise to 1 h. after sunrise.

Would this condition work?

  initial_state: True
  trigger:
    platform: time
    at: '07:00:00'
  condition:
    - condition: or
      conditions:
      - condition: sun
        before: sunrise
        before_offset: "-1:00:00"
      - condition: sun
        after: sunrise
        after_offset: "1:00:00"
  action: 
    service: homeassistant.turn_on
    entity_id: group.window_lights```

Will this trigger automation when the time is 07:00 and +- 1 h. to sunrise?

I think you would want -condition: and instead of OR.

Other wise I think it would work.

1 Like

I can’t get this to work. The automation doesn’t trigger, even though sunrise is ~06:50. Can someone see what I’m doing wrong with this automation?

  initial_state: True
  trigger:
    platform: time
    at: '07:00:00'
  condition:
    condition: and
    conditions:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: state
      entity_id: input_select.presence_mode
      state: 'Home'
    - condition: state
      entity_id: input_boolean.vacation_switch
      state: 'off'
    - condition: sun
      before: sunrise
      before_offset: "-1:00:00"
    - condition: sun
      after: sunrise
      after_offset: "1:00:00"
  action: 
  - service: homeassistant.turn_on
    entity_id: group.window_lights
  - service: notify.notifications_ios
    data: 
      message: 'Tänder belysning!"

Are you sure you have your offsets correct?

Shouldn’t:

- condition: sun
      before: sunrise
      before_offset: "-1:00:00"
    - condition: sun
      after: sunrise
      after_offset: "1:00:00"

Be like this:

- condition: sun
      before: sunrise
      before_offset: "1:00:00"
    - condition: sun
      after: sunrise
      after_offset: "-1:00:00"

Hmm… I can’t find any good description of the different variables used together with sun in conditions. I’ll try your suggestion!

I struggled with this while trying to create an automation condition based on time. I couldn’t figure out how the offsets behaved. I ended up using the solar angle attribute of the sun component.

You can see my condition here. I think ~15 degrees is an hour