Ai Action & Response Variable Issue

Hi all,

I’m relatively new to home assistant with most of my automations being built in the GUI. I’ve been trying to build an automation that does the following:

  • A button is pressed as a trigger
  • An AI response is generated using the Google Gemini integration using the AI task action using a response variable of AI_Task
  • Copy the response to a Input Text helper
  • Use the input text helper to trigger Alexa to read out the generated message

The idea is that when a button is pressed, it will generate a funny response based on my prompt and read it aloud on an Alexa device.

For some reason, I’m getting an error that says “Error rendering data template: UndefinedError: ‘ai_result’ is undefined” whenever the automation is run but I cant work out why.

With the help of Google Gemini, I’ve added a step to generate a notification which shows the AI response data and when running the automation the notification triggers and I successfully see the response. I’ve also used the actions tab in developer tools to verify the call to Gemini is working, which it is as i get the expected response back.

Would any of you kind people be able to help me understand what I’m doing wrong? YAML is as follows:

alias: AI Test
description: ""
triggers:
  - type: changed_states
    device_id: 2c76e3d4e8c28b80c6466d02c8d39515
    entity_id: 55248869f2c5e09f827bb38edd4cdcfc
    domain: light
    trigger: device
conditions: []
actions:
  - action: ai_task.generate_data
    metadata: {}
    data:
      task_name: Generate Response
      instructions: >-
        Generate a very short and funny announcement to tell people that the
        Sock and Conker Pub is now open for business. Include a funny warning
        about not getting too pissed.
      entity_id: ai_task.google_ai_task
    response_variable: ai_result
  - data:
      name: AI Test
      message: "{{ ai_result | default('AI task returned nothing') }}"
    action: logbook.log
  - data:
      title: AI Result
      message: >-
        {{ ai_result.response | default(ai_result) | default('No response from
        AI') }}
    action: persistent_notification.create
  - target:
      entity_id: input_text.pub_open
    data:
      value: >-
        {{ ai_result.response | default(ai_result) | default('No response from
        AI') }}
    action: input_text.set_value
  - action: notify.alexa_media_office
    data:
      message: >-
        {{ ai_result.response | default(ai_result) | default('No response from
        AI') }}
      data:
        type: tts
mode: single

So, this part works, but the rest of the automation doesn’t?:

If so, I think you have two problems, although I am not sure that either of them actually explains the specific error you pasted.

First, the AI Task documentation says that the response from a generate_data call is a dictionary with the keys data and conversation_id. But you’re not referencing either of those; you’re referencing a different key, response. For example, the very next thing you have is:

  - data:
      title: AI Result
      message: >-
        {{ ai_result.response | default(ai_result) | default('No response from
        AI') }}
    action: persistent_notification.create

This code isn’t going to work right because you’re referencing the key response, but that key is not expected to be an element of ai_result. You need to look at ai_result.data. (Or, since I don’t use the underlying Google entity, perhaps it should be ai_result.data.response?)

I’m going to use ai_result.data in myt suggested code for the remainder of the post instead if ai_result.response.

The other issue is that although you have a default filter, which if used correctly would prevent errors, you’re using it in a non-sensical way. It doesn’t make sense to try to access ai_result.response but then default to ai_result if there is an error with ai_result.response. What are you trying to accomplish? The most common thing to do would probably be:

{{ ai_result.data | default('No response from AI') }}

A better version, if generate_data fails sometimes for whatever reason, would be:

{{ (ai_result|default({})).get('data', 'No response from AI') }}
1 Like