Tuesday, June 30, 2015

Controlling the Honeywell WiFi Thermostat using Amazon Echo

In my previous article I reviewed the Amazon Echo. In further integrating it with my home automation setup, I want the Echo to control my Honeywell RTH9580WF WiFi thermostat through voice commands. Honeywell does not have any plans to allow its My Total Connect Comfort system to integrate with the Amazon Echo, and I have little hope that they will. But by creating our own Amazon Echo app, and using my previously discussed python script to control the thermostat, we can make it happen. Let's get started!



Preparing your webserver


While you are capable of using Amazon's AWS Lambda free tier for this, I will be covering how to do it with your own server. You need to have a web server that is accessible online and is configured to use HTTPS SSL encrypted connections. Amazon enforces HTTPS for the Echo. But you can create your own self-signed certificate and supply it to Amazon as a temporary development solution. For a cheap SSL certificate, I use Comodo Positive SSL.

Python script for My Total Comfort


Make sure you have configured my python script, as mentioned on my previously article, and that it is working correctly. Also verify that it is executable by the same user that runs your web server (http,  www-data, wwwuser, etc...).

PHP file to interface with the Amazon Echo


You will need my PHP file that the Amazon services will send the request from your Echo to. The one I created is a modified version thanks to OurAce.com. Their version is available here. I am not very experienced with web-based programming, so I am glad they provided that 'Hello World' example that I can hack.

Link to my PHP file


Place the PHP file in a directory that will be accessible by Amazon through the web. Make sure to edit the variable $cmd to correctly point to where you have the thermostat python script!

I have added comments and set configurations as variables so you can copy my PHP file and use it for any other projects that would like to interact with Alexa. The section of interest would be Responses. You set two things: 1. The speech Alexa will respond with. 2. The little web card that appears on your Echo homepage.

Create an Alexa Skill


If you do not have one already, create an Amazon developer account. We will create a new "Alexa Skills," which is what Amazon calls the scripts/applications that the Amazon Echo can use.

When ready, go to your Developer Console. At the top menu go to App & Services, and then select Alexa in the submenu.

Skill Information

  1. Click on the Add a New Skill button. 
  2. Set the Name to be any name you want to call this Skill (for reference).
  3. The Invocation Name will be the name you tell Alexa to let it know it is your application. I set it to thermostat.
  4. Set the Endpoint to the address you have the PHP file on your web server.
  5. Press Next.

Interaction Model

  1. The Intent Schema is the JSON data structure that our PHP file will be parsing. You pair a name with a data type. Data type options are available here. The structure I am using is below:

  2. {
        "intents": [
     {
         "intent":  "SetTemp",
         "slots": [
      {
          "name":"action",
          "type":"LITERAL"
      },
      {
          "name":"temp",
          "type":"NUMBER"
      },
      {
          "name":"duration",
          "type":"NUMBER"
      }
         ]
     }
        ]
    }
    

  3. The Sample Utterances is very important! This is essentially how the Echo will understand what you are saying. You should place every type of voice combination that you think you will say. The first word, in this case SetTemp, references the data structure in the Intent Schema. The rest are words that are to be said after "Ask Invocation_Name." So for my setup with the Invocation Name thermostat, I will not be writing the "Ask thermostat" part here. I will add any words that follow it. Do not use any numbers- spell everything out. My structure is below:

  4. SetTemp {eighty|temp} degrees
    SetTemp {eighty one|temp} degrees
    SetTemp {eighty two|temp} degrees
    SetTemp {eighty three|temp} degrees for {one|duration} hour
    SetTemp {eighty four|temp} degrees for {two|duration} hours
    
    SetTemp set my temperature to {eighty |temp}
    SetTemp set my temperature to {eighty one|temp} degrees
    SetTemp set my temperature to {eighty two|temp} fahrenheit 
    
    SetTemp set my temperature to {eighty |temp} for {one|duration} hour
    SetTemp set my temperature to {eighty one|temp} degrees for {two|duration} hour
    SetTemp set my temperature to {eighty two|temp} fahrenheit  for {three|duration} hour
    

  5. Press Save to check for errors. Then press Next.

