The below automation, built in the UI, but in YAML edit mode, produces an error. The error is caused because the value of the lightset
variable is empty when the automation attempts to call script.lightset_by_label_on
. So this implies to me that the line:
lightset: "{{ trigger_label_map[s9_lightset] }}"
is improperly configured.
Can someone help me figure this out? I’ve done a lot of searching about and this seems to be the syntax to use from what I can see.
AUTOMATION ====
alias: lightset | State Changed
description: ""
triggers:
- trigger: state
entity_id:
- input_select.lightset_lset_app_test_set_state
id: s9_lightset
conditions: []
actions:
- variables:
trigger_label_map:
s9_lightset: Light Set - Lset App Test Light Set
s1_lightset: Light Set - Day Dim - Kitchen
s2_lightset: Light Set - Day Dim
lightset: "{{ trigger_label_map[trigger.id] }}"
- alias: Choose Action Using State
choose:
- conditions:
- condition: template
value_template: "{{ trigger.to_state.state == 'In Process - ON'}}"
alias: IF State is In Process - On
sequence:
- alias: Turn on the Light Set
action: script.lightset_by_label_on
metadata: {}
data:
lightset_label: "{{ lightset }}"
mode: single
ERROR ====
Executed: March 8, 2025 at 8:39:01 AM
Error: UndefinedError: 'dict object' has no attribute ''
Result:
params:
domain: script
service: lightset_by_label_on
service_data:
lightset_label: ''
target: {}
running_script: true
child_id:
domain: script
item_id: lightset_by_label_on
run_id: 53961bf797e8873358428f15c7a1b14a
CONTEXT ====
I am building a mini ‘application’ to manage several different sets of lights that I associate to three different ambient light sensors. A key function of this app is to sense when it is dim during the day, for example if it’s cloudy out the room will be dim, and control it’s associated light-set accordingly. I define each light set using Labels (easy way to add/remove lights from the different light sets). I have a reusable script that takes such a label as input and turns on all the lights in that light set (this script is well tested and works fine) - that’s the script the offending automation is trying to call.
The above automation is a starting point skeleton for what I intend to be a router or controller - it will trigger anytime the state of any of the light sets changes. Based on the state-change it will call an appropriate script to take the appropriate action. In the above I am at present simply trying to sense and call the ‘light set on’ action for my test light set.
The trigger_label_map: variable is meant to lookup the light set label associated to the helper that holds the given light set’s current state - i.e., those helpers, one for each light set, are the triggers for this automation.
No doubt there are other ways to do this and avoid the error I’m getting - but, fundamentally, why is the variable assignment failing?