Use data sent to a script in a dict

Hi, I am trying to send a name for a room as data to a script. That works fine. I would then like to get the correct knx group address (based on the room name) from a dict in that script. (In an atempt not to spread knx group address all over the dashboard). Here I get lost… I thought the address line in the knx send message should look like this where knx_lookup is my dict (room_name1: 1/1/2, room_name2: 1/2/2…):

address: "{{ knx_lookup["{{ room_name }}"] }}"

Obviously my lack of programming skills are apparent here. Any help is greatly appreciated. Thanks!

Don’t nest templates, just use the variable:

address: "{{ knx_lookup[room_name] }}"

Thanks, sadly that generates the following error:
‘’ is not a valid KNX group address: Invalid format @ data[‘address’][0]
I read this as it returns an empty string

Post the whole script. We can only work off the information you give us…

Thanks for helping out!

This is the Action data used in the button I have for launching the script

room_name: Office

This is the script:

sequence:
  - variables:
      knx_scenes: >-
        'Alexandra_bedroom':'1/5/0',   'Alexandra_livingroom':'1/5/1', 
        'Alexandra_bathroom':'1/5/2', 'Alexandra':'1/5/3', 
        'Guest_bath':'1/5/4',   'Office':'1/5/5',
        'Cornelia_livingroom':'1/5/6',   'Cornelia_bathroom':'1/5/7',  
        'Cornelia_bedroom':'1/5/8', 'Cornelia':'1/5/9'
  - action: knx.send
    data:
      response: false
      payload: 2
      type: "5.010"
      address: "{{ knx_scenes[room_name] }}"
alias: Enable knx scene dictionary

I get the following error:

Failed to perform the action script/enable_knx_scene_dictionary. '' is not a valid KNX group address: Invalid format @ data['address'][0]

It works fine when I send the address directly to the script but the dict seems to mess it all up for me.
Thanks!

sequence:
  - variables:
      knx_scenes:
        Alexandra_bedroom: '1/5/0'
        Alexandra_livingroom: '1/5/1' 
        Alexandra_bathroom: '1/5/2'
        Alexandra: '1/5/3' 
        Guest_bath: '1/5/4'
        Office: '1/5/5'
        Cornelia_livingroom: '1/5/6'
        Cornelia_bathroom: '1/5/7'  
        Cornelia_bedroom: '1/5/8'
        Cornelia: '1/5/9'
  - action: knx.send
    data:
      response: false
      payload: 2
      type: "5.010"
      address: "{{ knx_scenes[room_name] }}"
alias: Enable knx scene dictionary

This is dict defined in YAML.

  - variables:
      knx_scenes:
        Alexandra_bedroom: '1/5/0'
        Alexandra_livingroom: '1/5/1' 
        Alexandra_bathroom: '1/5/2'
        Alexandra: '1/5/3' 
        Guest_bath: '1/5/4'
        Office: '1/5/5'
        Cornelia_livingroom: '1/5/6'
        Cornelia_bathroom: '1/5/7'  
        Cornelia_bedroom: '1/5/8'
        Cornelia: '1/5/9'

This is dict defined in Jinja2.

  - variables:
      knx_scenes: >-
        {'Alexandra_bedroom':'1/5/0',   'Alexandra_livingroom':'1/5/1', 
        'Alexandra_bathroom':'1/5/2', 'Alexandra':'1/5/3', 
        'Guest_bath':'1/5/4',   'Office':'1/5/5',
        'Cornelia_livingroom':'1/5/6',   'Cornelia_bathroom':'1/5/7',  
        'Cornelia_bedroom':'1/5/8', 'Cornelia':'1/5/9'}

Your version was almost a Jinja2 dict but lacked the outer braces.

Thank you so much. I guess this is what happens when you don’t have the right knowledge and try to let AI code for you… Thanks again

1 Like

For what it’s worth, LLM bullshit engines are pretty lousy at anything but the absolute basics of Home Assistant configuration. They produce a lot of plausible-looking, non-functional slop. Keep in mind that HA updates frequently and that the majority of available training data that its answer will be based on are either old and/or from people posting non-working configs here or on Reddit.

Deciphering and correcting LLM slop often requires a higher level of knowledge and more time than it takes to search the docs and the forums.