Automation help, sunrise and sunset

Hi,
I would like to make an automation that opens/closes our covers for the windows with following rules :

  • Open the covers at sunrise but no earlier than 6:15
  • Close the covers at sunset but no later than 21:00

How would an automation look that would satisfy those condition ?

/donnib

Examples of sun-related automations can be found in the documentation:

I did see the examples and i know how to use it, my problem is more how to fullfill the requirement Open the covers at sunrise but no earlier than 6:15, i know how to make it open at sunrise but not together with the time in the way described.

Post the code you’ve created and we can help you achieve your goal.

I suggest brushing up on conditions. This automation is probably one of the most basic automations out there and there are examples everywhere.

- alias: Open cover on sunrise
  trigger:
    - platform: sun
      event: sunrise
  condition:
    - condition: time
      after: '06:15:00'
  action:
    - service: open_cover
      data:
        entity_id: cover.mycoverx
- alias: close cover at sunset
  trigger:
    - platform: sun
      event: sunset
  condition:
    - condition: time
      before: '21:00:00'
  action:
    - service: open_cover
      data:
        entity_id: cover.mycoverx

Now it gets complicated if you still want the covers to open those days.

- alias: Open cover on sunrise
  trigger:
    - platform: sun
      event: sunrise
    - platform: time
      at: '06:15:00'
  condition:
    - condition: time
      after: '06:16:00'
    - condition: state
      entity_id: cover.mycoverx
      state: 'closed'
  action:
    - service: open_cover
      data:
        entity_id: cover.mycoverx
- alias: close cover at sunset
  trigger:
    - platform: sun
      event: sunset
    - platform: time
      at: '21:00:00'
  condition:
    - condition: time
      before: '21:01:00'
    - condition: state
      entity_id: cover.mycoverx
      state: 'closed'
  action:
    - service: open_cover
      data:
        entity_id: cover.mycoverx

You may need to adjust the state condition for cover.mycoverx as I don’t know the states of covers because I don’t use them.

thank you for the reply so here is what i think it may solve the req. please correct :

- alias: Open cover on sunrise
  trigger:
    - platform: sun
      event: sunrise
    - platform: time
      at: '06:15:00'
  condition:
    - condition: time
      after: '06:15:00'
  action:
    - service: cover.open_cover
      entity_id: cover.kkken_rullegardiner

I have removed the condition on the state since the cover i have i can’t get the state so i am doing this blindly, if they are open already it’s fine, if not they will open. I am not sure why you set the time in the condition 1min later, is it because you think it may cause problems when it’s triggered at 6:15 then if the condition is the same it may fail ?

for the close i do the reverse :

- alias: Close cover on sunset
  trigger:
    - platform: sun
      event: sunset
    - platform: time
      at: '21:00:00'
  condition:
    - condition: time
      after: '21:01:00'
  action:
    - service: cover.close_cover
      entity_id: cover.kkken_rullegardiner

I guess the added minute here is required otherwise the condition would be false. Correct ?

I believe you’ll want this (which is similar to what I do in some of my automations):

- alias: Open cover on sunrise, or at 6:15, whichever comes last
  trigger:
    - platform: sun
      event: sunrise
    - platform: time
      at: '06:15:00'
  condition:
    - condition: sun
      after: sunrise
    - condition: time
      after: '06:15:00'
  action:
    - service: cover.open_cover
      entity_id: cover.kkken_rullegardiner

I think you want it to open at sunrise or 6:15, whichever comes last. Is that right? The automation above will trigger at both sunrise and 6:15, but the action will only run if, at that time, it’s both after sunrise and after 6:15. (Don’t worry, a condition that requires it to be “after” a particular time or event will be true when triggered by that exact time or event. At least, that has been my experience.) So the action will only run once, at sunrise or at 6:15, whichever comes last.

For closing, it’s similar, but I think you want sunset or 21:00, whichever comes first. Is that right? If so, it’s even simpler (you don’t need a condition, as long as calling the close service when the cover is already closed isn’t an issue):

- alias: Close cover on sunset, or at 21:00, whichever comes first
  trigger:
    - platform: sun
      event: sunset
    - platform: time
      at: '21:00:00'
  action:
    - service: cover.close_cover
      entity_id: cover.kkken_rullegardiner
2 Likes

Yeah, that’s a better way to handle it. My only concern is the trigger to condition delay. If it’s under half a second delay does it resolve to <=? what’s the resolution on time? I don’t use traditional automation in HA so you could probably answer that.

If you look at how the “after” sun and time conditions are processed in this code, you’ll see that if the time when the condition is processed is greater than or equal to the value specified in the condition, the condition will evaluate to True. So it doesn’t require any delay. As soon as the trigger occurs, the corresponding condition will be true.

ah and they use dt objects. Good to know

It’s exactly what i need, thanks you very much. :wink:

1 Like