Skip to content

Automatic Brightness for Lighting

Sean edited this page Jul 7, 2019 · 7 revisions

What you'll need:

  • A dimmable light bulb + switch or smart bulb

For awhile now I've had my lights automatically dim as the sun went down to make it easier to get ready to sleep. There's also the added bonus of the lights brightening in the morning too and the best part is they share the same automation! The automations below don't require any custom components and are highly customizable.

Some of this may be obvious to some, but I'm sure there are others like me without a technical background that could learn from this.

Why use sun elevation? I've found that using the sun elevation is the best solution to have consistent lighting changes all year round rather than using time-based triggers.

First make sure you have the sun component enabled in your main configuration.

sun:

Once enabled, you can see the current elevation for the sun. I used https://www.suncalc.org to gather past sun elevation data to best choose which elevations would be best for each brightness change. Once you have your elevation values chosen, it's time to make the automation.

Most of my lights are triggered with a motion sensor and that's how my automation will appear below. I will give brief explanations for each condition and the action.

- alias: Living Room Auto Brightness
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0002047afe
      to: 'on'
  condition:
    - condition: numeric_state
      entity_id: sensor.illumination_158d0002047afe
      below: '6'
    - condition: state
      entity_id: group.family
      state: 'home'
    - condition: state
      entity_id: light.living_room
      state: 'off'
    - condition: template
      value_template: "{{ not is_state('media_player.plex_living_room_tv_2', 'paused') }}" # NOT condition
    - condition: template
      value_template: "{{ not is_state('media_player.plex_living_room_tv_2', 'playing') }}" # NOT condition
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.living_room
        brightness_pct: '{% if states.sun.sun.attributes.elevation <= -35 %}10{% elif states.sun.sun.attributes.elevation <= -22 %}25{% elif states.sun.sun.attributes.elevation <= -15 %}50{% elif states.sun.sun.attributes.elevation <= -6 %}75{% elif states.sun.sun.attributes.elevation > -6 %}100{% else %}0{% endif %}'

Conditions

  • The first condition ensures the lights only come on if the natural light in the room is dark enough as I don't need the lights on with the shades open. You can remove this if you don't have a light sensor.

  • The second conditions ensures that the automation will trigger only if my wife or I are at home. We have a cat so this is important for us.

  • The third condition is crucial because of the second automation that will follow this one. Because our trigger is a motion sensor, I don't need this automation to constantly trigger with movement and possibly interfere with the second automation.

  • The fourth and fifth conditions ensure the lights do not turn on if we're watching a movie or something on the TV.

The Action

Here is where the elevation data you chose comes in. I chose five elevation points to use for mine but you can have as many as you want. Starting with the lowest elevation first, this action compares the sun's current elevation and determines if it's less than or equal to -35. If the sun is lower than -35, the lights will automatically come on to 10% brightness.

Rinse and repeat for each of your data points with the exception of the final one. You'll notice that the last two data points are the same. The last one differs as if the sun's elevation is higher than -6 the lights will turn on at full brightness, which is the majority of the day.

Now for the second automation. I wanted the lights to automatically adjust with the sun without any input from me. Basically I didn't want the lights to be dependent on one of us moving to trigger and transition to a dimmer setting.

- alias: Living Room Auto Brightness Transition
  trigger:
    - platform: state
      entity_id: sun.sun
  condition:
    - condition: state
      entity_id: group.family
      state: 'home'
    - condition: state
      entity_id: light.living_room
      state: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.living_room
        brightness_pct: '{% if states.sun.sun.attributes.elevation <= -35 %}10{% elif states.sun.sun.attributes.elevation <= -22 %}25{% elif states.sun.sun.attributes.elevation > -15 %}50{% elif states.sun.sun.attributes.elevation > -6 %}75{% elif states.sun.sun.attributes.elevation > -6 %}100{% else %}0{% endif %}'
        transition: 30

The transition automation is triggered solely on the sun's movement and also only if the lights are currently on. Definitely didn't want the lights to constantly turn on whenever the sun moved. You can copy and paste your action settings into this one and simply add whatever length of transition you would like. I chose thirty seconds because it's not really noticeable when the lights begin to dim.

And that's it! I get up before sunrise a lot so these automations double their effect by not blinding me in the early morning too. I've been using these for every room for the past few days and everything works flawlessly. What's great about these automations is you can easily adjust your data points and fine tune to your liking on the fly.

Special thanks to this thread for allowing me to figure this out: https://community.home-assistant.io/t/help-with-creating-an-advanced-sunset-lights-automation/64204/5