Some fun with Alexa on a Friday afternoon

Spent some time experimenting with the Template support for Alexa Intents, and came up with the following to activate any scene:

In the Amazon Interaction module add this to the intent schema:

   {
      "intent": "ActivateSceneIntent",
      "slots":[
        {
        	"name" : "Scene",
          "type" : "Scenes"
        }

Then create a custom slot type called Scenes listing every scene you want to control:

The names must exactly match the scene names (minus underscores - amazon discards them anyway and we later map them back in with the template).

Add a sample utterance:

ActivateSceneIntent activate {Scene}

Then add the intent to your Alexa Section in your HA config file:

    ActivateSceneIntent:
      action:
        service: scene.turn_on
        data_template:
          entity_id: scene.{{ Scene | replace(" ", "_") }}
      speech:
        type: plaintext
        text: OK

Then say “Alexa ask homeassistant to activate downstairs on” or any other scene, and bingo! A lot easier than adding an intent for each scene.

As a bonus - tired of Alexa always saying the same boring thing back to you to confirm a command? Try this:

Replace the “OK” with:

        text: !include alexa_confirm.yaml

And create a file called alexa_confirm.yaml with something like the following in it (go on, be creative!):

          >
          {{ [
          "OK", 
          "Sure", 
          "If you insist",
          "Done",
          "No worries",
          "I can do that",
          "Leave it to me",
          "Consider it done",
          "As you wish",
          "By your command",
          "Affirmative",
          "Yes oh revered one",
          "I will",
          "As you decree, so shall it be",
          "No Problem"
          ] | random }}
```

Alexa will respond with a random phrase each time. You can use the include for as many different intents as you like! 

Have fun :)
14 Likes

That’s awesome! I know have to purchase Alexa.

2 Likes

Nice one Andrew! And timely since I was planning on finally getting the Alexa integration working now that I have a few other things off the checklist. Do you have any good resources (besides @brusc’s video for setting up Alexa) you could point me to on understanding intents? I’ve honestly been a bit intimidated by the whole process.

Dive in - it’s a lot of fun!

I used the docs to work through it, it wasn’t too bad.

One observation - the first method sounds easy but is really quite hard to set up and I found he results very patchy. The 2nd method using custom commands allows you to define exactly what happens and works really well.

Give it a shot and I’ll give you a hand if you run into any problems :wink:

Thanks brother; that’s awfully nice of you!

1 Like

This is really cool!

Scene support is such a basic thing that everyone would like to have. I think we should integrate the text of this forum post in the docs.

2 Likes

Feel free to grab it, or if you prefer I can figure out how to do a PR and add this to the docs myself - happy to help either way.

2 Likes

Try a PR and I can help out if you run into trouble. Think we should hardcode the scene intent into the Alexa component? It will be a common action

1 Like

Yep, I’ll take a look at the PR over the weekend.

Regarding integrating scenes into the Alexa component - I’m in two minds. You would want to be able to allow custom cards and responses, so would need some HA configuration, plus you would always need to do the configuration on the Amazon side anyway. The only plus point is you could hide the clunkiness of the template subbing underscores for spaces. For now, I think a documented example would be a lot easier, more instructive and just as effective.

1 Like

@baloob - I created the PR as you suggested, let me know if I did it correctly and if you want any changes.

Andrew, there are two methods listed for Alexa support in HA; I got the first method to work using Hasska. Would the setup you are describing here work with that or do I have to go the other route to have the custom Alexa responses?

Hi Rob -

No, this is for the second method, not Hasska,

Bummer. I think the second method is still a bit out of reach for me at the moment with my knowledge level of Alexa.

With the second method, would I be able to query states? For example, ask Alexa if the Front Door is locked or if my windows are open?

Definitely - I much prefer the second method because you can do practically anything with it. Hasska is a cool package and works great but for me, the Amazon approach is limited and confusing. Alexa didn’t understand what I wanted and I generally don’t want to work with individual lights and she didn’t seem to understand my scenes. (and Homekit doesn’t seem a lot better).

Here are a couple of automations I am using to query state - it really is pretty easy to setup after you figure out the first one.

Turn on night mode but remind me if the garage is open:

    NightModeIntent:
      action:
        service: script.turn_on
        data:
          entity_id: script.night
      speech:
        type: plaintext
        text: > 
          {% if is_state('garage_door.garage', 'open') %}
            No problem, but you might want to close the garage door before you go to sleep
          {% else %}
            {{ ["Good night, sleep well", 
            "Night Night", 
            "See you in the morning", 
            "OK, but no snoring!",
            "don't let the bed bugs bite" 
            ] | random }}
          {% endif %}

Ask Alexa if the heat is on:

    QueryHeatIntent:
      speech:
        type: plaintext
        text: >
          {% if is_state('input_boolean.heating', 'on') %}
            the heat is on
          {% else %}
            the heat is off
          {% endif %}
3 Likes

This is what I’m looking for. The examples you gave are so simple and easy for me to grasp; it’s just like any other automation. My concern is setting up the other side of it. I don’t have SSL setup and the link for the AWS Lambda proxy for Alexa skills is a 404. Is there another way to set up a Lambda proxy or another guide?

I went the SSL route since I already had it set up I don’t know anything about the Lambda Proxy, sorry.

The SSL setup isn’t too bad - I’d go ahead and do that first as its useful in its own right.

Having issues with the port forwarding. I’m sure you’ve never heard of Verizon FiOS, but they’re not the friendliest routers in the world. I have a cert set up with certbot, but having trouble getting the RPi to come up instead of the router.

Thanks for this. I started tying to do the same for sensors. But I’ve run into a dead end with the intent trying to distinguish sensors that have certain attributes. I suppose I could make anther intent but a catch all would be nice.

SensorIntent:
speech:
  type: plaintext
  text: >
    {%- for state in states.sensor -%}
      {%- if state.attributes.battery_level != None and state.name.lower() == Sensor.lower() -%}
        {{ state.name }} is {{ state.state }} as of {{ relative_time(state.last_updated) }} ago with a battery level of {{state.attributes.battery_level}} percent
     {%- elif state.attributes.battery_level == None and state.name.lower() == Sensor.lower() -%}
        {{ state.name }} is {{ state.state }} as of {{ relative_time(state.last_updated) }} ago
      {%- endif -%}
    {%- else -%}
      I could not find a sensor named {{ Sensor }}
    {%- endfor -%}

Any ideas?

1 Like

Did you figure out how to get this to work the way you wanted? I just got the example up and running and I’m looking to get into querying states next. Was just wondering.

Follow-up for anyone working with Alexa. I ran int an issue this morning where I was using a custom slot in my Alexa setup, but it wasn’t working. I was configuring “wake up” commands for my kids and using their name as part of the name of a script that gets triggered. My daughter’s name is Nora, but my intent was receiving “Norah”, which is not in the names slot for my skill. From Amazon:

Note: A custom slot type is not the equivalent of an enumeration. Values outside the list may still be returned if recognized by the spoken language understanding system. Although input to a custom slot type is weighted towards the values in the list, it is not constrained to just the items on the list. Your code still needs to include validation and error checking when using slot values.

So if you run into this kind of thing, you may have to do a bit of extra work in your intent handler to normalize inputs.