Hello everyone,
I have an automation where I only want it to run at night, after sunset, and before the sunrise. I set it up with an OR and it still says the condition was not valid.
Does this look correct to everyone or did I do something wrong?
Here is my automation that shuts turns on my shelly for 10 mins with an MQTT event.
- id: '1649213236173'
alias: Back Porch Light Person Auto
description: ''
trigger:
- platform: mqtt
topic: frigate/back_alley/person
condition:
- condition: or
conditions:
- condition: sun
before: sunrise
before_offset: 00:30:00
after: sunset
after_offset: 01:00:00
action:
- type: turn_on
device_id: 7e44d09d6956eae7188f98c77e3130c5
entity_id: switch.shelly1_98f4abf2a6b6
domain: switch
- delay:
hours: 0
minutes: 10
seconds: 0
milliseconds: 0
- type: turn_off
device_id: 7e44d09d6956eae7188f98c77e3130c5
entity_id: switch.shelly1_98f4abf2a6b6
domain: switch
mode: single
You need to split the Sun condition for the Or to have any effect:
condition:
- condition: or
conditions:
- condition: sun
before: sunrise
before_offset: 00:30:00
- condition: sun
after: sunset
after_offset: 01:00:00
tom_l
May 5, 2022, 2:24pm
3
Apparently there is a bug with this condition and it was intended that this should be true at night (it is currently not):
condition:
- condition: sun
after: sunset
before: sunrise
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_
1 Like
2 other ways to tackle it.
condition:
condition: state
entity_id: sun.sun
state: "below_horizon"
or this, which you can tweak more with the sun angle in the sky. Works similar to using offset.
condition:
condition: template
value_template: '{{ state_attr("sun.sun", "elevation") < -5 }}'
1 Like
WallyR
(Wally)
May 5, 2022, 3:53pm
5
There is another way.
I use an addon called sun2 (GitHub - pnbruckner/ha-sun2: Home Assistant Sun2 Sensor ) which provide one sensor for sunrises and one sensor for sunsets.
It is so much easier to handle and it have every thinkable value available in relation to the sun.
I have just chosen a few values to monitor and here are an example of my sensors.
[sensor.copenhagen_sunrise]
Copenhagen Sunrise 2022-05-05T05:18:37.575909+02:00
yesterday: 2022-05-04T05:20:46.012114+02:00
today: 2022-05-05T05:18:37.575909+02:00
tomorrow: 2022-05-06T05:16:30.697320+02:00
device_class: timestamp
icon: mdi:weather-sunset-up
friendly_name: Copenhagen Sunrise
[sensor.copenhagen_sunset]
Copenhagen Sunset 2022-05-05T20:55:39.939221+02:00
yesterday: 2022-05-04T20:53:42.159402+02:00
today: 2022-05-05T20:55:39.939221+02:00
tomorrow: 2022-05-06T20:57:37.237827+02:00
device_class: timestamp
icon: mdi:weather-sunset-down
friendly_name: Copenhagen Sunset
Thanks everyone I will look at your examples and put something together and test it this weekend.
@ kanga_who does “elevation” come default with the sun property? How would I know what elevation is for after sunset and for before sunrise?
1 Like
tom_l
May 6, 2022, 2:13pm
7
Elevation is an attribute of the sun.sun
entity you don’t need to do anything.
The sun moves at 15° per hour and is at 0° elevation at sunrise and sunset.
e.g. -15
is one hour after sunset, and 7.5
is 30 minutes after sunrise.
1 Like
Thank you! thats exactly what i was looking for.