Smoothly turn the lights on when the sun sets

When the sun sets at a partical level you could make an automation to turn on the lights with a transition of a couple of minutes till it’s dark. But how long would the transition be? It depens on the time of year.

I have made a formula that calculates the brightness of the light depending on the elevation of the sun. This automation will work every day of the year. The lights will go on very smoothly. I love this

 alias: ''sunset transition'
  trigger:
  - platform: time
    minutes: '/2'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: time
        after: '16:00:00'             (the time when a sunset could start in a year)
        before: '23:00:00'          (the time when a sunset could end in a year)
      - condition: numeric_state
        entity_id: sensor.sun_elevation
        above: 0
        below: 9
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ states.light.(livingroom light group).attributes.brightness < (((0.0625*(states.sun.sun.attributes.elevation**2))-(31.125*states.sun.sun.attributes.elevation)+246.5)|int) }} '
          - condition: template
            value_template: '{{ states.light.(livingroom light group).attributes.brightness==null }} '
  action:
    - service: LIGHT.TURN_ON
      entity_id: light.(livingroom light group)
      data_template:
        profile: sunset 
        brightness: '{{((0.0625*(states.sun.sun.attributes.elevation**2))-(31.125*states.sun.sun.attributes.elevation)+246.5)|int}}'

For the sun.elevation sensor I have used a template sensor:

sensors:
  - platform: template
    sensors:
      sun_elevation:
        value_template: {{states.sun.sun.attributes.elevation}}
6 Likes

Not sure why you don’t went with the Flux Light Adjustment component, sun component or any weather component that returns sun information.

Yes, I use the flux light adjustment switch in combination with this. it gets even more smooth. Good point. I have used the sun component as you can see. A weather attribute is a good addition to this. If it’s cloudy. You could increase the brithness of the lights

Will have to take a look at this and combine a few things, I’m putting up my weather station outside soon and it will have a lux value sensor on it to mix in for light adjustments with weather.

great! I’m curious how it will work out for you

How to have the sensor.sun_elevation?

Is this similar:

- platform: template
  sensors:
    solar_angle:
      value_template: '{{ "%+.1f"|format(states.sun.sun.attributes.elevation) }}'
      friendly_name: 'Inclinazione del Sole'
      unit_of_measurement: '°'

But with this i must change the values of above and below?

That’s correct. You don’t need the unit of measurement because the value is only used in the formula

So, using my sensor, what value to put on above and below ?

you can use the values in the example. I have chosen the value 9 because that’s the angle of the sun with the horizon when it’s beginning to get dark. when the value is 0 the light is at it’s brightest and the automation stops

What’s the point of the 4pm trigger if you’re triggering every 2 minutes anyway?

I don’t want to trigger every 2 minutes the whole day. Only when it’s possibly needed…Maybe it doesn’t make sense but I think it’s more focused on the sunset’s time window

But that’s not what you’re doing. You’re triggering every 2 minutes the whole day, and at 4pm.

Then you’re using a condition to prevent triggering unless it’s between 4pm and 11pm.

you are right. the 4pm trigger is unnecessary. I will remove it

This looks great, gonna try this out the weekend. Super cool. So this will go from off to 100% as the sun sets from 9 degrees to 0 degrees above horizion.

I assume you have a group called “livingroom light” I’m still getting used to the finer naming with yaml, but wouldn’t you need underscores?

So If I wanted to copy this exactly, I should create a group with only lights and name it “Livingroom Light Group” or “Livingroom Light”. My automations with groups dont do anything like this, just mostly turn them on or off, but the enties are named "group.outdoor_lights_sunset’ for example.

Thanks!

Yes, the name of the light group is not (livingroom light group). It’s just an example. You could use light.livingroom for example. A group of lights is seen as one entity. Like it is one light bulb:

https://www.home-assistant.io/components/light.group/

I run luminiscence (lux) sensors in some rooms, and use that information to see when I should turn on/off the lights. (Subtracting the lightsources from the sensor value, of course.)

If you remove the 4PM - 11PM condition I think it will also turn on the lights at sunrise?

If I understand correctly the max brightness is set to 246.5, next you make it an int which results in 246?

i’m adapting his automation with my garden lights, i’ve made some changes, removing the first condition time leaving the sun elevation can some one tell me in it’s ok? i whant that the group.luci_parete_giardino turn on at the dusk( in my case at below -2) and the transition stop after 30-40 min ( at above -7) is this code correct?

- alias: 'sunset transition luci parete giardino'
  trigger:
  - platform: time
    minutes: '/2'
    seconds: 00
  condition:
    condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.sun_elevation
        above: -7
        below: -2
      - condition: template
        value_template: '{{ states.light.group.luci_parete_giardino.attributes.brightness < (((0.0625*(states.sun.sun.attributes.elevation**2))-(31.125*states.sun.sun.attributes.elevation)+246.5)|int) }} '
      - condition: template
        value_template: '{{ states.light.group.luci_parete_giardino.attributes.brightness==null }} '
  action:
    - service: LIGHT.TURN_ON
      entity_id: group.luci_parete_giardino
      data_template:
        profile: sunset 
        brightness: '{{((0.0625*(states.sun.sun.attributes.elevation**2))-(31.125*states.sun.sun.attributes.elevation)+246.5)|int}}'