How to nest a field into a variable

I’m sure this is possible and described somewhere but I couldn’t find it. I’m trying to use a field as the input to a script to select a key in a dict variable. What’s the correct way to do this? Below the script I am writing, I’ve commented out the ‘fields’ part, but basically I want to pass the name of command to send to the script. In the example I hardcoded the command into “codedata”, that works. I had an example of a blueprint from which I thought that using codedata: '{{codes[command]}} should have worked with the field enabled but it did not. I tried multiple combinations of brackets and dots and quotation marks but nothing worked. What’s the characters I need to use here?

Thanks!

# fields:
#  command:
#    description: the command to send
#    example: hk_amp_on
variables:
  codes:
    hk_amp_on: >-
      BVUjhBEzAuATAQOmBjMC4AcBQBPAA+ATAcAj4A8HwAEJ1a1VI7UIMwL//+ArBwB44A43CXgC//9VI7UIMwI=
    hk_amp_off: >-
      BUMjgBE0AuATAQOiBjQC4AcBQBPAA0ABwAvgAwfAAUAT4AsBQBdAA0ABCbOtQyPJCDQC///gAgcCCDQC
    hk_amp_cd: >-
      BVAjdxEwAuATAQOpBjAC4AcBQBPAA+ADAUAT4AMBQA/gAwNAAeAHD0ABCaytUCPYCDAC///gGgcCCDAC
    hk_amp_aux: >-
      BU0jaRE0AuATAQOZBjQC4AcBQBPAA0ABQAtAAcAHwAFAD0ADQAHgAwdAC0ADwAEJwK1NI8kINAL//+ACBwIINAI=
    hk_amp_volume_up: >-
      BUQjnhE0AuATAQOGBjQC4AcBQBPAA0ABwAtABwXDAo8BNAKAAUAPQAPAAQPDAjQCQA/AA8ABD9utRCPyCDQC//9EI/IINAI=
    hk_amp_volume_down: >-
      BVUjcxEyAuATAQOiBjIC4AcBQBPAA+AHAUAXwAFAC+AHA0ABwBPAAQ+lrVUj0wgyAv//VSPTCDIC
  device_ieee: 18:0D:F9:FF:FE:D4:87:4D
  codedata: "{{codes.hk_amp_off}}"
sequence:
  - action: zha.issue_zigbee_cluster_command
    metadata: {}
    data:
      cluster_type: in
      ieee: "{{device_ieee}}"
      endpoint_id: 1
      cluster_id: 57348
      command: 2
      command_type: server
      params:
        data: "{{codedata}}"
        code: "{{codedata}}"
alias: Amplifier IR control
description: "script to send commands from MOES UFO controller "

I usually use get() because it lets you set a default, but using bracket notation should have worked too.

codedata: "{{codes.get(command, 'hk_amp_on') }}"

Just FYI, while I think it’s best practice… you don’t actually have to declare the fields, that’s just for the UI editor. As long as they’re in the action, they will work even if not declared in the script config.

Thanks! That worked. I also found out in the process that I was calling the script wrong. I was using the UI editor “run script” command and didn’t realize I had to explicitly type the command: xxxx in the box instead of only xxx . Most of the coding I’ve done is in a language (Matlab) where the order of the arguments determines which values are passed to a script, I guess that was still in my head somewhere.