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.
If anyone could point me in the right direction, that would be much appreciated
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 .
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.
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
The idea is to create a “catch all” rule that matches anything, but has the lowest priority possible. To do this create a fileconfig/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 }}
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.
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