Help with creating an advanced sunset + lights automation

ok, be prepared to be amazed with the power of HA.
You can have 1 automation for both scenarios, you just need 2 triggers:

- alias: Turn light on in line with the sun elevation
  trigger:
    - platform: state
      entity_id: input_select.redacted
      
      from: "Away" #can be removed, doens't matter what it changes from, key point is it's changing to Home
      to: "Home"
    - platform: state
      #triggers whenever sun entity (status or attributes) changes
      entity_id: sun.sun
  condition:
    - condition: template
      # checks that it's night
      value_template: '{{states.sun.sun.attributes.elevation <= 0}}'
    - condition: state
      #checks that you're home
      entity_id: input_select.redacted
      state: 'Home'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.light_name
        xy: '{% if states.sun.sun.attributes.elevation <= -30 %}70{% elif states.sun.sun.attributes.elevation <= -20 %}60{% elif states.sun.sun.attributes.elevation <= -10%}50{% else %}40{% endif %}'
        brightness: '{% if states.sun.sun.attributes.elevation <= -30 %}3{% elif states.sun.sun.attributes.elevation <= -20 %}2{% elif states.sun.sun.attributes.elevation <= -10%}1{% else %}0{% endif %}'

I took the liberty to assume this:

  • Sun is at elevation -30 or lower, set to brightness 3, xy_color: 70
  • added a new scenario for sun between 0 and -10, set to brightness 0, xy_color: 40

Feel free to adjust

I’ve checked the syntax but not tested on a real system

2 Likes