Help with creating an advanced sunset + lights automation

So I need some extensive help in achieving something I wanted for a long time:

An automation or script that dims my lights together with the position of the sun after sunset.

I’m guessing I need some value templates and stuff but I really don’t know where to start.

If possible I want to add an additional parameter to the whole automation:

If I get home after certain points in time after sunset I want the sunset+lights automation to fire up the lights at the brightness it would be if I where home.

Any help would be much appreciated.

Should be “fairly straight forward”
In quotes as I’m not 100% what exactly you want
Can you list the scenarios you want? what triggers them, what conditions and actions you want?
If you write them in English I can try and convert them in yaml
So far I understand

  • When sun position changes (using elevation), if after sunset (elevation is negative), dim the lights accordingly (Please confirm how you want to dim the lights, from and to values)
  • if you come home (obviously suggests above automation only runs when you’re home, please provide device_tracker details), set the dim level at that tome to what it would have been had you been here all along.

Thanks mate!

So basically there are 2 scenarios/triggers:

1: Someone is home
2: Someone comes home

Let’s start with 1:

Lights dims with the movement of the sun below the horizon up until a certain threshold

Trigger:
This part I think I can manage but it should trigger when the sun is below the horizon, maybe with a time offset. But I got it covered

Condition:
Someone is home. I probably got this part covered to.

Action:
Sun is at elevation -10, then lights is set to brightness 1, xy_color: 50
Sun is at elevation -20 then lights is set to brightness 2, xy_color: 60
Sun is at elevation -30 then lights is set to brightness 3, xy_color: 70

Problem with the above is how to trigger at every set elevation point.

Ok, number 2:

When someone comes home I want the lights to be set at the brightness it would normally be after sunset if someone was home

#the trigger below is my standard trigger when someone gets home
  trigger:
    platform: state
    entity_id: input_select.[redacted]
    from: "Away"
    to: "Home"
    
  # I'm thinking maybe I need several scripts here.
  
  condition:
    sun at an elevation point or between points
    
  action:
    - service: script.turn_on
         entity_id: script.sunatXYZ

Hi @Nutti85, looks like you’re in good hands with @lolouk44. I just wanted to let you both know, in case you didn’t see the associated topic, I’ve created an enhanced version of the sun component that adds sunrise and sunset attributes that stay constant throughout the day. (I’ve also made a PR to include this in an upcoming release.) So, if you wanted to calculate light values in a template based on time since sunset (as opposed to elevation), that would be possible now.

2 Likes

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

Thanks. I had seen it. Not looked into details yet so not sure if it would bring anything more here. Trying to keep it simple for now for @Nutti85 before diving into customisations :blush:

That IS amazing!

I will try it as soon as possible and report back.

Thanks again!

1 Like

It seems to work! This was excellent help as it gave me some more insight into templating to. So much more to try out now.

1 Like

Could you post your final automation?

The reason I ask is, I’m actually surprised it works, for a couple of reasons. Could just be I’m misunderstanding, which hopefully is the case.

First, I don’t think the light.turn_on service has an xy parameter that takes a single number. I thought it had an xy_color parameter that takes a sequence of two numbers, which I’ve found difficult to express with one jinja template.

Second, I thought brightness goes from 0 to 255. So brightness values of 1, 2 and 3 don’t seem reasonable.

What light platform are you using?

I’m not quite done yet but this is what is currently working.

- 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"
    - condition: template
      value_template: '{{ not is_state("input_select.home_mode", "Good Night")}}'
  action:
    - service: homeassistant.turn_on
      data_template:
        entity_id: group.living_room_lights
#        xy_color: '{% 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 <= -3 %}110{% elif states.sun.sun.attributes.elevation <= -2 %}115{% elif states.sun.sun.attributes.elevation <= -1 %}130{% else %}140{% endif %}"

The values I gave initially were just examples. I found out yesterday that it interfered with some of my other automations such as turning on light after we went to bed and also changing the lights when we watch neflix. I’ve got a harmony remote which I can get the states from so I will put in a condition on that also.

One thing though is that this triggers every time the sun moves, but I don’t know how to get around that.

That’s kinda the point. What’s the issue with the fact that it triggers this way?

Nothing special, just keeping the resource usage to a minimum. Not that it affects anything that much anyway.