Sun condition

Hi All,

I wondering who can tell me how this works. I have many scenes for my house. It use the conditions with the sun. But its a strange behaviour. I have it work now but can someone tell me the difference?

This work only when sun is above horizon.

  condition:
    - condition: and
      conditions:
        - condition: sun
          after: sunrise
        - condition: sun
          before: sunset

When I change this to:

  condition:
    - condition: and
      conditions:
        - condition: sun
          after: sunset
        - condition: sun
          before: sunrise

there is nothing that trigger and will do the action when sun is below horizon.
When I change this to:

  condition:
    - condition: or
      conditions:
        - condition: sun
          after: sunset
        - condition: sun
          before: sunrise

the AND changed to OR then the trigger is working and automation is starting.
For me it make no sense… why is the daytime (first condition) working between sunrise AND sunset
but not between sunset AND sunrise. I need using sunset OR sunrise.

hope someone understand the question and can tell me how this works?

If you want daytime, why not just use:

condition:
  - condition: state
    entity_id: sun.sun
    state: 'above_horizon'

Use below_horizon for nighttime

1 Like

I created it first with the automation creator and give me this code.

I’ll try your solution :slight_smile: seems much easier to use!

There is no time in a single day (ie between midnight and the next midnight) that is both before sunrise and after sunset.

To expand a bit on what nick said…

The sunrise/sunset (and time before & after) are referenced to midnight.

after sunset means from sunset to midnight. before sunrise means from midnight to sunrise.

so you will never have any time that is both between sunset and midnight and also between midnight and sunrise at the same time. So you have to use an ‘or’ condition for the logic to work.

And like I said above the same goes for time values.

After 18:00 means between 18:00 and midnight. Before 06:00 means between midnight and 06:00.

^ what he said :slight_smile:

below_horizon and above_horizon are more compact than an or condition.

Interesting thing I have found… for maybe 5 minutes after the elevation goes below zero, the state stays as above_horizon…

For a short time, because of the way the logic works.

At zero the centre of the sun is in line with the horizon. So the sun itself is not yet below the horizon. If those moments matter, all you have to do is switch over to using the elevation.

4 Likes

Yeah… it doesn’t bother me. I hadn’t considered why it does it only that it does and it’s only a few minutes. Thanks for the explanation though… makes perfect sense.

1 Like

I had noticed it too and wondered, but didn’t wonder enough to ask :slight_smile:

1 Like