Skip to content

Alexa Talking to Home Assistant (The Skill) (old version)

Elmar Hinz edited this page Jan 2, 2023 · 1 revision

Prerequisites

You did follow along the previous steps.

If you want to use token based authentication, get acquainted with token creation within the panel of your Home Assistant administrator. (Note: Token based authentication does work for older routers with loopback issues.). The alternative is account linking, albeit token based authentication is more easy to set up.

Screenshot of the token management

Setting up the custom Alexa Skill.

  • This part of the guide will show you all the steps necessary to login, create and deploy an custom Alexa skill for use by your Alexa devices. The end goal is a home assistant skill.

Logging into the Alexa Skill Developer Console and create your custom skill:

  1. Navigate to https://developer.amazon.com/alexa/console/ask and login with the same credentials you use for your Alexa devices.
  2. Click "Create Skill" in the upper right hand corner.
  3. Enter the skill name and under "Choose a model to add to your skill" make sure "Custom" is selected.
  4. Ensure "Alexa-Hosted (Python)" is also selected before continuing.
  5. Click "Create Skill" again in the upper right.
  6. Choose the "Start from Scratch" template. (Previously known as "Hello World")
  7. Click "Choose" in the upper right create the skill.

Please wait while amazon provisions the resources for your skill.

Once done with the above steps you should be presented with a page similar to this one:

Skill builder dashboard

Us kiwi (New Zealander) Language should be English (AU)

Configuring the Custom Skill:

Now that you have a custom skill template setup and ready, we need to make the modifications that will allow it to connect to your home assistant installation and also respond to your request.

Note: Some issues may happen if the language in the developer console is different from the language on echo devices. A new language could be added in the developer console, then the intents could be added to the skill. Each language setup represents a clean slate for the "build" stage; the code stays the same across all languages. The HelloWorldIntent needs to be added for each language, as this maps to an intent in the code.

Changing default invocation:

  1. On the Build tab of the Alexa Developer Console page, navigate to the Invocation section.
  2. Change the invocation from the pre-filled change me to custom actions.
  3. Click Save Model at the top of the page.

Uploading the skill manifest:

The installation process has been improved in 0.7.0 and later. Now you can configure the skill intents by simply uploading a JSON manifest.

  1. Find the locale_en_us.json file in this repository. (Hint: The files are migrated into this folder one by one). You can use this even if you're not in the US region, but languages for other locales will also be added in the future. If you speak another language, please reach out if you'd like to help with localization.
  2. In the Alexa Developer Console, in the left menu bar, under Interaction Model, you should see JSON Editor click that and either drag and drop the downloaded JSON file above, or copy and paste its contents.
  3. Click Build Model near the top.

Configuring account linking:

We want the highest security possible when connecting to our Home Assistant install, so we want to use the built in skill account linking feature to ensure that our skill is not using static credentials for access.

Note: If you reach your home assistant install with the :8123 at the end of the URL, account linking will not work for you. Skip the account linking steps and follow the notes in the (Adding the Code)[#adding-the-code] section.

  1. On the left hand side near the bottom you should see "Tools". Click it, then click on "Account Linking".
  2. Enable the toggle for "Do you allow users to create an account or link to an existing account with you?"
  3. The above action will also enable the toggle for the "Allow users to enable skill without account linking (Recommended)." please disable this toggle.
  4. Under Authorization URI provide your home assistant URL followed by /auth/authorize (Example: https://myhomeassistant.com/auth/authorize
  5. Under Access Token URI provide your home assistant URL followed by /auth/token (Example: https://myhomeassistant.com/auth/token)
  6. Under Client ID provide:
  • https://pitangui.amazon.com/ if you are in US
  • https://layla.amazon.com/ if you are in EU
  • https://layla.amazon.co.uk/ if you are in the UK (You may still need to use the EU URL if things do not work)
  • https://alexa.amazon.co.jp/ if you are in JP and AU
  1. Under Client Secret type anything.
  2. Change "Your Authentication Scheme" to "Credentials in request body"
  3. Lastly, click "add scope" and type "smart_home" in the box that appears.
  4. Click save in the upper right

Adding the code:

At this point you're nearly done! We now need to copy four sections of code from this repo: requirements.txt, lambda_function.py, language_strings.json, and prompts.py

In the Alexa Developer Console:

  1. Along the top of the page, click the tab labeled "Code"
  2. At the left side of the page you should see lambda_function.py, requirements.txt and utils.py. We need to open: lambda_function.py and requirements.txt
  3. Copy the code of lambda_function.py in this repo replacing the code of the example lambda_function.py file in the Alexa Developer Console
  4. Once copied, at the top of the replaced lambda_function.py, we need to update and replace the HOME_ASSISTANT_URL with your Home Assistant URL. Please note that: Private/internal URLs will not work and make sure there are no trailing / after your URL.
  1. Additionally, at this point if you need to ignore SSL verification please set VERIFY_SSL to False.
  2. Next, we will need to copy the text from requirements.txt in this repo, replacing the text of requirements.txt in the Alexa Developer Console.
  3. Next, we need to create a file called language_strings.json. Copy the code from language_strings.json and pasted it into this file.
  4. Next, we need to create a file called prompts.py. Copy the code from prompts.py and pasted it into this file.
  5. Finally, click Deploy and wait for Deployment to finish.

Note: Account linking will not work if your home assistant installation is only reachable by port 8123 (e.g - https://myhainstall.com:8123) If you have home assistant configured this way, please use long lived tokens instead of account linking. Place your long lived token generated in your home assistant install in the double quotes TOKEN = "".

All Done!

You've completed all the steps to create your custom skill, you can now navigate to the Alexa App and activate your skill by finding it under Skills. (Go to "your skills" and scroll to the right to click "dev")

To enable the skill it will ask you to link it with your home assistant installation. Do so by following the prompts.

Before testing head to homeassistant and, in your configuration, setup the following input_text:

input_text:
  alexa_actionable_notification:
    name: Alexa Actionable Notification Holder
    max: 255
    initial: '{"text": "This is a test of the alexa actions custom skill. Did it work?", "event": "actionable.skill.test"}'

reload configuration or, better, restart ha.

Then just say "Alexa, open custom actions" If all went according to plan, you should hear Alexa say "This is a test of the alexa actions custom skill. Did it work?"


Head to the next step: Home Assistant Talking to Alexa (The Script)