Intent Script Help - Using Response Variable from Action in Speech

I’m trying to create my first intent-to-reponse-with-action flow but am stuck at passing the actions response_variable to the speach: section. The only documentation example is horrible, but I’m familiar enough with scripts to understand its intent (pun intended… ok I’ll stop). I’ve tried quite a few different things, including using the example as writen (in case I completely failed to understand it), and no matter what I try I get an unexpected error where the response_variable is undefined. The only thing I can think of is that the response_variable is out of scope for the speach section, in which case the documentation example shouldn’t even exist.

I’m hoping that someone could point out what I might be doing wrong and/or point me in the right direction.

This is the intent in custom_sentances/en/:

language: "en"
intents:
  WasteItems:
    data:
      - sentences:
          - "What(s| is) [our] [(garbage|waste)] collection [(this|next) week]"
          - "What[(s| is)] go[(ing|es)] [out] to [the] (curb|street) [(this|next) week]"
          - "What [do] [we] (put|get) [out] to [the] curb [(this|next) week]"
lists: {}

And this is the intent_script :

WasteItems:
  action:
    action: "calendar.get_events"
    target:
      entity_id: calendar.waste_collect_calendar # This needs to be dynamic somehow
    data:
      duration:
        hours: 144
        minutes: 0
        seconds: 0
    response_variable: collection
  speech:
    text: >-
      "The waste items to be picked up
      {% if collection['calendar.waste_collect_calendar'].events[0].start
          |as_timestamp|timestamp_custom('%A %B %-d') == now()|as_timestamp|timestamp_custom('%A %B %-d') %}
        {{ "today" }}
      {% else %}
        on {{ collection['calendar.waste_collect_calendar'].events[0].start
            |as_timestamp|timestamp_custom('%A %B %-d') }}
      {% endif %}
      are: {{ collection['calendar.waste_collect_calendar'].events[0].summary }}

The relevant log info from the 2 errors are…
homeassistant.exceptions.TemplateError: UndefinedError: 'collection' is undefined
and
homeassistant.helpers.intent.IntentUnexpectedError: Error handling WasteItems

The first thing to do is to get rid of the first " in the text response of the intent script.

Then, IIRC, you need to include a stop action to return the response from the calendar query:

WasteItems:
  action:
    - action: "calendar.get_events"
      target:
        entity_id: 
          - calendar.example
          - calendar.waste_collect_calendar
      data:
        duration:
          hours: 144
          minutes: 0
          seconds: 0
      response_variable: collection
    - stop: "Return value to intent script"
      response_variable: collection
  speech:
    text: >-
      The waste items to be picked up
      {% if (action_response['calendar.example'].events[0].start | as_datetime).date() == now().date() %}
        today
      {% else %}
        on {{ action_response['calendar.waste_collect_calendar'].events[0].start|as_timestamp|timestamp_custom('%A %B %-d') }}
      {% endif %}
        are: {{ action_response['calendar.waste_collect_calendar'].events[0].summary }}

I agree that the documentation for speech is woefully underdeveloped and scattered all over the place. While I have been able to put together a few custom intents/scripts that work fine, I’ve also had quite a few that I could never get to work even though everything seemed to be correct. All but one of those I was able to get working using Sentence triggers in automations instead of intents/scripts. You might find you have better luck trying it that way.

Thanks for your response, would you be so kind as to remove my home address from your response? I corrected my OP since I apparently doxed myself unintentionally (missed 2 entries :roll_eyes: ).

Removing the surrounding "s and adding in the stop: yields the same results unfortunately.

1 Like

I’ve updated the intent script above… no matter what you use in the action, the variable name needs to be action_response in the speech block.

It seems to be working (slightly modified for the calendars on my instance):

image

1 Like

Son of a… it works… the documentation example is correct. A documented simple explanation of what’s going on and why would go a long way in helping an intermediate like me.

Anyways thank you very much for helping me out and getting me on a good path, it’s very much appreciated.

@Didgeridrew thank you so much, I’ve struggled a couple of hours with this.
To me the double use of action_response is also quite unintuitive…

If someone is also trying to read todo list entries, here’s my script:

ReadListEntries:
  action:
    - action: todo.get_items
      target:
        entity_id: "{{ list }}"
      data:
        status:
          - needs_action
      response_variable: liste
    - stop: "Return value to intent script"
      response_variable: liste
  speech:
    text: >-
      {%- for item in action_response[list]['items'] %}
      - {{ item.summary + '.' ~ "\n" }}
      {%- endfor %}
      Das war alles.
1 Like