Turn on lights at sunset and on/off sun for dates not working?

What’s up community!
I have this automation that does not seem to be working as expected. I’m not finding anything jumping out of me in the traces for it either.
The automation is supposed to turn the lights on at Sunset and then off at sunrise only between February 8th through the 15th. It is not turning them on to begin with.

Here’s the automation yaml:

alias: Garage lights on/off sun - Valentine's Day
description: ""
triggers:
  - trigger: sun
    event: sunset
    offset: 0
conditions:
  - condition: and
    conditions:
      - condition: template
        value_template: "{{ (8 <= now().day <= 15) and (now().month == 2) }}"
actions:
  - action: light.turn_on
    metadata: {}
    data:
      rgb_color:
        - 255
        - 0
        - 255
      brightness_pct: 100
    target:
      entity_id: light.garage_lights_group
  - delay: "{{ states('sensor.time') < states('sun.sunrise') }}"
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.garage_lights_group
mode: single

Can someone take a look and offer assistance?

Your delay is completely wrong. A delay expects either a number (interpreted as seconds), a timestring, a dict containing individual time attributes (hours, minutes, seconds), etc.

Your template however is doing a comparison which will evaluate to true or false. When this is fed into the delay, it will be cast to a number and thus interpreted as either 1 or 0 seconds.

Your lights will turn on and then pretty much immediately turn off again, possibly so quickly that you will never see them turn on in the first place.

1 Like

In addition to Magnus’ comments:

  1. sun.sunrise is not real… which leads me to guess this is the product of a bullshit engine, so… sensor.time may or may not exist on your system, it must be added manually to your configuration. It’s not actually necessary to accomplish what you want, but you can find the configuration details at Time & Date integration
  2. Prolonged delays are generally a bad practice. Use two triggers and branched logic or just use separate automations for on and off.
  3. The And condition isn’t doing anything.
1 Like

The following example turns on the light at sunset and turns it off 30 minutes later (adjust the time to suit your needs) for dates between February 8 and February 15

alias: Garage lights on/off sun - Valentine's Day
description: ""
triggers:
  - id: 'on'
    trigger: sun
    event: sunset
  - id: 'off'
    trigger: sun
    event: sunset
    offset: '00:30:00'
conditions:
  - condition: template
    value_template: "{{ (8 <= now().day <= 15) and (now().month == 2) }}"
actions:
  - if: "{{ trigger.id == 'on' }}"
    then:
      - action: light.turn_on
        metadata: {}
        data:
          rgb_color:
            - 255
            - 0
            - 255
          brightness_pct: 100
        target:
          entity_id: light.garage_lights_group
    else:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: light.garage_lights_group
mode: single
1 Like

Thank you all, especially for the example yaml.
:raised_hands::pray::+1:

Why not use sunrise for off instead of sunset with offset for if, then, else?

Sorry, I misunderstood your requirements. You can definitely do that. Simply replace the second trigger with this:

    - id: 'off'
      trigger: sun
      event: sunrise

You can optionally add an offset to it in case you want to turn off the light a few minutes before or after sunrise.

1 Like