Pass variables across scripts

Hello @all,

I try to create a reusable script thats prompts Gemini as an AI Task. The resonse variable e.g. response stores the response as text in the data key of a dictionary:

response:
  conversation_id: 01KDDTTKW693RMT343PVDFJ87D
  data: Hallo Acema! Es ist schön, dich zu sehen. Wie war dein Tag heute?

This response variable should be passed to another script that forwards this text to an Alexa or Voice Assistant device.

The calling script defines that the prompt script (see above) returns a value:

sequence:
  - action: script.1766399657012
    data:
      prompt: Sag Hallo Acema
    response_variable: response
  - action: script.ki_sagen
    data:
      device: media_player.home_assistant_voice_0585da_media_player
      message: "{{ response.data }}"
alias: KI Test
description: ""

Unfortunately the object(?) isn’t copied into the respnse variable response, see traces:

response: {}

Any advice how to proceed? Do I have a major missunderstanding of fields and response variables?

Please post your entire script and use the codeblock functionality instead of block quotes so you preserve spaces and formatting.

1 Like

KI Prompt:

sequence:
  - action: ai_task.generate_data
    metadata: {}
    data:
      task_name: Prompt
      instructions: >-
        Du bist ein Hausdiener und sprichst auf deutsch mit einem Kind von 12
        Jahren. Es ist {{ now().strftime('%A %H:%M %d.%m.%Y') }}. Die Antworten
        werden gesprochen.

        {{ prompt }}
      entity_id: ai_task.google_ai_task
    response_variable: response
alias: KI prompt
description: Gemini antwortet auf einen Prompt
fields:
  prompt:
    selector:
      text:
        multiline: true
    name: Prompt
    required: true

KI sagen:

sequence:
  - action: tts.cloud_say
    metadata: {}
    data:
      cache: false
      entity_id: "{{ device }}"
      message: "{{ message|replace('\\\\','')|replace('**','') }}"
alias: KI sagen
description: ""
fields:
  message:
    selector:
      text:
        multiline: true
  device:
    selector:
      text:
        multiple: false
    required: true
    default: media_player.echo_g

And finally the test script for integration tests:

sequence:
  - action: script.1766399657012
    data:
      prompt: Sag Hallo Acema
    response_variable: response
  - action: script.ki_sagen_echo_g
    data:
      device: media_player.home_assistant_voice_098e4d_media_player
      message: "{{ response.data }}"
alias: KI Test
description: ""

In this:

sequence:
  - action: ai_task.generate_data
    metadata: {}
    data:
      task_name: Prompt
      instructions: >-
        Du bist ein Hausdiener und sprichst auf deutsch mit einem Kind von 12
        Jahren. Es ist {{ now().strftime('%A %H:%M %d.%m.%Y') }}. Die Antworten
        werden gesprochen.

        {{ prompt }}
      entity_id: ai_task.google_ai_task
    response_variable: response
alias: KI prompt
description: Gemini antwortet auf einen Prompt
fields:
  prompt:
    selector:
      text:
        multiline: true
    name: Prompt
    required: true

That gets the var from ai_task and it’s sitting in response - BUUUT…

You never pass the var back out of your script - To do that, you have to do this:

  - stop: Return Response
    response_variable: response

So your script becomes something like this:

sequence:
  - action: ai_task.generate_data
    metadata: {}
    data:
      task_name: Prompt
      instructions: >-
        Du bist ein Hausdiener und sprichst auf deutsch mit einem Kind von 12
        Jahren. Es ist {{ now().strftime('%A %H:%M %d.%m.%Y') }}. Die Antworten
        werden gesprochen.

        {{ prompt }}
      entity_id: ai_task.google_ai_task
    response_variable: response
  - stop: Return Response
    response_variable: response
alias: KI prompt
description: Gemini antwortet auf einen Prompt
fields:
  prompt:
    selector:
      text:
        multiline: true
    name: Prompt
    required: true

That lets the next script have response so it can grab .data. dont do that and you get null. :slight_smile:

:wink: good luck.

1 Like

Great support! Just added your snippet and it works :slight_smile:
(Never read about the stop action)