๐ก Area Lights with Exclusions โ
Controls all light entities inside a target Home Assistant area (turn_on or turn_off), with optional exclusions by entity or label, optional brightness and transition, and a response payload listing the affected lights.
โ๏ธ Parameters โ
| Field | Selector | Required | Default | Description |
|---|---|---|---|---|
area | area | Yes | Target area to pull light entities from. | |
operation | select | No | turn_off | Action to perform (turn_off or turn_on). |
brightness_pct | number | No | Brightness percentage (1 to 100) when turning lights on. | |
transition | number | No | Fade duration in seconds (0 to 60). | |
exclude_labels | text | No | Comma-separated label names whose entities should be skipped. | |
exclude_entities | entity | No | Explicit list of light entities to skip (takes priority over labels). |
๐ How It Works โ
- Expands all entities in
areaand keeps only thelightdomain. - Resolves exclusions: if
exclude_entitiesis non-empty, it is used directly; otherwise,exclude_labelsis split by comma and resolved vialabel_entities. - Only calls
light.turn_onorlight.turn_offif at least one target light remains after filtering, passingbrightness_pctandtransitiononly when provided. - Ends with
response_variable: result, returningarea,operation,lights_in_area,excluded_entities,affected_entities, andlights_turned_off.
๐งช Example Call โ
yaml
action: script.area_lights_control_with_exclusions
data:
area: living_room
operation: turn_off
transition: 2
exclude_labels: always_on, night_light๐ Full Script YAML โ
Import in Home Assistant
Go to Settings -> Automations & Scenes -> Scripts -> Add Script -> Edit in YAML and paste the configuration below. Use the language selector to switch between English and Italian.
alias: ๐ก Area lights control with exclusions
description: >-
Controls (turns on or off) all lights inside a specified area,
optionally excluding:
โข all entities matching specific labels
โข or an explicit list of entities (takes priority)
๐ Parameters:
- Area: target area to select lights from (required)
- Operation: turn_off (default) or turn_on (optional)
- Brightness (%): optional brightness percentage when turning on (keeps the
previous state if omitted)
- Transition: fade duration in seconds (optional)
- Labels to exclude: comma-separated labels (optional)
- Entities to exclude: manual list of excluded entities (optional)
๐ Returns:
โข final list of affected lights
icon: mdi:lightbulb-multiple-outline
mode: single
fields:
area:
name: Area
description: ๐ Target area to select lights from
required: true
selector:
area: {}
operation:
name: Operation
description: 'โก Choose whether to turn lights on or off (optional, default: Turn off)'
required: false
default: turn_off
selector:
select:
options:
- label: ๐ Turn off
value: turn_off
- label: ๐ก Turn on
value: turn_on
brightness_pct:
name: Brightness (%)
description: >-
๐ Brightness percentage to apply when turning on (optional: if omitted,
lights turn on at their previous brightness)
required: false
selector:
number:
min: 1
max: 100
unit_of_measurement: '%'
transition:
name: Transition (seconds)
description: โฑ๏ธ Fade transition duration in seconds (optional)
required: false
selector:
number:
min: 0
max: 60
unit_of_measurement: s
exclude_labels:
name: Labels to exclude
description: ๐ท๏ธ Comma-separated list of labels to exclude (optional)
required: false
selector:
text:
multiline: false
exclude_entities:
name: Entities to exclude
description: >-
๐ซ Explicit list of entities to exclude (optional, takes priority over
labels)
required: false
selector:
entity:
multiple: true
filter:
domain: light
sequence:
- alias: ๐งฎ Compute area lights and exclusions
note: >-
Filters all light entities belonging to the area, resolves exclusions,
and builds the final target list
variables:
operation_mode: '{{ operation | default(''turn_off'') }}'
lights_in_area: |
{{ expand(area_entities(area))
| selectattr('domain', 'equalto', 'light')
| map(attribute='entity_id')
| list }}
entities_to_exclude: |
{% if exclude_entities is defined and exclude_entities | count > 0 %}
{{ exclude_entities }}
{% elif exclude_labels is defined and exclude_labels | length > 0 %}
{{ exclude_labels.split(',')
| map('trim')
| map('lower')
| map('label_entities')
| sum(start=[]) }}
{% else %}
[]
{% endif %}
final_entities: |
{{ lights_in_area
| reject('in', entities_to_exclude)
| list }}
result:
area: '{{ area }}'
operation: '{{ operation_mode }}'
lights_in_area: '{{ lights_in_area }}'
excluded_entities: '{{ entities_to_exclude }}'
affected_entities: '{{ final_entities }}'
lights_turned_off: '{{ final_entities }}'
- alias: ๐ฆ Check target entities and send light command
note: >-
Sends the command only if at least one light entity remains after applying
exclusion filters
if:
- alias: ๐ Check non-empty entity list
note: >-
Verifies that the final list of target lights contains at least one
entity
condition: template
value_template: '{{ final_entities | count > 0 }}'
then:
- alias: ๐ Dispatch command by operation
note: >-
Selects and executes either turn_on or turn_off based on
operation_mode
choose:
- alias: ๐ก Turn on area lights (turn_on)
note: >-
Turns on target lights, applying brightness and transition only if
explicitly provided
conditions:
- alias: โก Requested operation equals turn_on
note: Checks if the requested operation matches turn_on
condition: template
value_template: '{{ operation_mode == ''turn_on'' }}'
sequence:
- alias: ๐ก Send turn_on with optional parameters
note: >-
Calls light.turn_on with the filtered entity list, including
brightness_pct and transition only when set
action: light.turn_on
target:
entity_id: '{{ final_entities }}'
data: >-
{% set b = brightness_pct | int if (brightness_pct is defined
and brightness_pct is not none and (brightness_pct | string |
trim) != '') else none %} {% set t = transition | int if
(transition is defined and transition is not none and
(transition | string | trim) != '') else none %} {% if b is
not none and t is not none %}
{{ {'brightness_pct': b, 'transition': t} }}
{% elif b is not none %}
{{ {'brightness_pct': b} }}
{% elif t is not none %}
{{ {'transition': t} }}
{% else %}
{{ {} }}
{% endif %}
- alias: ๐ Turn off area lights (turn_off)
note: >-
Turns off target lights, applying the optional transition if
provided
conditions:
- alias: โก Requested operation equals turn_off
note: >-
Checks if the requested operation matches turn_off or the
default value
condition: template
value_template: '{{ operation_mode == ''turn_off'' }}'
sequence:
- alias: ๐ Send turn_off with optional transition
note: >-
Calls light.turn_off with the filtered entity list and the
optional transition parameter
action: light.turn_off
target:
entity_id: '{{ final_entities }}'
data: >-
{% set t = transition | int if (transition is defined and
transition is not none and (transition | string | trim) != '')
else none %} {% if t is not none %}
{{ {'transition': t} }}
{% else %}
{{ {} }}
{% endif %}
- alias: ๐ Finish script and return response
note: >-
Completes script execution and returns the result object containing the
list of modified entities
stop: Script completed
response_variable: result