Amazon Alexa adds new "Smart Home Skill API"

JonW

Senior Member
In my recent quest to integrate Alexa with my HAI OmniPro system, I had looked at both direct Amazon integration and at the Samsung SmartThings device.  Because of the complexity involved with the Amazon setup, I had decided I would take the easier route and write an interface to SmartThings.  SmartThings has an open API, allows you to create "virtual" devices and has built in support for Alexa without using the "trigger" word kludge.
 
Fast forward two weeks from when I made that decision and now Amazon has released (4/5/2016) an easier framework for integrating Smart Home devices/systems to the Alexa service.  Since I'm still waiting for the SmartThings device to arrive, I started playing with the new Alexa Smart Home API.  You need to create an Amazon developer account to do it, but individual usage should be low enough that there shouldn't be any monthly billing.  The one requirement of using the Amazon interface is that you need a web based/cloud service that Amazon can talk to.  That part I already have with a program I built a few years back that I call HAI Data Service.  It currently logs all HAI activity and lets me control items via web links.  I'll need to extend it and tweak it to work with Amazon, but that shouldn't be too difficult.  Right now, my first learning hurdle is implementing oAuth 2.0 authentication with Amazon, but once that's ironed out, the rest should be pretty easy.
 
I see this new offering from Amazon as really opening up the options for people to integrate with Alexa.  Right now they only support lighting and thermostats with the API, but it's not hard to map macros or scenes into the lighting on/off capability.
 
Blog post from Amazon:  
https://developer.amazon.com/public/community/post/Tx1KIRDSNFDHEA4/Amazon-Enables-Developers-to-Extend-Alexa-s-Smart-Home-Capabilities-a-New-Additi
 
Developer Home Page:  
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/content/smart-home
 
 
 
 
This Smart Home Skill API is really intriguing.  I can imagine the potential of combining this API with with the Iot AWS Rules engine.  The Smart Home Skill would provide the "control" where the Rules engine would provide the "automation".
 
But anyway, I plan to get my free T shirt by making the April 30th deadline.
 
 
 
I'm definitely impressed with the Amazon Home Automation Skills kit. But, unfortunately, in my situation, I just recently upgraded to all UPB switches from X10. We all know that Amazon won't provide native support for UPB and I will not pay $50+ a pop to upgrade to switches that are supported by Amazon such as Wemo switches.
 
Yes, there is a solution. The solution is to use the Device Gateway found in the AWS Iot service as a medium between the Home Automation skills kit and target device such as a UPB switch. Thus, you establish a connection from the the HA skill kit and your endpoint, in my case the endpoint would be a RP2 which interfaces with the UPB devices via the powerline.
 
As an added bonus, the Rules Engine within the AWS Iot service can be used to implement customized UPB scenes such as a theatre lighting scene or a mood setting scene. Also, since the AWS Iot or more specifically the Rules Engine can work independently of Alexa, we can now use local triggers such as PIR motion detectors to turn on the appropriate exterior security UPB controlled lighting.
 
Implemention of Alexa UPB control and Rules Engine UPB automation will be a very high priority for me since my HA installation is 100% UPB. It'll be posted to my github UPB repo as soon as it is finished.
 
Or, just use an automation system that supports the Echo, and which lets you define arbitrary commands and react to them in arbitrary ways, instead of being limited to what Amazon defines.
 
Has anybody attempted to configure a Smart Home skill?  If so, how did you complete the Account Linking section?  I was hoping to use Login With Amazon (LWA) to complete this section. I've searched the Smart Home Skills forum and found one post indicating that LWA cannot be used for Account Linking in the Smart Home Skills although it could be easily used in the old Alexa Lighting API. A suggestion was made to implement an Oauth2 server  to be used for account linking which is a very daunting task especially if the skill is only for personal use. 
 
I'm open to any suggestions  to avoid implementing a back end Oauth2 server. 
 
BTW, the Smart Home skills API is a very open API.  You can control just about any lighting device and/or thermostat with it.  It can control X10, RA2, Zwave, Zigbee, UPB lighting devices not to mention an assortment of thermostats.
 
 
 
Amazon's documentation is lacking and their examples don't work.  I finally got the Python demo fixed this morning to return sample virtual devices.  The next step is to fix their node.JS demo to make it work also.  After that, I'll decide which language to stick with and start making real calls to my own back-end server to replace the sample data with my own config.
 
I was able to implement oauth on Amazon's site without needing to setup my own server.  It took a lot of searching to figure it out!  I had e-mailed them about it last week and just this morning they replied back that it is no longer supported and they pointed me to the "Steps to Create a Smart Home Skill" documentation (which says nothing about how to implement oauth).
 
Truth is though, they do still support it, but it's just not documented well.  I'll document my setup for you when I get home later today.
 
Bob - I've posted a set of screenshots that go through my Alexa Skills config, Lambda function config and security profile config.
 
http://cocoontech.com/forums/gallery/album/150-alexa-smart-home-skills-setup/?sort_order=asc&view_style=large
 
Run through the normal Amazon steps to creating this at: 
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/steps-to-create-a-smart-home-skill
 
Then edit in the URL's for linking from my config and use the Client ID and Client Secret from your security profile.
 
