v_amish
(amish)
May 10, 2022, 4:20pm
1
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
m0wlheld
(Christoph Dahlen)
May 10, 2022, 4:25pm
2
1 Like
francisp
(Francis)
May 10, 2022, 4:46pm
3
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:
opened 12:10PM - 26 Apr 22 UTC
integration: sun
### The problem
The Sun integration has two conflicting conditions for night.… As seen here:
![Screenshot 2022-04-26 at 22-00-20 Conditions](https://user-images.githubusercontent.com/10679300/165295450-9ac229e9-26cc-4486-b5cc-8a368953b91d.png)
The first condition
```
condition:
- condition: sun
after: sunset
before: sunrise
```
always returns false.
The second condition:
```
condition:
condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
```
Does return true at night.
I attempted to remove the first condition from the documentation in a PR but was told by Frenk that if the first condition is not returning true at night it is an issue with the sun integration. See: https://github.com/home-assistant/home-assistant.io/pull/22486
### What version of Home Assistant Core has the issue?
core-2022.4.7
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant OS
### Integration causing the issue
Sun
### Link to integration documentation on our website
https://www.home-assistant.io/docs/scripts/conditions/#sunsetsunrise-condition
### Diagnostics information
_No response_
### Example YAML snippet
```yaml
- id: 52289855-75f5-437f-a28c-933e23446226
alias: 'Test Sun Condition' ### Does not operate as expected, input number is not incremented at night ###
trigger:
platform: time_pattern
minutes: "/1"
condition:
condition: sun
before: sunrise
after: sunset
action:
- service: input_number.increment
target:
entity_id: input_number.test
- id: 332125de-61c8-4c81-8c4c-3d0c4389617d
alias: 'Test Sun OR Condition' ### Operates as expected, increments input number at night ###
trigger:
platform: time_pattern
minutes: "/1"
condition:
condition: or
conditions:
- condition: sun
before: sunrise
- condition: sun
after: sunset
action:
- service: input_number.increment
target:
entity_id: input_number.test_or
```
### Anything in the logs that might be useful for us?
_No response_
### Additional information
_No response_
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
Tinkerer
(aka DubhAd on GitHub)
May 10, 2022, 5:34pm
5
Personally I’d use the much simpler state
condition on sun.sun
being either above_horizon
or below_horizon
2 Likes
v_amish
(amish)
May 10, 2022, 6:12pm
6
The above quote is from that very page!
v_amish
(amish)
May 10, 2022, 6:14pm
7
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.