Condition 'sun' is always false

I have an automation that should work only after sunset and before the sunrise. I used the following condition from the example but it is always false.

  - condition: sun
    after: sunset
    before: sunrise

From [Conditions - Home Assistant]
This is ‘when dark’ - equivalent to a state condition on sun.sun of below_horizon:

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

Read the docs: Conditions - Home Assistant

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

There have been at least two PR’s to remove that confusing section from the docs, and both were rejected. Apparently, the fact that the ‘when dark’ configuration example you have cited doesn’t work is “a bug”. There is an open issue on github:

For now you must use an Or condition:

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

Alternatively, you could use the elevation attribute or the sun2 integration.

2 Likes

Personally I’d use the much simpler state condition on sun.sun being either above_horizon or below_horizon

2 Likes

The above quote is from that very page!

This causes a lot of confusion, sometime you have to debug someone’s issue who have meticulously followed the examples.

I ended up using the double sun condition and now its working for me.