Last active
May 8, 2021 09:55
-
-
Save dschanoeh/993013ffe7e1c1d6413cce57bac77278 to your computer and use it in GitHub Desktop.
Home Assistant Light Control Pattern
This file contains hidden or 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: 'Livingroom lights alone off' | |
trigger: | |
- platform: state | |
entity_id: binary_sensor.hallway_motion | |
to: 'on' | |
- platform: state | |
entity_id: binary_sensor.bedroom_motion | |
to: 'on' | |
condition: | |
condition: and | |
conditions: | |
- condition: numeric_state | |
entity_id: sensor.people_present | |
above: 0 | |
below: 2 | |
- condition: state | |
entity_id: binary_sensor.livingroom_motion | |
state: 'off' | |
for: | |
seconds: 10 | |
- condition: state | |
entity_id: input_select.livingroom_light_mode | |
state: 'auto' | |
action: | |
- service: light.turn_off | |
data: | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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: 'Adjust living room lights brightness' | |
trigger: | |
- platform: time | |
minutes: '/1' | |
seconds: 00 | |
- platform: state | |
entity_id: media_player.tv | |
condition: | |
condition: and | |
conditions: | |
- condition: state | |
entity_id: input_select.livingroom_light_mode | |
state: 'auto' | |
- condition: state | |
entity_id: group.livingroom_lights | |
state: 'on' | |
action: | |
- service: light.turn_on | |
data_template: | |
brightness_pct: > | |
{{ states.sensor.target_brightness_livingroom.state|int}} | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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: 'Livingroom lights off at night' | |
trigger: | |
- platform: state | |
entity_id: binary_sensor.livingroom_motion | |
to: 'off' | |
for: | |
seconds: 30 | |
condition: | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: input_select.livingroom_light_mode | |
state: 'auto' | |
- condition: state | |
entity_id: input_boolean.bedtime | |
state: 'on' | |
action: | |
- service: light.turn_off | |
data: | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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: 'Livingroom lights presence off' | |
trigger: | |
- platform: state | |
entity_id: binary_sensor.livingroom_motion | |
to: 'off' | |
for: | |
minutes: 20 | |
- platform: state | |
entity_id: binary_sensor.livingroom_motion | |
to: 'off' | |
for: | |
minutes: 2 | |
condition: | |
condition: and | |
conditions: | |
- condition: state | |
entity_id: input_select.livingroom_light_mode | |
state: 'auto' | |
- condition: state | |
entity_id: input_boolean.bedtime | |
state: 'off' | |
- condition: or | |
conditions: | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: binary_sensor.livingroom_motion | |
state: 'off' | |
for: | |
minutes: 20 | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: binary_sensor.livingroom_motion | |
state: 'off' | |
for: | |
minutes: 2 | |
- condition: template | |
value_template: "{{ (as_timestamp(now())-as_timestamp(states.group.livingroom_lights.last_changed)) <= 300 }}" | |
action: | |
- service: light.turn_off | |
data: | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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: 'Living room lights presence on' | |
trigger: | |
- platform: state | |
entity_id: binary_sensor.livingroom_motion | |
to: 'on' | |
condition: | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: group.livingroom_lights | |
state: 'off' | |
- condition: state | |
entity_id: input_select.livingroom_light_mode | |
state: 'auto' | |
action: | |
- service: light.turn_on | |
data_template: | |
brightness_pct: > | |
{{ states.sensor.target_brightness_livingroom.state|int}} | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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: 'Livingroom lights switch off' | |
trigger: | |
- platform: state | |
entity_id: input_select.livingroom_light_mode | |
to: 'off' | |
action: | |
- service: light.turn_off | |
data: | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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: 'Living room lights switch on' | |
trigger: | |
- platform: state | |
entity_id: input_select.livingroom_light_mode | |
to: 'on' | |
action: | |
- service: light.turn_on | |
data_template: | |
brightness_pct: 100 | |
entity_id: | |
- group.livingroom_lights |
This file contains hidden or 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
- platform: template | |
sensors: | |
target_brightness_outside: | |
friendly_name: "Target brightness outside" | |
unit_of_measurement: "%" | |
value_template: > | |
{% set bedtime_value = 10 %} | |
{% set maximum = 100 %} | |
{% set a = -0.0037 %} | |
{% set c = 0 %} | |
{% set n = 100-c %} | |
{% set minimum = 20 %} | |
{% set i = states('sensor.light_intensity') | float %} | |
{% set p = n*e**(a*i)+c %} | |
{% if is_state('input_boolean.bedtime','on') %} | |
{{ bedtime_value | int }} | |
{% else %} | |
{%if p < minimum %} | |
0 | |
{% elif p > maximum %} | |
{{ maximum }} | |
{% else %} | |
{{ p | int}} | |
{% endif %} | |
{% endif%} | |
target_brightness_inside: | |
friendly_name: "Target brightness inside" | |
unit_of_measurement: "%" | |
value_template: > | |
{% set bedtime_value = 10 %} | |
{% set maximum = 100 %} | |
{% set a = -0.0037 %} | |
{% set c = 20 %} | |
{% set n = 100-c %} | |
{% set minimum = 20 %} | |
{% set i = states('sensor.light_intensity') | float %} | |
{% set p = n*e**(a*i)+c %} | |
{% if is_state('input_boolean.bedtime','on') %} | |
{{ bedtime_value | int }} | |
{% else %} | |
{%if p < minimum %} | |
0 | |
{% elif p > maximum %} | |
{{ maximum }} | |
{% else %} | |
{{ p | int}} | |
{% endif %} | |
{% endif%} | |
target_brightness_livingroom: | |
friendly_name: "Target brightness livingroom" | |
unit_of_measurement: "%" | |
value_template: > | |
{% if is_state('media_player.tv','playing') or (as_timestamp(now())-as_timestamp(states.media_player.tv.last_changed)) < 60 %} | |
{% if states('sensor.target_brightness_outside') | int >= 20 %} | |
20 | |
{% else %} | |
0 | |
{% endif %} | |
{% else %} | |
{{ states.sensor.target_brightness_outside.state }} | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sensor.light_intensity is the output of a BH1750 light sensor (in lux). Now what the templates are supposed to do is map this to a target brightness for lights between 0 and 100%. The exponential function it's using (p = ne**(ai)+c) doesn't have any physical background (that I know of) but simply was a pretty good fit for my needs. Parameters can be tweaked as needed of course. There is a second variant ("target_brightness_inside") where the light is always set to 20% more by setting c to 20 (since it's used in rooms that don't have windows).
Finally, there is some logic that limits the values to between 0 and 100% and not more than that and sets it to a fixed value of 10% for the night when someone is walking around in the middle of the night.
I hope that helps.