Auto Adjusting Dynamic Brightness with Custom Transition Time and Brighntess Values

I created an automation to fade the brightness of all lights that are on with user specified start and stop times for both fade in and fade out as well as minimum and maximum brightness values. I took inspiration from another thread found here.

The whole setup requires a few input_datetime’s, an input_boolean and a couple input_numbers. A template sensor is also required along with the automation itself. Below is the code for all those items. It looks complicated and it sort of is but once you understand what is being done, its much easier to digest. There may be a better way to achieve what this does and I’d be happy to take suggestions for improvement.

Below is the lovelace card and its code I use.

image

entities:
  - entity: input_boolean.global_brightness
  - entity: input_number.day_brightness
    icon: 'mdi:white-balance-sunny'
  - entity: input_number.night_brightness
    icon: 'mdi:weather-night'
  - entity: input_datetime.fade_in_start
  - entity: input_datetime.fade_in_stop
  - entity: input_datetime.fade_out_start
  - entity: input_datetime.fade_out_stop
show_header_toggle: false
title: Global Light Brightness Settings
type: entities

Automation: All lights need to be manually entered as a trigger for the brightness to be set at the time of it being turned on.

- alias: 'Autoadjust brightness When Turned On' #When a light is turned on adjust to appropriate dim level
  initial_state: true
  trigger:
##################LIGHTS####################
    - platform: state
      entity_id: light.back_porch_inside_light
      to: 'on'
    - platform: state
      entity_id: light.bathroom_light
      to: 'on'
    - platform: state
      entity_id: light.stairs_light
      to: 'on'
################^^LIGHTS^^##################
    - platform: state
      entity_id: sensor.template_global_light_brightness
    - platform: state
      entity_id: input_boolean.global_brightness
    - platform: state
      entity_id: input_number.day_brightness
    - platform: state
      entity_id: input_number.night_brightness
    - platform: state
      entity_id: input_datetime.fade_in_start
    - platform: state
      entity_id: input_datetime.fade_in_stop
    - platform: state
      entity_id: input_datetime.fade_out_start
    - platform: state
      entity_id: input_datetime.fade_out_stop
  action:
    - service: light.turn_on
      data_template:
        entity_id: "{{ states.light | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | join(',') }}"
        brightness: >-
          {% if states.input_boolean.global_brightness.state == 'off' %}
            {{trigger.entity_id.attributes.brightness}}
          {% else %}
            {{ (states.sensor.template_global_light_brightness.state) }}
          {% endif %}

Template Sensor: The sensor refreshes every minute allowing it to be used as a trigger for the automation when its state/value changes.

  - platform: template
    sensors:
      template_global_light_brightness:
        friendly_name: Global Light Brightness
        value_template: >-
          {% if states('sensor.time') and states.input_boolean.global_brightness.state == 'off'%}
            {{trigger.entity_id.attributes.brightness}}
          {% elif (( now().hour ) * 60 |float) + ( now().minute ) < (states.input_datetime.fade_in_start.attributes.hour * 60) + (states.input_datetime.fade_in_start.attributes.minute)  %}
            {{ (states.input_number.night_brightness.state|float * 255 / 100) | round(0)}}
          {% elif (( now().hour ) * 60 |float) + ( now().minute ) < (states.input_datetime.fade_in_stop.attributes.hour * 60) + (states.input_datetime.fade_in_stop.attributes.minute)  %}
            {{((((now().hour * 60 + now().minute)-(states.input_datetime.fade_in_start.attributes.hour * 60 + states.input_datetime.fade_in_start.attributes.minute)) / ((states.input_datetime.fade_in_stop.attributes.hour * 60 + states.input_datetime.fade_in_stop.attributes.minute) - (states.input_datetime.fade_in_start.attributes.hour * 60 + states.input_datetime.fade_in_start.attributes.minute)) * ((states.input_number.day_brightness.state|float) - (states.input_number.night_brightness.state|float)) + (states.input_number.night_brightness.state)|float) * 255 / 100) | round(0)}}
          {% elif (( now().hour ) * 60 |float) + ( now().minute ) < (states.input_datetime.fade_out_start.attributes.hour*60) + (states.input_datetime.fade_out_start.attributes.minute)  %}
            {{ (states.input_number.day_brightness.state|float *255 /100) | round(0)}}
          {% elif (( now().hour ) * 60 |float) + ( now().minute ) < (states.input_datetime.fade_out_stop.attributes.hour*60) + (states.input_datetime.fade_out_stop.attributes.minute)  %}
            {{((((states.input_datetime.fade_out_stop.attributes.hour * 60 + states.input_datetime.fade_out_stop.attributes.minute)-(now().hour*60 + now().minute)) / ((states.input_datetime.fade_out_stop.attributes.hour * 60 + states.input_datetime.fade_out_stop.attributes.minute) - (states.input_datetime.fade_out_start.attributes.hour * 60 + states.input_datetime.fade_out_start.attributes.minute)) * ((states.input_number.day_brightness.state|float) - (states.input_number.night_brightness.state|float)) + (states.input_number.night_brightness.state)|float) * 255 / 100) | round(0)}}
          {% else %}
            {{ (states.input_number.night_brightness.state|float * 255 / 100) | round(0)}}
          {% endif %}

Input Datetime:

input_datetime:
#Light Brightness global fade settings
  fade_in_start:
    name: Fade in Start Time
    has_date: false
    has_time: true
  fade_in_stop:
    name: Fade in Stop Time
    has_date: false
    has_time: true
  fade_out_start:
    name: Fade out Start Time
    has_date: false
    has_time: true
  fade_out_stop:
    name: Fade out Stop Time
    has_date: false
    has_time: true

Input Number:

input_number:
#Light Brightness global fade settings
  day_brightness:
    name: Day Brightness
    initial: 100
    min: 1
    max: 100
    step: 1
    unit_of_measurement: '%'

  night_brightness:
    name: Night Brightness
    initial: 1
    min: 1
    max: 100
    step: 1
    unit_of_measurement: '%'

Input Boolean

input_boolean:
  global_brightness:
    name: Global Brightnes
    initial: on

Let me know if this was useful to you or if you have any suggestions for how I could improve it for my setup.

6 Likes

You should turn this into a blueprint.

1 Like

Hi, I’d like to give this a try but I’m new to Home Assistant, is there an idiots guide to set this up.