Introduction: Custom AlexaPi Skill

In this Instructables we will be looking at how to create a custom skill to operate the GPIO on the raspberry pi using the homeassistant software.

Step 1: Setup Homeassistant .yaml File

You need to go to your HTTP section of your file and uncommon to the password section and create a password for your homeassistant instance.

Then you will need to add the alexa section to your homeassistant configuration yaml file. Check my other Instructables for configuring homeassistant for more information.

Once in the file you need to drop to the bottom of the file and add the following:

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

Now you will need to setup your Scene so that you can turn things on and off. Thus, right after this text entry you will add the following (this is my example and I am naming things "test" you can pick your own name)

Scene:
- name: Test_light_on
Entities:
Switch.TestLED1: on
- name: Test_light_off
Entities:
Switch.TestLed1: off

Now you will need to add the GPIO section so you can choose what GPIO pin to assign. To add you will place the following (this is still my example you can change the GPIO number anytime)

Switch:
- platform: rpi_gpio
Ports:
11: TestLED1

Once you have added these lines save the .yaml file and restart homeassistant.

Step 2: Create Skill

You will first need to use your developer account in Amazon.

After login you will need to go to developer.amazon.com choose alexa and then choose alexa skills kit

You will now add new skill

Now fill out what you want to name your skill and the invocation name. The invocation name will be the name you will say when you want to activate your skill.

Also take note of the application id since you will need this later to set up your lambda function.

Now move to interaction model and enter the following:

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

Now you will add a slot.

Once you have added the slot you will need to enter the name as "Scene" and enter the name that you specified in your .yaml file in homeassistant but without the underscore character "_" since alexa will not recognize them properly when the name is spoken.

Example:

Test light on
Test light off

Once this is added you need to add the utterance. This is how you will speak the command to Alexa. I have chosen the following:

ActivateSceneIntent activate {Scene}

This will allow you to utter the command "activate" and then after that you can say the name of a slot from above like "Test light on" to turn it on.

Refer to this video for a live demonstration:

Step 3: Router Setup

First you will need to forward your router. To do this you will first need to see if you have a static IP or not. For the majority of us all we get our ip address from our ISP from DHCP. Thus, this address could chang if you become disconnected from your ISP for some reason. Thus you need to employ DDNS. There are many services our there that will do this one of them is duckdns . I have an asus router so asus has their own service. Under your WAN settings you will find the DDNS tab or something similar. Once you have your web address from whatever ddns site you use you will enter that into your router and choose what ddns service you are using so that your router will keep that ddns server updated with your ISP IP address just in case it changes for whatever reason.

Once this is set up you will need to port forward your homeassistant port 8123 to your raspberry pi.

To do this you will need to get into your router's port forwarding settings and forward both tcp/udp port 8123 to the ip address of your raspberry pi. To obtain the ip address from your raspberry pi just login to a terminal from your raspberry pi and type ifconfig.

Check out www.portforward.com to see how to forward ports on your brand router.

Once your router is set up you can begin building your lambda function.

Step 4: Lambda Function Setup

For the HTTPS setup I used a lambda function from amazon aws. Thus, go to console.aws.amazon.com and create an account.

Once the aws account is created you will go to all services then choose lambda under computer section

The choose to create a new lambda function.

You will add the Code found at the following web address to the code section of the lambda function:

https://community.home-assistant.io/t/aws-lambda-proxy-custom-alexa-skill-when-you-dont-have-https/5230

Once that is added you will need to change the following sections:

'amzn1.ask.skill.xxxx-xxxx-xxxx-xxxx-xxxxxx' Will need to be changed to your amazon developer skill's Application id that we talked about in Step 2

http://xxxxxxxx:8123/api/alexa?api_password=pas Will need to be updated to your ddns web address and your password you created for homeassistant in setup 1

Under the configuration tab you will need to make sure the runtime is node.js handler is index.handler and role will be an existing role. Under existing role I just named mine lmabda_basic_execution.

For the description i just labeled it automated home

Change the memory to 128 MB and the timeout to 0min 10sec

Under the triggers it will need to be the alexa skills kit.

Here is a video of how to set all this up for your reference:

Step 5: Final Testing

You will finally need to test this all out.

Check out this video on how to test out your new custom skill.

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017

Sensors Contest 2017

Participated in the
Sensors Contest 2017