How to use slots in intent_script in configuration.yaml

I have defined my first intent in the configuration.yaml but I cant get to the slot values in the intent-script

conversation:
  intents:
    FetchSpotify:
      - "Transfer spotify to (my|the) {player}"
      
intent_script:
  FetchSpotify:
    action:
      - action: rest_command.call_smartflat
        data:
          intent: "fetch_spotify"
          JSONData: >
            {
              "playername": "{{ intent.slots.player }}"
            }
        response_variable: result                     # get action response
      - stop: ""
        response_variable: result                     # and return it
    speech:
      text: "{{  action_response['rest_command.call_smartflat'].reply  }}"

Whatever I tried to recevie the “player” slot I find a logentry like this

ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'intent' is undefined when rendering '{
  "playername": "{{ intent.slots.player }}"
}'

Try using {{ player }} instead of {{ intent.slots.player }}

That’s where I started… :-/
Current state is the following error log:

2025-03-10 19:38:46.826 INFO (MainThread) [homeassistant.helpers.intent] Triggering intent handler <ScriptIntentHandler - FetchSpotify>
2025-03-10 19:38:46.845 INFO (MainThread) [homeassistant.helpers.script.intent_script_fetchspotify] Intent Script FetchSpotify: Running intent_script script
2025-03-10 19:38:46.846 INFO (MainThread) [homeassistant.helpers.script.intent_script_fetchspotify] Intent Script FetchSpotify: Executing step call service
2025-03-10 19:38:46.847 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: ‘player’ is undefined when rendering ‘{
“playername”: “{{player}}”
}’
2025-03-10 19:38:47.250 INFO (MainThread) [homeassistant.helpers.script.intent_script_fetchspotify] Intent Script FetchSpotify: Stop script sequence:
2025-03-10 19:38:47.251 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: ‘dict object’ has no attribute ‘rest_command.call_smartflat’ when rendering ‘{{ action_response[‘rest_command.call_smartflat’].reply }}’

with this code in the config

rest_command:
  call_smartflat:
    url: "https://xxxxxxxxxx"
    method: "POST"
    headers:
      Content-Type: "application/json"
    payload: >
      {
        "Intent": "{{ intent }}",
        "JSONData": {{ params | tojson }}
      }

intent_script:
  FetchSpotify:
    description: Ask Smartflat to transfer spotify to requested device
    action:
      - action: rest_command.call_smartflat
        data:
          intent: "fetch_spotify"
          params: >
            {
              "playername": "{{player}}"
            }
        response_variable: result                     # get action response
      - stop: ""
        response_variable: result                     # and return it
    speech:
      text: "{{  action_response['rest_command.call_smartflat'].reply  }}"

Thx for any help, this is sooo much new to me :smiley:

Looks like you are missing some YAML lines “data” and “sentences”. Check the docs.

So I managed to get the params correct:

intent_script:
  FetchSpotify:
    description: Ask Smartflat to transfer spotify to requested device
    action:
      - action: rest_command.call_smartflat
        data:
          intent: "fetch_spotify"
          params:
            playername: "{{  player  }}"
        response_variable: result                     # get action response
      - stop: ""
        response_variable: result                     # and return it
    speech:
      text: "{{  action_response['rest_command.result'].reply  }}"

No I only fail to get the response right.

2025-03-10 21:19:59.338 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: ‘dict object’ has no attribute ‘rest_command.result’ when rendering ‘{{ action_response[‘rest_command.result’].reply }}’

same for ‘rest_command.call_smartflat.result’…
Referring to Intent Script - Home Assistant
:frowning:

I don’t have spotify, so can’t help there. However if you run that action in DevTools and can show the “Response”, then maybe I or someone else can help.

I’m not talking directly to spotify anyway, the response looks something like this:

{“state”:“OK”,“reply”:“Now playing on SMARTHOME”}

I think the way this works, is that the results of the rest command gets stored in response_variable and response_variable in turn is copied into action_response.
Something like action_response = {“state”:“OK”,“reply”:“Now playing on SMARTHOME”}
so try {{ action_response['reply'] }}

THANK YOU VERY MUCH!
action_response.content.reply did the trick then.