Following on from my last post this time I’m looking at how to implement Alexa Home Skills for use with Node-RED.
Home Skills provide ON/OFF, Temperature, Percentage control for devices which should map to most home automation tasks.
To implement a Home Skill there are several parts that need to created.
For Home Skills the request is passed in a JSON object and can be one of three types of message:
These messages are triggered when you say “Alexa, discover devices”. The reply this message is when the skill has the chance to tell the Echo what devices are available to control and what sort of actions they support. Each device section includes it’s name and a description to be shown in the Alexa phone/tablet application.
The full list of supported actions:
- GetTemperatureReading 1
- GetTargetTemperature 1
- GetLockState 1
- SetLockState 1
1 These actions are listed as only available in the US at the moment.
These are the actual control messages, triggered by something like “Alexa, set the bedroom lights to 20%”. It contains one of the actions listed earlier and the on/off or value of the change.
This is the Echo system checking that the skill is all healthy.
In order for the skill to know who’s echo is connecting we have to arrange a way to link an Echo to an account in the Skill. To do this we have to implement a oAuth 2.0 system. There is a nice tutorial on using passport to provide oAuth 2.0 services here, I used this to add the required HTTP endpoints needed.
Since there is a need to set up oAuth and to create accounts in order to authorise the oAuth requests this means that is makes sense to only do this once and to run it as a shared service for everybody (just got to work out where to host it and how to pay for it).
A Link to the Device
For this the device is actually Node-RED which is probably going to be running on people’s home network. This means something that can connect out to the Skill is probably best to allow or traversing NAT routers. This sounds like a good usecase for MQTT (come on, you knew it was coming). Rather than just use the built in MQTT nodes we have a custom set of nodes that make use of some of the earlier sections.
Firstly a config node that uses the same authentication details as account linking system to create oAuth token to be used to publish device details to the database and to authenticate with the MQTT broker.
Secondly a input node that pairs with the config node. In the input node settings a device is defined and the actions it can handle are listed.
Hooking it all together
At this point the end to end flow looks something like this:
Alexa -> Lambda -> HTTP to Web app -> MQTT to broker -> MQTT to Node-RED
At this point I’ve left the HTTP app in the middle, but I’m looking at adding direct database access to the Lambda function so it can publish control messages directly via MQTT.
Enough talk, how do I get hold of it!
I’m beta testing it with a small group at the moment, but I’ll be opening it up to everybody in a few days. In the mean time the code is all on github, the web side of all of this can be found here, the Lambda function is here and the Node-RED node is here, I’ll put it on npm as soon as the skill is public.
The skill should now be generally available in the UK,US and Germany