I was trying to test a very simple automation which just sent a notification upon sunrise/sunset, but the sun events are not aligning with the sun states.
My automation:
trigger:
- platform: sun
event: sunrise
- platform: sun
event: sunset
condition: []
action:
- service: notify.my_phone
data:
message: >-
The sun is {% if is_state('sun.sun', 'above_horizon') %}up{% else
%}down{% endif %}!
Checking the history for sun.sun vs the automation traces, I can see that the ‘Set’ time coincides exactly with the automation trace for sunset (and the notification text is correct in reporting the sun state). However, for sunrise, the automation triggers exactly 2 minutes before the ‘Rose’ state change in the logbook (ie. Automation triggered by the sunrise at 07:42:46, but sun.sun changed to ‘Rose’ at 07:44:46). When this happens, I get a notification two minutes before sunrise that the sun is down.
Anything obvious that I am doing wrong? I realize I could put some delays in the automation to get the correct output, but it seems like something is broken here.
There are many different definitions of the sun states.
I suggest you install the sun2 integration instead and use that, since it has many of the definitions available as standard.
Good thought. I just checked, and when the event fired, the elevation attribute was -0.9. When the state changed to ‘above_horizon’ exactly 2 minutes later, the elevation attribute changed to -0.48.
Perhaps the criteria for changing the state and firing the event are different, and the 2 minutes just happens to be a coincidence?
Looks like the sun state changes when elevation increases above -0.833, which tracks with what I found so far: source.
I haven’t been able to find the relevant code related to the SUN_EVENT_SUNRISE event firing, yet. Any ideas?
This really seems like a bug in the Sun integration. But I am not sure how to fix it, because I don’t yet understand the trigger for the sunrise event.
I agree that 0.83° is fine for the state change, but why doesn’t the event fire at the same time? I will try changing my elevation to 0 to see if this impacts anything, if it does, it definitely seems like a bug.
There are several workarounds for this, but at the end of the day (well really the beginning of the day lol), it seems like the integration isn’t working properly.