SSL Certificate


Select the corresponding certificate setup that you are using. If you are using a purchased SSL certificate that is correctly set up, keep the first option. The other options are self-explanitory. 

Testing


You are now ready to begin testing your script. Go to your Echo homepage. On the left side you should see Skills under Alarm. Go in there and make sure the Skill we created is enabled. If you are using my setup, you can now ask Alexa to control your thermostat. Some examples are:
  • Alexa, ask thermostat for status.
  • Alexa, ask thermostat for settings.
  • Alexa, ask thermostat for 78 degrees.
  • Alexa, ask thermostat for 78 degrees for two hours.
  • Alexa, ask thermostat to cancel.

If Alexa does not respond with anything after you ask it, check the Sample UtteIrances you have set. It is very picky. For example, I had "eighty one" as an invocation. So when I said "eighty," it did not understand and I had to add it.

If Alexa responds that there was an error, check the Echo homepage for more information such as an issue with your SSL certificate. Use your web card to debug as well as PHP error_log functions to get more information.

Success!


I would prefer to say "Alexa, set thermostat..." but it seems to be a keyword reserved for other devices. Alexa will reply that there are no devices set up with that name. Hopefully my steps have been successful for you and you are now dangerous with the Amazon Echo. I found the Echo to be a great additional to my home automation setup! I'm excited to see more creative applications people can come up with.

