How can I use variable in Alexa random responses?

I already read few topics related with Alexa random responses, but I’m really not good with jinja’ing, so I decided to make my own topic for certain question.

I have Alexa custom skill which is working correctly.
This is my intent_script.yaml :

SetEffect: # Intent type
    speech:
      text: If you insist, the {{ effect }} effect has been applied to {{ entity }}
    action:
      service: light.turn_on
      data:
        entity_id: "{{ entity | string }}"
        effect: "{{ effect | string }}"

Let’s say that we have utterance “Alexa, ask home lights to apply Rainbow effect to bedside lamp”
In this case:

  1. “home lights” is skill name
  2. “Rainbow” is {{ effect }} variable (slot)
  3. “bedside lamp” is {{ entity }} variable (slot)

As said before, this is working perfectly - rainbow (or some other) effect will be applied to bedside lamp (or some other light) and Alexa will give proper response with proper effect/light entity variable.

I want to put this on higher level and I looked into this. Of course, below code will not work because of {{ }} within {{ }}, and I am not good at all with jinja templating:

SetEffect: # Intent type
    speech:
      text: >
        {{[
        "If you insist, the {{ effect }} effect has been applied to {{ entity }}",
        "{{ effect }} effect has been applied to {{ entity }}, do you want me to sing you something"
        ]|random}}
    action:
      service: light.turn_on
      data:
        entity_id: "{{ entity | string }}"
        effect: "{{ effect | string }}"

Could someone help me with this, thanks?

OK, I found it on my own :slight_smile:
The key is that " ~ effect + " and/or " ~ entity + " could be used in place of variable, inside of double curly brackets {{ }}.

So my complete code now is looking like below, and it is working as expected:

intent_script.yaml

SetEffect: # Intent type
    speech:
      text: !include alexa_confirm.yaml
    action:
      service: light.turn_on
      data:
        entity_id: "{{ entity | string }}"
        effect: "{{ effect | string }}"

alexa_confirm.yaml

>
  {{ [
    "Stop asking stupid shit, I'm sick of " ~ effect + " effect",
    "Yes my master, the " ~ effect + " effect has been applied",
    "If you insist, the " ~ effect + " effect has been applied",
    "On your command, the " ~ effect + " effect is running",
    "You are so boring, I applied " ~ effect + " effect to " ~ entity + "",
    "" ~ effect + " effect is sucks and has been applied to " ~ entity + "",
    "Leave me alone, " ~ entity + " deserves better than " ~ effect + "",
    "Your wish is my command, enjoy your " ~ effect + " effect",
    "Just one more time, " ~ entity + " is running " ~ effect + " effect",
    "ju-hu, stupid " ~ effect + " effect running again",
    "" ~ effect + " effect has been applied, but don't spam me any more",
    "" ~ effect + " effect has been applied, do you want me to sing you something",
    "Yes, my most beautiful boy, " ~ entity + " is running " ~ effect + " effect"
  ] | random }}

There is a one more problem: In response, Alexa wouldn’t say the friendly name of device, i.e. Bedside Lamp, but would rather say entity ID, i.e. light.lamp_wood_0 (with all these underscores in response), which sounds stupid :slight_smile: , but I will figure out later how to fight with this.

I was wondering if you ever figured this out, and also, if you know how to include (see below) as part of the random?

<speak>
<say-as interpret-as="interjection">abracadabra!</say-as>.
</speak>
<speak>
<say-as interpret-as="interjection">totally kidding</say-as>.
</speak>

Hey, just to be sure we are on same track, you want just to randomize “abracadabra” and “totally kidding” without any variable? Should “abracadabra” and “totally kidding” be whole responses?

Sorry, I ended up figuring it out. I guess what I’d TRULY like to so, is randomize the words within the interjection phrase