Skip to content

Instantly share code, notes, and snippets.

@Twanne
Last active February 5, 2025 13:10
Show Gist options
  • Save Twanne/02dfb154084a8b9bf927c29160df4af3 to your computer and use it in GitHub Desktop.
Save Twanne/02dfb154084a8b9bf927c29160df4af3 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Smart Lux Dimmer
blueprint:
name: Smart Lux Dimmer
author: AntonH
description: |
Version 4.3
Switch or dim lights based on the value of a light sensor.
Light colors or temperature can be set to a fixed value when inside or outside the measuring range (different values are possible).
Settings for brightness, color or temperature outside min and max values of the sensor can be set.
To smoothen out transitions, a time can be set where the light will move from the current to the new brightness.
source_url: https://gist.github.com/Twanne/02dfb154084a8b9bf927c29160df4af3
domain: automation
input:
light_sensor_entity:
name: Light Sensor
description: Which light sensor do you want to use to measure the ambient light value?
selector:
entity:
filter:
- domain: sensor
device_class: illuminance
target_light:
name: Target lights
description: Which lights do you want to control?
selector:
target:
entity:
domain: light
run_conditions:
name: Conditions
description: |
Only run when these conditions are met.
(Remove Time Condition if not needed)
default:
- condition: time
selector:
condition:
max_brightness_value:
name: Maximum ambient light value
description: Dynamically change the light so long as the ambient light stays under this value.
default: 500
selector:
number:
min: 0
max: 1000
step: 10
unit_of_measurement: lx
mode: slider
min_brightness_value:
name: Minimum ambient light value
description: Dynamically change the light so long as the ambient light stays above this value.
default: 0
selector:
number:
min: 0
max: 1000
step: 10
unit_of_measurement: lx
mode: slider
light_value_1:
name: Brightness at maximum light level
description: Brightness of the light at maximum ambient light.
default: 0
selector:
number:
min: 0
max: 100
step: 1
unit_of_measurement: '%'
mode: slider
light_value_2:
name: Brightness at minimum light level
description: Brightness of the light at minimum ambient light.
default: 100
selector:
number:
min: 0
max: 100
step: 1
unit_of_measurement: '%'
mode: slider
transition_time:
name: Transition time
description: |
The time it takes for the light to transition from the set value to the next.
WARNING: this can smoothen the transition, but your light needs to support it.
default: 0
selector:
number:
min: 0
max: 300
step: 1
unit_of_measurement: s
mode: slider
include_color_or_temp:
name: Include color or temperature
description: Do you want to set a color or temperature value for the light?
default: include_no_color_temp
selector:
select:
options:
- label: I don't want to set color or temperature
value: include_no_color_temp
- label: Set color
value: include_color
- label: Set temperature
value: include_temp
light_color:
name: Light color
description: Color of the light when between minimum and maximum ambient light values.
selector:
color_rgb:
default: [255,255,255]
light_temp:
name: Light temperature
description: Temperature of the light when between minimum and maximum ambient light values.
default: 2000
selector:
color_temp:
include_brightness_over_under:
name: Include brightness values when outside range
description: I want the automation to set a brightness when the ambient light goes outside of the set range. (Over maximum and under minimum ambient)
default: false
selector:
boolean:
light_brightness_over_max:
name: Brightness when ambient light value over max.
description: Brightness of the light when the ambient light is higher than the set maximum value.
default: 0
selector:
number:
min: 0
max: 100
step: 1
unit_of_measurement: '%'
mode: slider
light_brightness_under_min:
name: Brightness when ambient light value under min.
description: Brightness of the light when the ambient light is lower than the set minimum value.
default: 100
selector:
number:
min: 0
max: 100
step: 1
unit_of_measurement: '%'
mode: slider
include_color_or_temp_over_under:
name: Include color or temperature values when outside range
description: |
Set a color or temperature value for the light when over maximum or under minimum ambient light value?
**'Include values for under min and over max' MUST BE TRUE**
default: include_no_color_temp_outside_range
selector:
select:
options:
- label: I don't want to set color or temperature when outside range
value: include_no_color_temp_outside_range
- label: Set color when outside range
value: include_color_outside_range
- label: Set temperature when outside range
value: include_temp_outside_range
light_color_over_max:
name: Color when ambient light value over max.
description: Color of the light when the ambient light is higher than the set maximum value.
selector:
color_rgb:
default: [255,255,255]
light_color_under_min:
name: Color when ambient light value under min.
description: Color of the light when the ambient light is lower than the set minimum value.
selector:
color_rgb:
default: [255,255,255]
light_temp_over_max:
name: Temperature when ambient light value over max.
description: Temperature of the light when the ambient light is higher than the set maximum value.
default: 2000
selector:
color_temp:
light_temp_under_min:
name: Temperature when ambient light value under min.
description: Temperature of the light when the ambient light is lower than the set minimum value.
default: 2000
selector:
color_temp:
mode: single
#Variables for use in calculations and statements
variables:
include_color_or_temp: !input include_color_or_temp
include_brightness_over_under: !input include_brightness_over_under
include_color_or_temp_over_under: !input include_color_or_temp_over_under
light_sensor: !input light_sensor_entity
max_brightness_value: !input max_brightness_value
min_brightness_value: !input min_brightness_value
maxB: '{{ max_brightness_value * 2.55 }}'
minB: '{{ min_brightness_value * 2.55 }}'
light_value_1: !input light_value_1
light_value_2: !input light_value_2
light1: '{{ light_value_1 * 2.55 }}'
light2: '{{ light_value_2 * 2.55 }}'
slope: '{{ ( light1 - light2 ) / ( maxB - minB ) }}'
constant: '{{ light1 - ( slope * maxB ) }}'
light_brightness_over_max: !input light_brightness_over_max
light_brightness_under_min: !input light_brightness_under_min
brightness_over_max_pct: '{{ light_brightness_over_max * 2.55 }}'
brightness_under_min_pct: '{{ light_brightness_under_min * 2.55 }}'
#Triggers
triggers:
trigger: state
entity_id: !input light_sensor_entity
#Conditions
conditions: !input run_conditions
#Actions
actions:
- choose:
#Actions when no states need to be sent to target outside ambient light value range
- conditions:
- condition: template
value_template: '{{ include_brightness_over_under is false }}'
sequence:
- choose:
#no color or temperature values needed
- conditions:
- condition: template
value_template: '{{ include_color_or_temp == "include_no_color_temp" }}'
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ (( slope * states(light_sensor)|int ) + constant)|round }}'
target: !input target_light
#color value needed
- conditions:
- condition: template
value_template: '{{ include_color_or_temp == "include_color" }}'
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ (( slope * states(light_sensor)|int ) + constant)|round }}'
rgb_color: !input light_color
target: !input target_light
#temperature value needed
- conditions:
- condition: template
value_template: '{{ include_color_or_temp == "include_temp" }}'
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ (( slope * states(light_sensor)|int ) + constant)|round }}'
color_temp: !input light_temp
target: !input target_light
#Actions when states need to be sent to target outside ambient light value range
- conditions:
- condition: template
value_template: '{{ include_brightness_over_under is true }}'
sequence:
- choose:
#no color or temperature values needed outside range
- conditions:
- condition: template
value_template: '{{ include_color_or_temp_over_under == "include_no_color_temp_outside_range" }}'
sequence:
- choose:
#Actions when over maximum measured light value
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
above: !input max_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ brightness_over_max_pct }}'
target: !input target_light
#Actions when under minimum measured light value
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
below: !input min_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ brightness_under_min_pct }}'
target: !input target_light
#Actions when measured light value is between minimum and maximum settings
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
below: !input max_brightness_value
above: !input min_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ (( slope * states(light_sensor)|int ) + constant)|round }}'
target: !input target_light
#Color values needed outside range
- conditions:
- condition: template
value_template: '{{ include_color_or_temp_over_under == "include_color_outside_range" }}'
sequence:
- choose:
#Actions when over maximum measured light value
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
above: !input max_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ brightness_over_max_pct }}'
rgb_color: !input light_color_over_max
target: !input target_light
#Actions when under minimum measured light value
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
below: !input min_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ brightness_under_min_pct }}'
rgb_color: !input light_color_under_min
target: !input target_light
#Actions when measured light value is between minimum and maximum settings
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
below: !input max_brightness_value
above: !input min_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ (( slope * states(light_sensor)|int ) + constant)|round }}'
rgb_color: !input light_color
target: !input target_light
#Temperature values needed outside range
- conditions:
- condition: template
value_template: '{{ include_color_or_temp_over_under == "include_temp_outside_range" }}'
sequence:
- choose:
#Actions when over maximum measured light value
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
above: !input max_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ brightness_over_max_pct }}'
color_temp: !input light_temp_over_max
target: !input target_light
#Actions when under minimum measured light value
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
below: !input min_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ brightness_under_min_pct }}'
color_temp: !input light_temp_under_min
target: !input target_light
#Actions when measured light value is between minimum and maximum settings
- conditions:
- condition: numeric_state
entity_id: !input light_sensor_entity
below: !input max_brightness_value
above: !input min_brightness_value
sequence:
- action: light.turn_on
data:
transition: !input transition_time
brightness: '{{ (( slope * states(light_sensor)|int ) + constant)|round }}'
color_temp: !input light_temp
target: !input target_light
@vyper144
Copy link

vyper144 commented Feb 1, 2025

I'm using this with Govee lights, which have a maximum amount of polls it allows within 24 hours. AND if too many requests in a short period of time, the Govee lights error out. And won't perform the actions.

If I use the Blueprint as-is, I run into both issues (max poll and too many polls within X time).

How can I delay the blueprint to run every (let's say) 2 minutes?

@Twanne
Copy link
Author

Twanne commented Feb 5, 2025

I'm using this with Govee lights, which have a maximum amount of polls it allows within 24 hours. AND if too many requests in a short period of time, the Govee lights error out. And won't perform the actions.

If I use the Blueprint as-is, I run into both issues (max poll and too many polls within X time).

How can I delay the blueprint to run every (let's say) 2 minutes?

Change the trigger to a time patter trigger.
Right now it will run every time the light sensor's value changes, which during the day can be quite quickly.
If you let it run let's say every 2-3 minutes, that will lessen the load.

You can also achieve this through my other Blueprint: https://community.home-assistant.io/t/the-everything-light-trigger-a-light-any-way-and-turn-it-on-in-any-way/663278/73
It uses the same logic, but you can specify other triggers like the time pattern trigger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment