I’ve been trying to find a good project to really leverage some API’s and even go a step further and implement some server-less functions. I really thought the best application for this would be at work, I mean where else am I going to find a project that needs this kind of stuff? That was until I got an Ecobee thermostat. My hope is convey how revolutionary this standard is to today’s technology. I feel as passionately about it as I do open source software and I want people to know how empowering this simple approach to software design has been for consumers, system integrators, and developers.
When I installed the Ecobee I ended up deciding about a week later that I’d buy a sensor pack to monitor temps in other parts of the house. This is because our bedroom is the hottest place in the summer and one of the coldest in the winter while the basement is the coldest in the summer and in the winter I run a pellet stove when we’re home, making it the hottest. I figured the sensors would work to automatically circulate air to bring the temperature to a pleasant average, not the case. I’m not going to get into what they actually do but they do provide temperatures. There’s another thing the Ecobee provides, an API. I want the air to circulate via my fan turning on when the temperature has more than a 3.9 degrees difference between my basement, main floor, or bedroom. The Ecobee API doesn’t offer that as a setting but what it does offer is current temperatures and the ability to change it’s current settings / program. Here I can perform the logic. I can grab the temps, determine the temp difference, and adjust the schedule configuration to either turn the fan ON continuously or set to AUTO and then push that new configuration to the thermostat.
So what an API does is it really empowers a user or developer to leverage the subject in a way that was impossible in what I’ll call the “Dark Ages.” Anything that an API gives access to can be manipulated and worked in whatever way a user see’s fit. This is such a far cry from where we were with developed solutions not even 10 years ago. API’s are the norm today and allow integration among independent systems, platforms, or complete architectures. Good software today is expected to follow a solid API standard. Many web sites or platforms simply call an API backend infact, giving rise to alternative ways of managing products. Look at terraform for example. That whole project would likely never exist if this industry wasn’t built on an API today. I’ve been adamant since I learned what an API was that I need to be proficient with using them. All that said I guess I never thought one would be in my thermostat.
On to the fun stuff
So here’s where the fun begins. I have an API, I have something I want to do. What’s needed is some place to pull API information down to, process, and return a decision. For getting things off and ground and making sure the basic logic works the easiest place to start is a server I had with some spare clock cycles. I threw together a bash script with some very ugly sed commands to parse the API query and create my own response to adjust the programming. I was running this as a cron job (scheduled task) and performing it every 15 minutes.
With that out of the way and having a proof of concept, I wanted to take this a step further. I don’t want to have a server to worry about just to run this simple task. Lets make this server-less. Since I am an Azure architect, that’s of course the first place I’m going to turn. Azure Function Apps allow you to pay for a couple CPU cycles so to speak to just run a code snippet based on a timer or event. They support a few languages, none of which I’m familiar with except Python which is still in preview. My next step was to convert my ugly bash script to python and see how that fared.
FYI, in both the previous script and the coming python script, a token.json file is referenced. This is another repeating task that maintains an authentication session with the ecobee API. It’s simple and if you explore the API I’m sure you can guess what it really is. You can find the code though at https://github.com/mtz4718/ecobee-circulate
So converting to Python has been a success. The last big hurdle I faced was getting the code to run in Azure. Tonight I finally made that happen. I’ll do a followup post on getting Python Code running in Azure as a Serverless function and using more, you guessed it, API’s; to push information to an azure storage account. Hopefully it’ll contain something of value to you.