24 comments:

  1. I was hoping that this was possible so thanks for putting it up here!
    I did have a question about using this on Lambda. I have everything set up on Lambda and have the trigger set to Alexa. When asked, Alexa says that it can't communicate with my Honeywell app. I have limited programming knowledge but my thought was that since I don't have your PHP code anywhere (on a web server), then this isn't going to work. So my question is can your python code work alone with Lambda or do I need to have the PHP used (for triggering, etc.).
    I'm not looking for tech support so if this is more than a brief answer, I'll just set up the web server per your instructions and go from there. I thought using Lambda might be an easier solution for me.
    Thanks!

    ReplyDelete
    Replies
    1. Hi Mark! Thanks for checking out my article.

      You are correct. The Python script does not have the ability to talk directly with the Echo service. When Amazon does not receive a reply, it will say it could not communicate. The PHP code acts like the middle-man between Amazon and the Python script. It will capture the request from Amazon and then give it the reply Amazon is looking for.

      I am not too familiar with Lambda, but it seems it could handle the Python part but not the PHP part.

      Delete
  2. Your sample utterances doesn't include status or settings. How can you say "Alexa, ask thermostat for status." if it's not in the sample utterances?

    ReplyDelete
    Replies
    1. You are correct that it is required. My example did not fully include everything. My complete set is:

      SetTemp {cancel|action}
      SetTemp {status|action}
      SetTemp {settings|action}

      SetTemp {eighty|temp}
      SetTemp {eighty one|temp}
      SetTemp {eighty two|temp}
      SetTemp {eighty three|temp}

      SetTemp {eighty|temp} degrees
      SetTemp {eighty one|temp} degrees
      SetTemp {eighty two|temp} degrees

      SetTemp {eighty|temp} degrees for {one|duration} hour
      SetTemp {eighty three|temp} degrees for {two|duration} hours
      SetTemp {eighty four|temp} degrees for {three|duration} hours

      SetTemp set my temperature to {eighty |temp}
      SetTemp set my temperature to {eighty one|temp} degrees
      SetTemp set my temperature to {eighty two|temp} fahrenheit

      SetTemp set my temperature to {eighty |temp} for {one|duration} hour
      SetTemp set my temperature to {eighty one|temp} degrees for {two|duration} hour
      SetTemp set my temperature to {eighty two|temp} fahrenheit for {three|duration} hour

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I tried this step by step, and as it is my first time to ever attempt something like this, Alexa says there was a providence with the requested skills response. Any pointers?

    ReplyDelete
    Replies
    1. Does Amazon ever reach your server? It could be an issue with the SSL certificate. If Amazon doesn't like it, it kills the connection immediately.

      When debugging, I would put error_log() statements in the php file to know where exactly the session would die.

      Delete
  5. I am using a Mac and have set up the Python script with no problem as I have done it with Smartthings and plan to host it on the Amazon's AWS Lambda free tier. But from there on I am lost, is there any more details or an easier step by step? I hate being a pain in the butt, but I am really confused with this.

    ReplyDelete
    Replies
    1. I would like to get it working with AWS Lambda too, but since they only allow JavaScript the entire script would need to be converted from python. I would like to eventually take a shot at trying to port it though.

      Delete
  6. I just got it working on a Debian box after trying first on Raspberry Pi and then Ubuntu. My biggest obstacle was the SSL stuff but finally got that fixed this morning after 6 hours the day before.

    Now I'm trying to find the API so I can expand on what it does. I would like to turn the fan from Auto to Circulate to Off and a few other things but your program doesn't really tell me what the other stuff requires.

    I do want to thank you for the excellent documentation though.

    ReplyDelete
    Replies
    1. Another user seems to have commented on adding the fan on the bottom of the github page:

      https://gist.github.com/ghostbitmeta/694934062c0814680d52

      I will take a look at it and merge it in when I get the chance.

      Delete
  7. The python script works great on my server. But no matter what I try Echo tells me it has a problem communicating with requested skill. I have my own self signed certificate on apache server running on Ubuntu. Getting skills to work with the echo is very frustrating.
    I know my server is open as I can reach it via https so that is not the issue. I have the python and php files both in my var/www/html folder and have the $cmd line in php pointing to /var/www/html/echo.php also tried just echo.php. I suspect the issue is somewhere in the certificate but no idea where to start.

    ReplyDelete
    Replies
    1. Have you taken a look yet at Amazon's documentation for using self-signed certs?

      https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/testing-an-alexa-skill#Configuring%20Your%20Web%20Service%20to%20Use%20a%20Self-Signed%20Certificate

      Delete
  8. What do you have your echo.php and therm.py chmoded to?

    ReplyDelete
    Replies
    1. Never looked at that. What should it be? I do not do much Linux so bare with me.

      Delete
    2. Hi,

      My echp.php is set to 644: RW for owner and read for others. therm.py is set to 755, allowing anyone to execute it.

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Hi

    What to do, if I have more than one Honeywell RTH9580WF WiFi thermostat?
    I have three for three zones. Any idea appreciated.

    Thanks

    Jozsef

    ReplyDelete
  11. I just got my Echo for Christmas and I've been scouring the internet to learn more about providing custom programmed tech for it. Thank you for providing a wonderful post on setting up a thermostat with this.

    I wanted to note that now LetsEncrypt.org is live and out of beta, so getting Free SSL certs is easy :)

    ReplyDelete
  12. Echo skill to control Honeywell WiFi is now available.

    ReplyDelete
  13. Hi. It was very interesting to read. Thanks for sharing. Smart home is very popular now. I've tried to make my air conditioning system a part of general controling device, but I failed. I've asked this guys http://myairmatics.com/ for help and they helped me out. I decided to get an AC controll over my phone for now.

    ReplyDelete
  14. Dan, my name is Scott. I have some questions I'd like to ask you about the Honeywell script (I wish to have the thermostat set as a result of data coming from my solar array). How can I reach you by email? Perhaps email me, and I can reply? My address is my name, sterlingservices is the domain, and it is the original commercial TLD. Sorry, but spam, y'know! :)

    ReplyDelete
  15. Hi, I hacked your PHP code about to do something slightly different for my own purposes: https://gist.github.com/Westlad/d6c72a0e6597e9723dcc974018e9eae0
    I just wanted to check you are ok with that, as the original author.

    ReplyDelete