After Sunset, After Midnight

So I have an automation that has a condition “after sunset”. This is working great.


When we stayed up to watch the new year (after midnight) the automation didn’t work as I guess it was then “before sunrise”.

How is everyone handling this? Is there an easy way to define the period of time between sunset and sunrise?


The easiest way to do this is by using elevation as follows:

- alias: 'timing-Porch light on at dusk'
    platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -1.0
    service: switch.turn_on
    entity_id: switch.front_porch_light

It’s reliable and it doesn’t mess up because of the change of day at midnight.

1 Like

Or use an input_boolean “helper”, toggled on at sunset and off at sunrise.

If you don’t like the elevation suggestion above (But it is the easiest) then you can just create another condition for “before: sunrise” and make the two conditions an “or”.

Or even easier than the elevation you could use the sun state as below_horizon and get the same effect.