If you want one single automation to toggle the lights, you would need two triggers, one for sunset and one for sunrise (or just one for each sun.sun state change).
automation:
- alias: Toggle with sun
trigger:
- platform: state
entity_id: sun.sun
action:
- service: light.toggle
entity_id: light.mylight
Or you create 2 automations for separate events. One for on at sunset and one for off at sunrise.
You can also use the Sun elevation angle, I use that in all of my Sun based automations instead of sunrise/sunset as when a reboot occurs the sunrise/sunset usage fails as it looks to the next event.
I.e. if the sun is already down, that event has past, so if a reboot occurs of your system, that automation will fail to work.
Try something like this. In this example, the light will trigger on while the sun is below 8° elevation in your location.
This code doesn’t match the document. The document has sunset and then sunrise ( which is how most people Invision this to work, i.e it turns on when the sun sets and off at sunrise) Not sure if it matters, but my automation did not run correctly like this.
The new built-in automation GUI builds it similar to how you typed it.
The order of sunrise and sunset doesn’t matter, it’s an OR condition it evaluates both conditions no matter which comes first. Please show the automation that did not run correctly.
So if I understand correctly, the code goes to sundown and goes to the next sunrise? It does not go to the 00 time and evaluate from there?
I am testing the new one and will know tonight. If it works I will post both. The one that failed was one that the automation GUI created. I’ll try and recreate the or. The code was definitely different than the docs. It has one condition: sun statement. I am not fluent in the code yet to be able to evaluate if it is equivalent. I do know that this is a common need and it is not working for me. I’ll post it when I get home.
From the automation
condition: or
conditions:
- condition: sun
before: sunrise
after: sunset
after_offset: '03:00:00'
From the docs:
condition: or
conditions:
- after: sunset
after_offset: '03:00:00'
condition: sun
- before: sunrise
condition: sun