Problems with adding random to Assist responses

Hi, I was wondering if anyone could help me with the following. I’m trying to create random responses to intents, where I’ve created one for errors (copied from another helpful soul on the HA forum), however it’s not giving me the response I expected.

Here’s my code:

language: "en"
responses:
  errors:
    no_intent:
      speech:
        text: >-
          {{ ["Sorry, I don't speak Pleb",
              "Sorry, I didn't understand that", 
              "I'm sorry, Dave. I'm afraid I can't do that",
             ] | random }}

So far, is just giving me the default ‘Sorry, I didn’t understand that’ response (no random).

I also tried removing the speech: and text: lines, then I receive a strange response.

image

If anyone could point me in the right direction, that would be much appreciated :slight_smile:

The only thing I can see is the last comma at the end. On the random text I have setup there is no last comma just the "] | random }}

1 Like

I agree, Python can be persnickety about syntax. I also tend to avoid apostrophe’s because that can also cause issues, but YMMV. Whenever I type out my phrases I treat it like it’s Commander Data from Star Trek, no conjunctions :joy:.

1 Like

And maybe start the template with " or ’ too:

language: "en"
responses:
  errors:
    no_intent:
      speech:
        text: >-
          '{{ ["Sorry, I don't speak Pleb", "Sorry, I didn't understand that", "I'm sorry, Dave. I'm afraid I can't do that"] | random }}'
1 Like

Thanks all for the suggestions and responses. I tried the following, but now I’m getting a different response:

language: "en"
responses:
  errors:
    no_intent:
      speech:
        text: >
          {{ [ "Sorry, I don't speak Pleb", "Sorry, I didn't understand that", "I'm sorry, Dave. I'm afraid I can't do that" ] | random }}

Here’s the response I’m getting:

Any other thoughts?

Have you tried adding the apostrophe at the start and end as I described abobe?

Adding single or double quotes to a template already demarcated by a block scalar indicator, > or |, will not fix OP’s problem and will just cause other issues.

Yes I have tried that, it results in the error below.

image

Here’s my code again for reference.

language: "en"
responses:
  errors:
#    no_intent: "Sorry, could you please repeat that"
    no_intent:
      speech:
        text: >
          '{{ ["Sorry, I don't understand", "Sorry, I didn't understand that", "I'm sorry, Carl. I'm afraid I can't do that"] | random }}'
``

OK So I’ve just had a chat with a few on discord, apparently it’s not possible to add templates to the error.yaml file, meaning it’s not possible to add random error responses… Shame :cry:

Ah, thats a shame. I was wondering why it didnt work…

1 Like

Cool idea, and there’s a hacky way to do it!

The idea is to create a “catch all” rule that matches anything, but has the lowest priority possible. To do this create a file config/custom_sentences/<lang>/default.yaml with the following contents:

# config/custom_sentences/<lang>/default.yaml
# replace <lang> with "en" or something

language: "<lang>"
intents:
  # catch all intent, in case everything else fails. Home Assistant gives lower
  # priority to sentences with wildcards, and among those lower priority to
  # sentences with fewer text chunks, so this is guaranteed to be used only as a
  # last resort.
  DefaultIntent:
    data:
      - sentences:
        - "{wild}"
lists:
  wild:
    wildcard: true

And then in your configuration.yaml handle the DefaultIntent with a random error message:

# configuration.yaml

intent_script:
  # The default "catch all" intent essentially signifies an error
  DefaultIntent:
    speech:
      text: |-
          {{ ["Sorry, I don't speak Pleb",
              "Sorry, I didn't understand that", 
              "I'm sorry, Dave. I'm afraid I can't do that",
             ] | random }}
1 Like

Nice! Thank you

Does the filename default.yaml have a special meaning?
I added the yaml to a file i named custom_sentences/de/catch_all.yaml and some intents didn’t work anymore. Renaming the file to default.yaml seems to resolve that issue.

No default.yaml has no special meaning.

Actually what I wrote in the yaml comments above is not accurate, Home Assistant will give higher priority to wildcard sentences in the next version (2023.12).

Currently, it seems that within the same file, later sentences have higher priority, so you need to put the catch-all in the beginning of the file.

Between different sentence files I’m not sure, I guess it depends on the order in which HA processes the files, which is likely filesystem specific. So it could be that your default.yaml just happens to be processed before other files, leading to lower priority, while catch_all.yaml is processed after, getting higher priority and messing up your intents.

TLDR:

  • If your senteces are in one file put the catch-all there, in the beginning
  • If you have multiple files figure out which one is loaded first and put the catch-all there
  • Or wait for 2023.12 :smiley:
1 Like

Thanks for clarification. I’ll probably wait for the next Version than.