Some folks want to see if they can improve my lighting automations and scripts. I’m going to post some of what I have here to avoid going off topic in my other thread
.
Here are my basic functions:
- If occupancy detected, turn on lights.
- If occupancy cleared, turn off lights.
- If button up pressed, turn on lights to the daytime or nighttime setting depending on time of day and disable occupancy sensing.
- If button up x 2 pressed, re-enable occupancy sensing.
- If button up held, enable daytime lighting even if it’s nighttime.
- If button down pressed, turn off lights and disable occupancy sensing.
- If button down held, disable occupancy sensing (if you wanna keep the lights in their current state).
Lighting Automation YAMLs
Automation
All lighting automations share the same basic layout with Trigger IDs and call the same script.
I’m passing in a helper entity to the script: “are occupancy sensors controlling the lights?”.
There’s something I want to add but haven’t tested yet. Another trigger for when the lights go off. I wanna ensure occupancy sensing gets re-enabled because I could turn off lights in my dashboard or via voice, and this automation won’t run the “Button: Turn off Lights” trigger.
I could have other triggers in here for it being daytime or nighttime, but to avoid complicating things I put those into looping scripts instead.
Automation YAML
alias: Control Bedroom Hallway Lights
description: ""
triggers:
- device_id: 359bc5a5f510d587696d3042b560c8b7
domain: zha
type: press
subtype: Up
trigger: device
id: "Button: Turn on Lights"
- device_id: 359bc5a5f510d587696d3042b560c8b7
domain: zha
type: remote_button_double_press
subtype: Up
trigger: device
id: "Button: Turn on Occupancy Sensing"
- device_id: 359bc5a5f510d587696d3042b560c8b7
domain: zha
type: hold
subtype: Up
trigger: device
id: "Button: Turn on Daytime Lighting"
- device_id: 359bc5a5f510d587696d3042b560c8b7
domain: zha
type: press
subtype: Down
trigger: device
id: "Button: Turn off Lights"
- device_id: 359bc5a5f510d587696d3042b560c8b7
domain: zha
type: hold
subtype: Down
trigger: device
id: "Button: Turn off Occupancy Sensing"
- entity_id:
- binary_sensor.bedroom_hallway_occupancy_sensors
to: "on"
id: "Occupancy: Turn on Lights"
trigger: state
- entity_id:
- binary_sensor.bedroom_hallway_occupancy_sensors
to: "off"
id: "Occupancy: Turn off Lights"
trigger: state
for:
hours: 0
minutes: 0
seconds: 20
conditions: []
actions:
- metadata: {}
data:
nighttime_brightness: 7
lights:
- light.bedroom_hallway_lights
trigger_id: "{{ trigger.id }}"
daytime_lighting_mode: Adaptive
nighttime_lighting_mode: Nighttime
occupancy_sensing: input_boolean.bedroom_hallway_occupancy_sensing
id: Bedroom Hallway
action: script.control_button_occupancy_day_night_lights
mode: restart
Main Script
All of the main logic for controlling lighting is in this script.
This script handles calling other scripts based on the Trigger ID.
Main Script YAML
alias: Control Button-Occupancy Day-Night Lights
fields:
lights:
name: Lights
description: A list of lights to control.
example: light.living_room_lights, light.kitchen_lights
required: true
selector:
entity:
multiple: true
domain: light
occupancy_sensing:
name: Motion Sensor Toggle
description: Toggle that controls whether motion sensing is on or off.
example: input_boolean.living_room_occupancy_sensing
required: false
selector:
entity:
multiple: false
domain: input_boolean
daytime_lighting_mode:
selector:
select:
options:
- Adaptive
- Bright
name: Daytime Lighting Mode
default: Adaptive
required: true
description: >-
Lighting style that turns on during the daytime. Adaptive is normal and
changes based on the weather. Bright is great for closets or rooms where
you want always-bright lights.
nighttime_lighting_mode:
selector:
select:
options:
- Nighttime
- Adaptive
- Bright
- Scene
- None
name: Nighttime Lighting Mode
default: Nighttime
required: true
description: >-
Lighting style that turns on during the evening. Nighttime is a calmer
light temperature whereas Adaptive and Bright are the same as daytime
lights.
nighttime_brightness:
selector:
number:
min: 1
max: 100
step: 1
name: Nighttime Brightness (Optional)
description: >-
Only used when selecting "Nighttime". The brightness setting used during
nighttime lighting.
required: false
default: 20
nighttime_scene:
selector:
entity:
domain: scene
name: Nighttime Scene (Optional)
description: >-
Only used for the "Scene" mode. Meant for specific rooms and lighting
needs where only specific lights need to be turned on with specific
brightness and color values.
trigger_id:
selector:
template: {}
name: Trigger ID
required: true
description: "Trigger ID used to determine which path use when controlling lights. "
default: "{{ trigger.id }}"
id:
selector:
text: null
name: ID
description: >-
This is an always-running script until lights turn off, so you need to
pass a unique ID, so its operation isn't duplicated.
required: true
sequence:
- data:
name: "Control Lights:"
message: >
{% set light_names = lights | map('state_attr', 'friendly_name') |
join(', ') %} "{{ trigger_id }}" ➡️ "{{ light_names }}".
enabled: true
alias: Log Trigger ID and Light Names
action: logbook.log
- choose:
- conditions:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Turn on Lights\" }}"
alias: "Trigger ID is \"Button: Turn on Lights\""
- alias: "Trigger ID is \"Button: Toggle Lights\" and lights are off"
condition: and
conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Toggle Lights\" }}"
alias: "Trigger ID is \"Button: Toggle Lights\""
- alias: Are lights are off?
condition: template
value_template: >-
{{ lights | map('states') | select('eq', 'on') | list |
length == 0 }}
- condition: and
conditions:
- condition: template
value_template: "{{ trigger_id == \"Occupancy: Turn on Lights\" }}"
alias: "Trigger ID is \"Occupancy: Turn on Lights\""
- condition: template
value_template: >-
{{ is_state(occupancy_sensing, 'on') if occupancy_sensing
is defined else True }}
alias: Motion sensor toggle is "on"
alias: >-
Trigger ID is "Occupancy: Turn on Lights" and motion sensor
toggle is on
alias: If Button or Occupancy turn on lights
sequence:
- alias: Turn on lights and turn off occupancy sensing
parallel:
- alias: Turn off occupancy sensing if button pressed
if:
- condition: template
value_template: >-
{{ trigger_id == "Button: Turn on Lights" or trigger_id ==
"Button: Toggle Lights" }}
alias: >-
Trigger ID is "Button: Turn on Lights" or "Button: Toggle
Lights"
then:
- alias: Turn off Occupancy Sensing
metadata: {}
data:
occupancy_sensing: "{{ occupancy_sensing }}"
action_name: Turn off Occupancy Sensing
action: script.control_motion_lights
- metadata: {}
data:
daytime_lighting_mode: "{{ daytime_lighting_mode }}"
nighttime_lighting_mode: "{{ nighttime_lighting_mode }}"
nighttime_brightness: "{{ nighttime_brightness | default(0) }}"
nighttime_scene: "{{ nighttime_scene | default('') }}"
lights: "{{ lights }}"
id: "{{ id }}"
action: script.turn_on_day_night_adaptive_light
enabled: false
- action: script.turn_on
metadata: {}
data:
variables:
daytime_lighting_mode: "{{ daytime_lighting_mode }}"
nighttime_lighting_mode: "{{ nighttime_lighting_mode }}"
nighttime_brightness: "{{ nighttime_brightness | default(0) }}"
nighttime_scene: "{{ nighttime_scene | default('') }}"
lights: "{{ lights }}"
id: "{{ id }}"
target:
entity_id:
- script.turn_on_day_night_adaptive_light
enabled: true
alias: Turn on Lights
- conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Turn on Daytime Lighting\" }}"
alias: "Trigger ID is \"Button: Turn on Daytime Lighting\""
sequence:
- alias: Turn on lights and turn off occupancy sensing
parallel:
- alias: Turn off Occupancy Sensing
metadata: {}
data:
occupancy_sensing: "{{ occupancy_sensing }}"
action_name: Turn off Occupancy Sensing
action: script.control_motion_lights
- metadata: {}
data:
lights: "{{ lights }}"
id: "{{ id }}"
action: script.turn_on_lights_for_adaptive_lighting
enabled: false
- action: script.turn_on
metadata: {}
data:
variables:
lights: "{{ lights }}"
id: "{{ id }}"
target:
entity_id:
- script.turn_on_lights_for_adaptive_lighting
enabled: true
alias: Turn on Daytime Lights
- conditions:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Turn off Lights\" }}"
alias: "Trigger ID is \"Button: Turn off Lights\""
- condition: and
conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Toggle Lights\" }}"
alias: "Trigger ID is \"Button: Toggle Lights\""
- condition: template
value_template: >-
{{ lights | map('states') | select('eq', 'on') | list |
length > 0 }}
alias: At least one light is "on"
alias: "Trigger ID is \"Button: Toggle Lights\" and a light is on"
- condition: and
conditions:
- condition: template
value_template: "{{ trigger_id == \"Occupancy: Turn off Lights\" }}"
alias: "Trigger ID is \"Occupancy: Turn off Lights\""
- condition: template
value_template: >-
{{ is_state(occupancy_sensing, 'on') if occupancy_sensing
is defined else True }}
alias: Motion sensor toggle is "on"
alias: >-
Trigger ID is "Occupancy: Turn off Lights" and motion sensor
toggle is on
alias: If Button or Occupancy turn off lights
sequence:
- alias: Turn off lights and turn on occupancy sensing
parallel:
- metadata: {}
data:
transition: 1.5
lights: "{{ lights }}"
action: script.turn_off_lights
- alias: Turn on Occupancy Sensing
metadata: {}
data:
occupancy_sensing: "{{ occupancy_sensing }}"
action_name: Turn on Occupancy Sensing
action: script.control_motion_lights
alias: Turn off Lights
- conditions:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Turn on Occupancy Sensing\" }}"
alias: "Trigger ID is \"Button: Turn on Occupancy Sensing\""
- alias: "Trigger ID is \"Occupancy: Turn on Occupancy Sensing\""
condition: template
value_template: "{{ trigger_id == \"Occupancy: Turn on Occupancy Sensing\" }}"
sequence:
- alias: Turn on occupancy sensing
metadata: {}
data:
occupancy_sensing: "{{ occupancy_sensing }}"
action_name: Turn on Occupancy Sensing
action: script.control_motion_lights
alias: Turn on Occupancy Sensing
- conditions:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger_id == \"Button: Turn off Occupancy Sensing\" }}"
alias: "Trigger ID is \"Button: Turn off Occupancy Sensing\""
- condition: template
value_template: "{{ trigger_id == \"Occupancy: Turn off Occupancy Sensing\" }}"
alias: "Trigger ID is \"Occupancy: Turn off Occupancy Sensing\""
sequence:
- alias: Turn off occupancy sensing
metadata: {}
data:
occupancy_sensing: "{{ occupancy_sensing }}"
action_name: Turn off Occupancy Sensing
action: script.control_motion_lights
alias: Turn off Occupancy Sensing
description: >-
Turns on lights with adaptive lighting during the day, but at night, it turns
on a much dimmer version.
icon: mdi:theme-light-dark
mode: parallel
max: 100
“Turn On” Lighting Script
I have two long-running lighting scripts. This one controls the Adaptive lighting one.
This one controls whether we’re in daytime or nighttime lighting mode.
'Turn On' Lighting Script YAML
alias: Turn on Lights for Day & Night Adaptive Lighting
description: >-
Turns on lights with adaptive lighting during the day, but at night, it turns
on a much dimmer version.
icon: mdi:theme-light-dark
mode: parallel
fields:
lights:
name: Lights
description: The list of lights to turn on.
example: light.living_room_lights, light.kitchen_lights
required: true
selector:
entity:
multiple: true
domain: light
daytime_lighting_mode:
selector:
select:
options:
- Adaptive
- Bright
name: Daytime Lighting Mode
default: Adaptive
required: true
description: >-
Lighting style that turns on during the daytime. Adaptive is normal and
changes based on the weather. Bright is great for closets or rooms where
you want always-bright lights.
nighttime_lighting_mode:
selector:
select:
options:
- Nighttime
- Adaptive
- Bright
- Scene
- None
multiple: false
name: Nighttime Lighting Mode
default: Nighttime
required: true
description: >-
Lighting style that turns on during the nighttime. Nighttime is a calmer
light temperature whereas Adaptive and Bright are the same as daytime
lights.
nighttime_brightness:
selector:
number:
min: 1
max: 100
step: 1
name: Nighttime Brightness (Optional)
description: >-
Brightness setting used during nighttime lighting. Not used for other
nighttime lighting modes.
required: false
default: 20
nighttime_scene:
selector:
entity:
domain: scene
name: Nighttime Scene (Optional)
description: >-
Meant for specific rooms and lighting needs where only specific lights
need to be turned on with specific brightness and color values.
id:
selector:
text: null
name: ID
description: >-
This is an always-running script until lights turn off, so you need to
pass a unique ID, so its operation isn't duplicated.
required: true
sequence:
- event: turn_on_day_night_adaptive_light
event_data:
id: "{{ id }}"
- alias: Repeat until lights are off
repeat:
sequence:
- event: turn_on_lights_for_adaptive_lighting
event_data:
id: "{{ id }}"
- parallel:
- alias: Trigger repeat or stop script
sequence:
- alias: >-
Wait for time to be daytime, nighttime, lights off, or
duplicate call
wait_for_trigger:
- trigger: time
at: input_datetime.morning_lighting_time
enabled: true
- trigger: time
at: input_datetime.nighttime_lighting_time
enabled: true
- trigger: template
value_template: >-
{{ lights | map('states') | select('eq', 'on') | list |
length == 0 }}
alias: When lights are off
- trigger: event
event_type: turn_on_day_night_adaptive_light
enabled: true
event_data:
id: "{{ id }}"
continue_on_timeout: false
- alias: Stop if duplicate call
if:
- alias: Is duplicate call
condition: template
value_template: |-
{{
wait.trigger is defined
and wait.trigger.platform == "event"
and wait.trigger.event.data.id == id
}}
then:
- stop: Duplicate call
enabled: true
- action: logbook.log
metadata: {}
data:
message: "{{ id }}"
name: Stopped Day & Night Adaptive Lighting
- alias: Turn on lighting mode
sequence:
- alias: Choose daytime or nighttime lighting mode
if:
- condition: time
after: input_datetime.morning_lighting_time
before: input_datetime.nighttime_lighting_time
then:
- alias: Run chosen daytime lighting mode
choose:
- conditions:
- condition: template
value_template: "{{ daytime_lighting_mode == \"Adaptive\" }}"
alias: Is adaptive lighting mode
sequence:
- metadata: {}
data:
lights: "{{ lights }}"
id: "{{ id }}"
action: script.turn_on_lights_for_adaptive_lighting
enabled: false
- action: script.turn_on
metadata: {}
data:
variables:
lights: "{{ lights }}"
id: "{{ id }}"
target:
entity_id:
- script.turn_on_lights_for_adaptive_lighting
enabled: true
- conditions:
- condition: template
value_template: "{{ daytime_lighting_mode == \"Bright\" }} "
alias: Is bright lighting mode
sequence:
- metadata: {}
data:
lights: "{{ lights }}"
enabled: true
action: script.turn_on_lights_for_bright_areas
else:
- alias: Run chosen nighttime lighting mode
choose:
- conditions:
- condition: template
value_template: "{{ nighttime_lighting_mode == \"Nighttime\" }}"
alias: Is nighttime lighting mode
sequence:
- data:
lights: "{{ lights }}"
nighttime_brightness: "{{ nighttime_brightness }}"
action: script.turn_on_nighttime_lights
- conditions:
- condition: template
value_template: "{{ nighttime_lighting_mode == \"Adaptive\" }}"
alias: Is adaptive lighting mode
sequence:
- metadata: {}
data:
lights: "{{ lights }}"
id: "{{ id }}"
action: script.turn_on_lights_for_adaptive_lighting
enabled: false
- action: script.turn_on
metadata: {}
data:
variables:
lights: "{{ lights }}"
id: "{{ id }}"
target:
entity_id:
- script.turn_on_lights_for_adaptive_lighting
enabled: true
- conditions:
- condition: template
value_template: "{{ nighttime_lighting_mode == \"Bright\" }} "
alias: Is bright lighting mode
sequence:
- metadata: {}
data:
lights: "{{ lights }}"
enabled: true
action: script.turn_on_lights_for_bright_areas
- conditions:
- condition: template
value_template: "{{ nighttime_lighting_mode == \"Scene\" }}"
alias: Is scene lighting mode
sequence:
- metadata: {}
target:
entity_id: "{{ nighttime_scene }}"
data:
transition: 2
action: scene.turn_on
until:
- alias: If lights off or duplicate call
condition: or
conditions:
- alias: Lights are off
condition: template
value_template: >-
{{ lights | map('states') | select('eq', 'on') | list | length
== 0 }}
- alias: If a duplicate call was made
condition: template
value_template: |-
{{
wait is defined
and wait.trigger is defined
and wait.trigger.platform == "event"
and wait.trigger.event.data.id == id
}}
max: 100
Adaptive (Daytime) Lighting Script
This script’s purpose is to change the lighting mode based on the value of my global light temperature and brightness helpers. Those get affected by the cloud coverage values in a separate Weather-based automation.
My purpose is changing the light color to match the color of light outside. It’s unimportant how those values get changed in this automation, only that it listens for changes in those values.
Adaptive (Daytime) Lighting Script YAML
alias: Turn on Lights for Adaptive Lighting
fields:
lights:
name: Lights
description: The list of lights to turn on.
example: light.living_room_lights, light.kitchen_lights
required: true
selector:
entity:
multiple: true
domain: light
id:
selector:
text: {}
name: ID
description: >-
This is an always-running script until lights turn off, so you need to
pass a unique ID, so its operation isn't duplicated.
required: true
sequence:
- event: turn_on_lights_for_adaptive_lighting
event_data:
id: "{{ id }}"
- alias: Repeat until script stopped
repeat:
sequence:
- alias: Turn on lights and stop or loop again
parallel:
- alias: Trigger repeat or stop script
sequence:
- alias: >-
Wait for color temperature, brightness, lights off, or
duplicate call
wait_for_trigger:
- trigger: state
entity_id:
- input_select.light_color_temperature
- trigger: state
entity_id:
- input_number.light_brightness_percentage
- trigger: template
value_template: >-
{{ lights | map('states') | select('eq', 'on') | list |
length == 0 }}
alias: When lights are off
enabled: true
- trigger: event
event_type: turn_on_lights_for_adaptive_lighting
enabled: true
event_data:
id: "{{ id }}"
continue_on_timeout: false
- alias: Stop if duplicate call
if:
- alias: Is duplicate call
condition: template
value_template: |-
{{
wait.trigger is defined
and wait.trigger.platform == "event"
and wait.trigger.event.data.id == id
}}
then:
- stop: Duplicate call
enabled: true
- action: script.turn_on_lights
metadata: {}
data:
lights: "{{ lights }}"
brightness: "{{ states('input_number.light_brightness_percentage') }}"
kelvin: "{{ states('input_select.light_color_temperature') }}"
transition: 1.5
- alias: Turn on lights
sequence:
- alias: Log Light Names
data:
name: "Turn on Lights for Day & Night Adaptive Lighting:"
message: >
{% set light_names = lights | map('state_attr',
'friendly_name') | join(', ') %} "{{ light_names }}".
enabled: true
action: logbook.log
- parallel:
- metadata: {}
data:
brightness_pct: >-
{{ states('input_number.light_brightness_percentage')
}}
kelvin: "{{ states('input_select.light_color_temperature') }}"
transition: 1.5
target:
entity_id: "{{ lights }}"
action: light.turn_on
alias: Set light brightness and color temperature
- alias: >-
Set white LED brightness and turn off RGB LEDs in light
strips
metadata: {}
data:
rgbw_color:
- 0
- 0
- 0
- >
{{
(states('input_number.light_brightness_percentage')
| int) / 100 * 255 }} transition: 1.5
target:
entity_id: "{{ lights }}"
action: light.turn_on
alias: Configure light settings
enabled: false
until:
- alias: If lights off or duplicate call
condition: or
conditions:
- alias: Lights are off
condition: template
value_template: >-
{{ lights | map('states') | select('eq', 'on') | list | length
== 0 }}
- alias: If a duplicate call was made
condition: template
value_template: |-
{{
wait is defined
and wait.trigger is defined
and wait.trigger.platform == "event"
and wait.trigger.event.data.id == id
}}
enabled: true
description: >-
Turn on lights to a brightness value based on the time of day and a
temperature relative to the current weather.
icon: mdi:lightbulb-auto
mode: parallel
max: 100
Other Scripts
I left out the other scripts because they’re pretty self-explanatory:
- Turn off lights.
- Turn on Bright lights.
- Turn on Nighttime lighting.
- Stop existing adaptive lighting scripts for a lighting area.






