How to Build Automation Only Work 1:00 AFTER Sunset until 1:00 BEFORE next Sunset?

After reviewing several threads on this topic, the documentation, and being unable to figure it so far through trial and error, I need some help.

I want an automation to always work EXCEPT for the window of time that lasts from 1 hour BEFORE Sunset to 1 hour AFTER sunset. How do you set this up?

I’ve tried using OR, NOT, and other conditional variables in the automation, but it seems like it treats each single sunset event differently, so it doesn’t allow for either a true or false event during that window only.

Here’s an adjusted visual from the documentation of what I’m trying to accomplish - I want the red to be true and the white space between it to be false, therefore the automation only runs during the red parts. I would think I would simply indicate After Sunset +1:00:00 and Before Sunset -01:00:00 in the same sun condition, but it just is not working for me.

Using before and after in the one condition is AND logic, and it cant be both before and after sunset at the same time, so your condition would always be false. To get OR logic you need two sun conditions in an OR block:

condition:
  - condition: or
    conditions:
      - condition: sun
        before: sunset
        before_offset: "-01:00:00"
      - condition: sun
        after: sunset
        after_offset: "01:00:00"

This will be true from midnight to one hour before sunset as well as being true from one hour after sunset to midnight.

I had a variant of this in my tests, but for some reason thought I had it wrong in my testing. Configured the automation today and it worked perfectly. Thank you @tom_l .

1 Like