Sunset/sunrise condition does not work when configured together

Hi.

I want to turn on ambient light in the hall when someone enters it and then turn it off automatically again. That works fine - I have a motion sensor that detects move and a light that is turned on/off based on that movement info. So far so good. But I wanted to add a condition to turn on the light only during when (called “when dark” in the docs). So I created a condition and selected both before sunrise and after sunset. I believe this is the right way. It does not work though.

And yaml looks like this:

condition: sun
after: sunset
before: sunrise

With this condition the light is not turned on automatically even after sunset - I have another automation that use sunset as a trigger and it was already triggered today so I am sure that HA knows that now it is dark outside. It works only if I select just after sunset, then it works fine. But this setting turns the light on only until midnight.

What is wrong with my config or understanding this sunset/sunrise condition? Can someone maybe please test that it really works for you as expected? Because to me it seems like it might be a bug.

HAOS 6.6
supervisor-2021.10.8
core-2021.11.5

Try this instead:

condition: state
entity_id: sun.sun
state: below_horizon
2 Likes

the docs say that it’s supposed to work that way but there have been lots of people who never get it to work.

so either do it as @123 says above or use both conditions as an ‘or’ instead:

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

Do the docs say it is supposed to work? Can you point out where?

As @123 pointed out in a similar topic, the docs state sunrise and sunset need to be specified in two separate conditions under an or condition, as you have written above.

From the docs:

Therefore, to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or.

well, isn’t that interesting.

it used to work as it is now but at some point I was sure the docs were changed to say that if you used both before and after in the same condition it would then span midnight.

it was either changed or i was completely misremembering. My bet would be on the latter, tho. :stuck_out_tongue:

EDIT: nevermind. I was thinking of the time condition which if you use both before and after then it spans midnight. confusing that they work differently tho.

So yeah, the latter… :laughing:

1 Like

It is here Conditions - Home Assistant (home-assistant.io)
And see the second example


So it is still in the docs :slight_smile:

But anyway changed to

condition: state
entity_id: sun.sun
state: below_horizon

and works like a charm. Thanks a lot!

2 Likes

I knew it was in there somewhere!

Thanks for making me not look (more) senile! :laughing:

2 Likes

Here’s an important sentence in the documentation:

image

then it provides an example for logically ORing the two states (sunrise/sunset).

true but both things are included in the docs and they are contradictory or at minimum confusing:

they seem to be saying that it will work in the first example above then later saying it won’t work and you have to use ‘or’.

it’s mostly understandable if you already know it but if you don’t then it is very misleading.

and it makes it more confusing since you actually can use “after: sunrise” and “before: sunset” in the same condition since they are not logically contradictory since they don’t span midnight.

I agree. It amounts to:

Like this.
Like that.
Not.

“Not” what? The last or both examples? :thinking: :man_shrugging:


Full disclosure: I don’t use the Sunrise/Sunset Condition so I never bothered to confirm if both examples represent “counter-examples” (as in “don’t do this”) or just the second one.

Somebody ought to confirm what’s what and then update the docs … but I’m not volunteering for it (too much on my plate at the moment).

2 Likes