I am intrigued by the selector target, but had problems getting the behaviour I expected. I want to use the target and no matter what I select, I want a list of entities. I spend a few weeks banging my head against the wall, and then I found this Iterate over Target Selector post.
I got inspired to create two scripts (the second one using on the first)
Script get_entities
sequence:
- variables:
entities: >-
{{lights_sources.entity_id if lights_sources.entity_id is list else []
if [lights_sources.entity_id] is defined else []}}
areas: >-
{{(lights_sources.area_id | map('area_entities') | list) | sum(start=[])
if lights_sources.area_id is list else
area_entities(lights_sources.area_id)}}
devices: >-
{{(lights_sources.device_id | map('device_entities') | list) |
sum(start=[]) if lights_sources.device_id is list else
device_entities(lights_sources.device_id) }}
labels: >-
{{(lights_sources.label_id | map('label_entities') | list) |
sum(start=[]) if lights_sources.label_id is list else
label_entities(lights_sources.label_id) }}
all_entities: >-
{{(entities + areas + devices + labels) | select('match', 'light.') |
unique | list}}
return_value: |
{{ {"entities": all_entities} }}
- stop: Sources
response_variable: return_value
alias: Get entities
description: ""
fields:
lights_sources:
selector:
target:
entity:
domain: light
required: true
name: Input source
description: Input source
domain:
selector:
select:
options:
- light.
- switch.
name: Pick a domain
required: true
This script uses a target selector as a source and a domain selector (including .) as a domain filter.
The script returns a list of entities
.
The second script get_entities_with_exclude
calls the first script
sequence:
- action: script.get_entities
metadata: {}
data:
lights_sources: "{{ sources }}"
domain: "{{ domain }}"
response_variable: sources_out
- action: script.get_entities
metadata: {}
data:
lights_sources: "{{ exclude }}"
domain: "{{ domain }}"
response_variable: exclude_out
- variables:
entities: "{{ sources_out.entities | reject('in', exclude_out.entities) | list }}"
result: |
{{ {'entities': entities} }}
- stop: entities
response_variable: result
fields:
sources:
selector:
target: {}
name: sources
description: Input sources
required: true
exclude:
selector:
target: {}
name: exclude
domain:
selector:
select:
options:
- light.
- switch.
name: Domain
required: true
This script has an extra field to exclude entities.
I use this script in my automations mainly to select light entities. I have created areas and I have created labels for mood lightning. Now I can select the dining room lights without and specifiy that I want to exlude the mood lighting in this area.
An example of a simple automation that reacts to a dummy switch helper.
alias: Test toggle lights in dining room without changing mood lights
description: ""
triggers:
- trigger: state
entity_id:
- input_boolean.dummy
conditions: []
actions:
- action: script.get_entities_with_exclude
metadata: {}
data:
sources:
area_id: dining_room
exclude:
label_id: mood_lighting
domain: light.
response_variable: light_entities
- action: light.toggle
metadata: {}
data: {}
target:
entity_id: "{{light_entities.entities}}"
mode: single
Maybe some of you can use it. Tried to make it as straightforward as possible including a simple use case.