Automation - after sunset (tonight) but before sunrise (the next morning) Does Not Work

I am pretty sure that this used to work a couple of years ago. Now, it doesn’t. What I want:

Automation description: If Office motion detector says I’m in the office, and it’s between sunset tonight and sun rise next morning, then turn on the desk lamp (basically, do this when it’s dark based on sun).
Automation triggers: every minute
Condition: Sensor must show I’m detected in the office; Must be dark based on sun - therefore, time must fall between sunset the current night, and sun rise the next morning.

What happens: condition fails even though I am inside the window - the sun has set, and it has not yet risen, therefore it’s still dark based on the sun’s state.

What I tried:

  1. Set to after sunset - works until midnight I think, then, automation no longer triggers.
  2. Set to before sunrise - Doesn’t work until past midnight, I think. Meaning there is a window between sun setting in the current evening, and HA resetting the “sun” which seems to be midnight!?

I do not want to deal with fixed timings because England could get dark at 10PM during summer, or 3PM during Winter. So if I set to 7, my office will be too dark until 7 in Winter, or it will waste electricity during Summer by turning on the lamp when it’s not needed.

The above way - after sunset / before sunrise used to work before, as I had an automation to control hallway lights that way and it worked. Then after some release I can’t remember, it stopped working.

Posting here first for advice, if this is a bug let me know and I’ll raise a report.

1 Like

1 Like

You could also switch to sun2. It has yesterday, today and tomorrow sunrise and sunset. This works for me.

1 Like

I personally find it a lot easier to simply use the condition / state of: below_horizon


Here’s an automation I use, to turn ON the lights when motion is detected, a little bit before sunset (1:45) (hour and 45 mins) and does not run after sunrise. The kitchen group is just the kitchen light, and the small room prior to the kitchen, which we pass through to get to the kitchen.

  alias: Kitchen MotionGroup-light ON 
  description: Kitchen MotionGroup-light ON 
    - platform: state
      entity_id: group.kitchenmotion
      id: group.kitchenmotion
      from: "off"
      to: "on"
    - condition: or
        - condition: sun
          after: sunset
          after_offset: -01:45:00
        - condition: sun
          before: sunrise
    - service: switch.turn_on
        device_id: 4856f51b9d4xxxxxxx
  mode: single
1 Like

Thanks all. I went for the “Or” conditional and set up 2 suns, it works flawlessly and can easily be done in the UI in seconds.

1 Like