Help with sunset/sunrise automation

Hello people,

I want to turn on lights when the sun sets down en go off when the sun comes up.
Whatever i try i get error: UndefinedError: ‘trigger’ is undefined

My automation:

alias: Verlichting aan bij zonsondergang en uit bij zonsopgang
trigger:
  - platform: sun
    event: sunset
    offset: '-00:30:00'
  - platform: sun
    event: sunrise
    offset: '+00:15:00'
action:
  - service: light.turn_{{'on' if trigger.event == 'sunset' else 'off'}}
    target:
      entity_id: light.voordeur
    data:
      brightness_pct: 100
      color_name: purple
  - service: light.turn_{{'on' if trigger.event == 'sunset' else 'off'}}
    target:
      entity_id: light.extended_color_light_1 
    data:
      brightness_pct: 100
      kelvin: 2900
mode: single

You get that because you’re pushing Run actions

Can you clarify please because i don’t get it :frowning:

You typically see that error if you push run actions on the automation, rather than letting it trigger itself.

If you wait, and let it trigger itself, then it should be fine.

You can also use sun elevation so that it adjusts to TOD throughout the year. The light level 30mins before sunset in summer is normally quite a bit different to 30mins before sunset in Winter due to the suns angel in the sky. Using the elevation attribute sorts that part out.

- alias: Sunset - Turn on Driveway & Downstairs
  initial_state: true
  trigger:
    platform: template
    value_template: '{{ state_attr("sun.sun", "elevation") < 8 }}'
  action:
    service: homeassistant.turn_on
    entity_id:
      - script.white_lights
      - switch.driveway
      - light.downstairs

and

- alias: Turn off at Sunrise
  initial_state: true
  trigger:
    platform: template
    value_template: '{{ state_attr("sun.sun", "elevation") > 1 }}'
  action:
    service: homeassistant.turn_off
    entity_id:
      - switch.driveway
      - light.hall
      - light.downstairs
      - group.kitchen

You may also be better off putting light config into a script, that way you can call that setting for other automations more easily. I have scipt.white_lights above, and the script is this;

white_lights:
  alias: White Lights
  sequence:
    - service: light.turn_on
      entity_id: light.steps
      data:
        brightness: 75
        color_temp: 222
        transition: 3
    - service: light.turn_on
      entity_id: light.table
      data:
        brightness: 75
        color_temp: 222
        transition: 3

and for when we are watching TV, the lights turn blue.

blue_lights:
  alias: Blue Lights
  sequence:
    - service: light.turn_on
      entity_id: light.steps
      data:
        brightness: 60
        rgb_color:
          - 0
          - 0
          - 255
        transition: 3
    - service: light.turn_on
      entity_id: light.table
      data:
        brightness: 60
        rgb_color:
          - 0
          - 0
          - 255
        transition: 3

Just simplifies things a little in the automations.

Thank you for the reply, i’m going to try this asap, much better indeed with the elevation.
For more advanced automations i always fail (too noobish) :smiley:

1 Like