Need help with conditions in automation (mixing AND /OR)

Hello, I’d like to active an automation this is condition:

“(If light1 is off AND light2 is off)
AND
(if sun is 30min before sunrise OR sun if 30 min after sunset)”
then…

To be clear: If sunrise is at 6:00AM and sunset is at 6:00PM, I’d like to include the time range
[5:30 AM - 18:30 PM]

  condition:
      condition: and
      conditions:
        - condition: state
          entity_id: light.luce_sala_1
          state: 'off'
        - condition: state
          entity_id: light.luce_sala_2
          state: 'off'
        - condition: or
          conditions:
            - condition: sun
              before: sunrise
              before_offset : "-00:30:00"
            - condition: sun
              after: sunset
              after_offset : "-00:30:00"

Is this correct ?

Thanks!

Not sure if I understand this correctly. Do you want the condition to be true from 30 min before sunrise to 30 min after sunset or should it be false during this time?
What you currently have will be true from 30 min before sunset until 30 min before the sun rises.
No matter which way you want, you need to remove the space between after_offset and : and between before_offset and :

logic looks good, unsure about sunset, is this meant to be a negative for what you are trying to achieve, I suspect it should be 00:30:00?

@Burningstone
The condition must cover “dark hours” (between sunset and sunrise) plus 30 minutes in the beginnig, plus 30 minutes at the end:

[6:00PM - 30 min -----> 6:00AM + 30 min ] = [5:30PM —> 6:30AM]

So I think is wrong: I 'd like to have 30 min AFTER the sunrise!
Not sure that spaces matter but I deleted them!

@johnw230873
That’s the problem (aside the and/or logic)!
The image:
https://www.home-assistant.io/images/docs/scripts/sun-conditions.svg
located at:
https://www.home-assistant.io/docs/scripts/conditions/
doesn’t cover this case.

I guess I have to try it for real,tonight, watching the clock! :laughing:

I’ll be interested in the results as they were the graphs I was going off and read you need to add a positive number .

They do matter!

Just change the after_offset from -00:30:00 to 00:30:00 like this:

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

This will be true from 30 min after sunset until 30 min before sunrise.

Thanks @Burningstone: done!
I hope to give it a try tonight.

Your other option is using the elevation attribute of sun.sun, and a simple numeric state condition.

If you check just now what 30 minutes after sunset is in elevation terms, then that light level will be consistent through the year (the time offset won’t), and your condition becomes a much simpler:

        - condition: state
          entity_id: light.luce_sala_1
          state: 'off'
        - condition: state
          entity_id: light.luce_sala_2
          state: 'off'
        - condition: numeric_state
          entity_id: sun.sun
          below: -1
          value_template: "{{ state_attr('sun.sun','elevation') }}"
2 Likes

Thanks @Tinkerer, indeed I have looked into elevation attribute but I have to say I didn’t understand it! :innocent:
I already modified the automation using sunrise/sunset in order to be indipendent from the time.

I can try an automation, just to check that “-1” really means, in terms of elevation.

I agree with Tinkerer,
Unless you actually want sunset or sunrise the ‘time offsets’ are often (when guestimating light level) highly variable (and the degree depends on your latitude).
1 in this context is 1°
Solar angle is a much better indicator
But there are a couple of things to bear in mind, the sun doesn’t ‘set’ until the top is seen to be level with the horizon and it hasn’t risen until the top is seen to be level with the horizon.
I say “seen to be” for both as the atmospheric lens effect alters the perceived position of the sun which at these times sums and equate to a - 0.86° offset.
Civil dawn is - 6 degrees and gives a generally accepted walking level of light but you should suck it and see

2 Likes