wkearney99 said:
Interesting.  I'm doing something similar with my UPB project.  I'm using PubNub as my device gateway and RPIs as my IOTs.    A remote control unit such as a web browser, cell phone etc. would publish a control message in JSON format  which is sent to PubNub (device gateway) and then routed by PubNub to the local RPI which in turn interprets the JSON message and performs the appropriate UPB command.
 
I eventually plan to use Amazon's Rules Engine to add automation to the mix.
 
JonW, 
I have an Omnipro II and my echo is on the way. I want to try your Omnipro skill. Can I just install it like an iphone app or do I have to sign up for an amazon developer kit?
 
Desertdog said:
JonW, 
I have an Omnipro II and my echo is on the way. I want to try your Omnipro skill. Can I just install it like an iphone app or do I have to sign up for an amazon developer kit?
 
You need an Amazon developer account and an Amazon AWS account.  I can provide the AWS Lambda function I wrote that the skill links to, but the hard work is in a Windows server service that I use.  That is a custom app that I use for my house that isn't really setup for me to hand out the code at this point.
 
For anyone doing your own development using the new Alexa Smart Home Skill, I found that as I was playing around with creating an AWS function to talk to my home automation system (HAI OmniPro II), I found that I really only wanted the AWS function to send my web service the Alexa JSON request and to accept the return JSON package. I didn't want any parsing/processing going on within AWS since I needed to code a custom JSON processor on my back end anyways.  I already had a web service application that maintains a state machine of the HA system as well as a continuous login (TCP session) to the system. Because of this, it made more sense for me to put all the logic into my web service.
 
Below is a copy of the AWS function that I am now using to handle all request/responses (Control, Discovery, HealthCheck, etc.). It doesn't matter what the request is from your Skill, it is forwarded to the defined host and the response is returned back to the function. This also gives you a single place for debugging (your own host) without worrying about the code in the AWS function.  
 
If you need to reformat the code below, copy and paste it into a site such as http://www.cleancss.com/python-beautify/ )
 
 

"""
--------------------  REFERENCE MATERIALS  --------------------  
 
Smart Home Skill API Reference:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/smart-home-skill-api-reference
 
---------------------------------------------------------------  
"""
import json
import urllib
import urllib2
import logging
 
logger = logging.getLogger()
logger.setLevel(logging.INFO)
 
# ============================================================================
#   Default Handler for all function requests
# ============================================================================
def lambda_handler(event, context):
 
    # Set the request URL to host:port.
    url = 'http://www.YOUR-HOST.com:9876/'
 
    # Encode the request event data.
    data = urllib.urlencode(event)
 
    # Perform the request with the event data.
    req = urllib2.Request(url, data)
 
    # Create the response object.
    response = urllib2.urlopen(req)
    
    # Get the complete response page.
    the_page = str(response.read())
    
    # Load raw page data into JSON object.
    parsed_json = json.loads(the_page)
 
    # Log the event request, page response and json object.
    logger.info('**  Event Request  ** = {}'.format(event)) 
    logger.info('**  Page Response  ** = {}'.format(the_page)) 
    logger.info('**  Parsed JSON  ** = {}'.format(parsed_json)) 
 
    # Return value as a JSON object.
    return parsed_json
    
 
Just a bit of an update here.   I have all my thermostats, lights & buttons (macros) within the HAI OmniPro II working with Alexa now.  At present, Alexa only supports thermostats and light type devices.  The lighting supports on/off and dimming via percentage commands.  Since I have a few macros that perform distinct On or Off functions, I've created virtual switches in the OPII that allows mapping two buttons/macros to a single device.  For example, my espresso machine is controlled by an output on the OPII that turns it on and off.  To turn the espresso machine off, I need a 1 second close and release on the output and to turn it on, I need a 3 second close and release.  I have two different buttons setup to do this, but now with a virtual device, I have two lines of code in the OPII automation that say "when Espresso machine on then run button X" and when off, run button Y.  This lets me simply say to Alexa: "Alexa, turn the espresso machine on" (or off).  You can still run a button by saying "Alexa, turn buttonname on", but for those with two distinct buttons, it makes more sense to map them to an on/off device.
 
While not OPII controlled, I've also added control of my Denon amp to my app with power, volume and source selection control.  Each source is a virtual switch, so I can say "Alexa, turn the DVD Player on" and it will turn on the amp and select the DVD source input.  I have two virtual devices for the amp called "stereo" and "inside amplifier" that you can say "Alexa, turn the stereo on" or using the dimming percentage commands, I can say "Alexa, set the stereo to 60 percent" and it will set the volume to 60db.  The added benefit of doing it this way is that anywhere I can control OPII devices, I can now turn these virtual devices on/off. 
 
I'm going to try and work up a version of the app that is not specific to my installation so that others can use it.  I'm tied up on a couple projects for a few weeks, so hopefully some time later in May I can get this out.  You'll see in the screenshot below that I've created a device list with the ability to enter Alexa "friendlyname's".  The friendlyname is what you speak to Alexa.  Unfortunately, the OPII only holds 15 character descriptions, so that by itself is not enough to define your spoken commands.  I've also set it up so that you can enable or disable specific devices within your OPII setup so that when Alexa runs a discovery, it will only setup the devices you want.  I currently have the app returning 115 devices to Alexa and it takes only a couple of seconds for the sync.
 

HAI_Data_Service.PNG
 
Back
Top