Defining a timespan in an automation

I want my automation to be started by a trigger (numeric value <1) during a timespan between 1 hour before sunset and sunset. i.e. at any time during that period. However the automation should start even if the value is above 1 at sunset. What is wrong with the logic of the following condition, it doesn’t seem to work.

condition:
  - condition: and
    conditions:
      - condition: sun
        before: sunset
      - condition: sun
        after: sunset
        after_offset: '-01:00:00'`

If you want one hour before sunset, why is your second Sun Condition using after sunset?

I think you need to get rid of the before: The offset function modifies only the portion of the timespan proximal to your reference point (sunset or sunrise). For sunset, before: allows timespans from midnight to a time before sunset or a time after sunset. On the other hand after: allows timespans from before sunset or after sunset until midnight. Your original condition boils down to “from midnight to sunset and from sunset until midnight”… it is always true.

condition:
  - condition: sun
    after: sunset
    after_offset: '-01:00:00'
  - condition: not
    conditions:
      - condition: sun
        after: sunset

Because I find the sun condition’s offset function annoying I would probably use a Template condition:

condition:
  - condition: template
    value_template: >
      {% set sunset = state_attr('sun.sun', 'next_setting')|as_datetime|as_local %}
      {{ sunset - timedelta(hours=1) < now() <= sunset }}
1 Like

My interpretation is that both alternatives with an offset give the same time. I could be wrong :frowning:

Thanks you Drew, I will give this a try.