Last active
March 9, 2025 17:42
-
-
Save RubenKelevra/ab50f89209851a3e515d4f56abca21c5 to your computer and use it in GitHub Desktop.
Home Assistant automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias: Left Room Outdoor Mirroring Brightness/Color Temp | |
description: >- | |
Adjusts the brightness of lamps to achieve the target brightness in the left | |
room. | |
triggers: | |
- entity_id: | |
- sensor.target_brightness_left_room_outdoor_mirroring | |
trigger: state | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 2 | |
- entity_id: | |
- sensor.illuminance_left_room_under_desktop_corrected | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- light.spot_lights_left_room | |
attribute: brightness | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- light.ceiling_lamps_left_room | |
attribute: brightness | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- light.ceiling_lamps_left_room | |
attribute: color_temp_kelvin | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- sensor.presence_left_room_presence_left_room_left_room_color_temperature | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- light.spot_lights_left_room | |
attribute: brightness | |
for: | |
hours: 0 | |
minutes: 1 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- light.ceiling_lamps_left_room | |
attribute: brightness | |
for: | |
hours: 0 | |
minutes: 1 | |
seconds: 10 | |
trigger: state | |
- entity_id: | |
- light.ceiling_lamps_left_room | |
attribute: color_temp_kelvin | |
for: | |
hours: 0 | |
minutes: 2 | |
seconds: 10 | |
trigger: state | |
- trigger: state | |
entity_id: | |
- sensor.target_brightness_left_room_outdoor_mirroring | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 1 | |
- trigger: state | |
entity_id: | |
- input_boolean.left_room_lights | |
- trigger: state | |
entity_id: | |
- input_boolean.left_room_lights | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
- trigger: state | |
entity_id: | |
- input_boolean.left_room_lights | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 20 | |
- trigger: state | |
entity_id: | |
- input_boolean.left_room_lights | |
for: | |
hours: 0 | |
minutes: 0 | |
seconds: 1 | |
- trigger: homeassistant | |
event: start | |
id: HA Start | |
conditions: [] | |
actions: | |
- if: | |
- condition: trigger | |
id: | |
- HA Start | |
then: | |
- delay: | |
hours: 0 | |
minutes: 2 | |
seconds: 0 | |
milliseconds: 0 | |
- stop: "" | |
- variables: | |
boolean_state: "{{ is_state('input_boolean.left_room_lights', 'on') }}" | |
any_light_on: >- | |
{{ not is_state('light.ceiling_lamps_left_room', 'off') or not | |
is_state('light.lamp_left_room_floor_lamp', 'off') or not | |
is_state('light.spot_lights_left_room', 'off') }} | |
current_brightness: >- | |
{{ states('sensor.illuminance_left_room_under_desktop_corrected') | | |
float(default=0) }} | |
target_brightness: >- | |
{{ states('sensor.target_brightness_left_room_outdoor_mirroring') | | |
float }} | |
max_ceiling_contribution: >- | |
{{ | |
states('input_number.max_brightness_contribution_left_room_ceiling_lamps') | |
| float }} | |
max_spot_contribution: >- | |
{{ | |
states('input_number.max_brightness_contribution_left_room_floodlight') | |
| float }} | |
ceiling_contribution_per_step: "{{ max_ceiling_contribution / 255 }}" | |
spot_contribution_per_step: "{{ max_spot_contribution / 255 }}" | |
required_lux: "{{ target_brightness - current_brightness }}" | |
initial_ceiling_brightness: "{{ [(required_lux / ceiling_contribution_per_step), 4] | max | int }}" | |
color_temp: >- | |
{% set sensor_value = | |
states('sensor.presence_left_room_presence_left_room_outdoor_color_temperature') | |
| int %} {% set lower_limit = 1800 %} {% set upper_limit = sensor_value | |
%} {% set brightness = target_brightness | float(default=30) %} {% if | |
brightness >= 30 %} | |
{{ upper_limit }} | |
{% elif brightness <= 10 %} | |
{{ lower_limit }} | |
{% else %} | |
{{ ((brightness - 10) / (30 - 10) * (upper_limit - lower_limit) + lower_limit) | round }} | |
{% endif %} | |
room_color_temp: >- | |
{{ | |
states('sensor.presence_left_room_presence_left_room_left_room_color_temperature') | |
| float(default=color_temp) }} | |
light_current_color_temp: >- | |
{{ color_temp + | |
(states('input_number.light_temperature_delta_left_room') | int) }} | |
new_light_temperature: |- | |
{% if target_brightness | float(default=1.0) < 10 %} | |
1800 | |
{% elif not ( | |
is_state('switch.spotlights_relais_left_room', 'on') and | |
is_state('light.spot_lights_left_room', 'on') and | |
state_attr('light.spot_lights_left_room', 'brightness') == 255 | |
) and (required_lux > 50 or required_lux < -50) %} | |
{{ light_current_color_temp | int(default=1800) }} | |
{% elif ((room_color_temp - color_temp) | abs) > 5 %} | |
{% set color_temp_difference = (color_temp - room_color_temp) * 0.33 %} | |
{% set color_temp_adjustment = 43 if ((color_temp_difference) | abs) > 43 else ((color_temp_difference) | abs) %} | |
{% set temp = light_current_color_temp + (color_temp_adjustment if color_temp_difference > 0 else -color_temp_adjustment) %} | |
{{ [ [temp, 1800] | max, 6500 ] | min | int }} | |
{% else %} | |
{{ light_current_color_temp | int(default=1800) }} | |
{% endif %} | |
- if: | |
- condition: state | |
entity_id: input_boolean.left_room_lights | |
state: "on" | |
for: | |
hours: 0 | |
minutes: 1 | |
seconds: 10 | |
- condition: template | |
value_template: "{{ target_brightness | float(default=1.0) >= 10 }}" | |
then: | |
- target: | |
entity_id: input_number.light_temperature_delta_left_room | |
data: | |
value: "{{ new_light_temperature - color_temp }}" | |
action: input_number.set_value | |
- choose: | |
- conditions: | |
- condition: state | |
entity_id: input_boolean.left_room_lights | |
state: "on" | |
- condition: state | |
entity_id: light.ceiling_lamps_left_room | |
state: "off" | |
- condition: or | |
conditions: | |
- condition: state | |
entity_id: light.spot_lights_left_room | |
state: "off" | |
- condition: state | |
entity_id: switch.spotlights_relais_left_room | |
state: "off" | |
sequence: | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: 10 | |
transition: 0 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 3 | |
milliseconds: 0 | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', 'brightness') != | |
initial_ceiling_brightness }} | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: "{{ [initial_ceiling_brightness, 255] | min | int }}" | |
transition: 2 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
milliseconds: 0 | |
- stop: "" | |
- conditions: | |
- condition: state | |
entity_id: input_boolean.left_room_lights | |
state: "off" | |
- condition: or | |
conditions: | |
- condition: state | |
entity_id: light.ceiling_lamps_left_room | |
state: "on" | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: light.spot_lights_left_room | |
state: "on" | |
- condition: state | |
entity_id: switch.spotlights_relais_left_room | |
state: "on" | |
sequence: | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
- light.spot_lights_left_room | |
data: | |
transition: >- | |
{{ 0 if state_attr('light.ceiling_lamps_left_room', | |
'brightness') < 10 else 5 }} | |
action: light.turn_off | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 2 | |
seconds: 10 | |
milliseconds: 0 | |
- action: switch.turn_off | |
metadata: {} | |
data: {} | |
target: | |
entity_id: switch.spotlights_relais_left_room | |
- stop: "" | |
- conditions: | |
- condition: state | |
entity_id: light.ceiling_lamps_left_room | |
state: "off" | |
- condition: or | |
conditions: | |
- condition: state | |
entity_id: light.spot_lights_left_room | |
state: "off" | |
- condition: state | |
entity_id: switch.spotlights_relais_left_room | |
state: "off" | |
sequence: | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
milliseconds: 0 | |
- stop: "" | |
- variables: | |
spot_lights_on: | | |
{% if is_state('switch.spotlights_relais_left_room', 'off') %} | |
{{ false }} | |
{% else %} | |
{{ is_state('light.spot_lights_left_room', 'on') }} | |
{% endif %} | |
ceiling_brightness: >- | |
{{ state_attr('light.ceiling_lamps_left_room', 'brightness') | | |
int(default=0) }} | |
spot_brightness: >- | |
{{ state_attr('light.spot_lights_left_room', 'brightness') | | |
int(default=0) }} | |
lower_limit: "{{ target_brightness - 0.05 }}" | |
upper_limit: "{{ target_brightness + 0.05 }}" | |
- choose: | |
- conditions: | |
- condition: template | |
value_template: "{{ spot_lights_on and (ceiling_brightness < 255) }}" | |
sequence: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', 'brightness') != | |
255 or state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: 255 | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 7 | |
milliseconds: 0 | |
- conditions: | |
- condition: template | |
value_template: "{{ current_brightness < lower_limit }}" | |
sequence: | |
- variables: | |
needed_ceiling_brightness: "{{ (required_lux / ceiling_contribution_per_step) | int }}" | |
adjusted_ceiling_brightness: > | |
{{ [ceiling_brightness + needed_ceiling_brightness, 255, | |
ceiling_brightness + 26] | min | int }} | |
remaining_required_lux: >- | |
{{ required_lux - (adjusted_ceiling_brightness - | |
ceiling_brightness) * ceiling_contribution_per_step }} | |
needed_spot_brightness: >- | |
{{ (remaining_required_lux / spot_contribution_per_step) | int | |
if remaining_required_lux > 0 else 0 }} | |
adjusted_spot_brightness: >- | |
{{ [spot_brightness + needed_spot_brightness, 255, | |
spot_brightness + 26] | min | int }} | |
- choose: | |
- conditions: | |
- condition: template | |
value_template: "{{ not spot_lights_on }}" | |
sequence: | |
- if: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'brightness') != adjusted_ceiling_brightness }} | |
then: | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: "{{ adjusted_ceiling_brightness }}" | |
transition: 4 | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 5 | |
milliseconds: 0 | |
- if: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
then: | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
kelvin: "{{ new_light_temperature }}" | |
transition: 4 | |
action: light.turn_on | |
- conditions: | |
- condition: template | |
value_template: "{{ spot_lights_on }}" | |
sequence: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'brightness') != 255 or | |
state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: "255" | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- choose: | |
- conditions: | |
- condition: template | |
value_template: >- | |
{{ remaining_required_lux > 20 and ceiling_brightness == | |
255 and not spot_lights_on }} | |
sequence: | |
- action: switch.turn_on | |
target: | |
entity_id: | |
- switch.spotlights_relais_left_room | |
data: {} | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
milliseconds: 0 | |
- target: | |
entity_id: light.spot_lights_left_room | |
data: | |
brightness: 3 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- conditions: | |
- condition: template | |
value_template: >- | |
{{ remaining_required_lux > 0 and ceiling_brightness == | |
255 and spot_lights_on }} | |
sequence: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.spot_lights_left_room', 'brightness') | |
!= adjusted_spot_brightness or | |
state_attr('light.spot_lights_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: light.spot_lights_left_room | |
data: | |
brightness: "{{ adjusted_spot_brightness }}" | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 7 | |
milliseconds: 0 | |
- conditions: | |
- condition: template | |
value_template: "{{ current_brightness > upper_limit }}" | |
sequence: | |
- variables: | |
excess_lux: "{{ current_brightness - target_brightness }}" | |
needed_spot_reduction: "{{ (excess_lux / spot_contribution_per_step) | int }}" | |
needed_ceiling_reduction: "{{ (excess_lux / ceiling_contribution_per_step) | int }}" | |
adjusted_spot_brightness: >- | |
{{ [spot_brightness - needed_spot_reduction, 3, spot_brightness | |
- 26] | max | int }} | |
adjusted_ceiling_brightness: >- | |
{{ [ceiling_brightness - needed_ceiling_reduction, 3, | |
ceiling_brightness - 26] | max | int }} | |
spot_reduction_guarded: >- | |
{{ 3 if adjusted_spot_brightness <= 3 else | |
adjusted_spot_brightness }} | |
ceiling_reduction_guarded: >- | |
{{ 3 if adjusted_ceiling_brightness <= 3 else | |
adjusted_ceiling_brightness }} | |
- choose: | |
- conditions: | |
- condition: template | |
value_template: >- | |
{{ spot_brightness == 3 and spot_lights_on and excess_lux | |
> 5 }} | |
sequence: | |
- target: | |
entity_id: light.spot_lights_left_room | |
action: light.turn_off | |
data: {} | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 10 | |
milliseconds: 0 | |
- action: switch.turn_off | |
target: | |
entity_id: | |
- switch.spotlights_relais_left_room | |
data: {} | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'brightness') != 255 or | |
state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: "255" | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- conditions: | |
- condition: template | |
value_template: "{{ spot_lights_on }}" | |
sequence: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.spot_lights_left_room', 'brightness') | |
!= spot_reduction_guarded or | |
state_attr('light.spot_lights_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: light.spot_lights_left_room | |
data: | |
brightness: "{{ spot_reduction_guarded }}" | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'brightness') != 255 or | |
state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: "255" | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- conditions: | |
- condition: template | |
value_template: "{{ not spot_lights_on }}" | |
sequence: | |
- if: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'brightness') != ceiling_reduction_guarded }} | |
then: | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
brightness: "{{ ceiling_reduction_guarded }}" | |
transition: 4 | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 5 | |
milliseconds: 0 | |
- if: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
then: | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
kelvin: "{{ new_light_temperature }}" | |
transition: 4 | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 7 | |
milliseconds: 0 | |
default: | |
- if: | |
- condition: template | |
value_template: "{{ spot_lights_on }}" | |
then: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.spot_lights_left_room', 'color_temp_kelvin') | |
!= new_light_temperature }} | |
- target: | |
entity_id: light.spot_lights_left_room | |
data: | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- if: | |
- condition: template | |
value_template: "{{ any_light_on }}" | |
then: | |
- condition: template | |
value_template: >- | |
{{ state_attr('light.ceiling_lamps_left_room', | |
'color_temp_kelvin') != new_light_temperature }} | |
- target: | |
entity_id: | |
- light.ceiling_lamps_left_room | |
- light.lamp_left_room_floor_lamp | |
data: | |
transition: 4 | |
kelvin: "{{ new_light_temperature }}" | |
action: light.turn_on | |
- wait_for_trigger: | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "off" | |
to: "on" | |
trigger: state | |
- entity_id: | |
- input_boolean.left_room_lights | |
from: "on" | |
to: "off" | |
trigger: state | |
timeout: | |
hours: 0 | |
minutes: 0 | |
seconds: 7 | |
milliseconds: 0 | |
mode: single |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment