Using Nested Variable in an Automation

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?

When I tested your variable section, it worked as expected. Are you sure these items have been pasted correctly? The script in your error message is spelled differently than the script in your automation.

You could consider simplifying this. There’s no restriction against spaces and dashes in trigger ids, so you could use Light Set - Lset App Test Light Set as the trigger id instead of s9_lightset and not have the variables section at all.

I did goof on the script name in the automation yaml I pasted in (I had saved that in a text editor earlier) - I did correct that in the actual yaml that produced the error message, which is the correct error. I’ll edit the original post to fix that.

So why does it work for you and not me?? I would like to figure that out because I’m going to need to be able to do that as I build this out.

Your suggestion on the trigger ID is a good one. That would solve for this particular case. But as I go forward I’m going to need to be able to use variables in the way I have it here.

I just experimented with replacing the assignment line with the below, explicitly using a key from the trigger_label_map:, and it worked. But so far can’t get the reference to trigger.id to work.

lightset: "{{ trigger_label_map['s9_lightset'] }}"

color me perplexed… I tried a bunch of different forms of the offending line. Changing quote marks and such, using different things for the key… I couldn’t get any of them to work as intended. So then I simply pasted the original line from the text I used to paste into this post, and now it’s working. I have taken a magnifying glass to the code and haven’t been able to tell what is different. ??

But, OK, it’s now working as designed. I don’t get it, but I’ll take it!

Thank you atlflyer for taking the time to help me.