As the days start to get cooler again in North Carolina I’m always left with this conundrum… should I leave the window open or not?
On one hand I could feel a pleasant draft that cools me and my wife down all evening without having to pay for air conditioning, on the other hand I could end up waking up to a soaked wall as it’s rained in the middle of the night or the air conditioning has kicked on and I’m pumping lovely cool air that I’ve just paid for directly out the open window.
#firstworldproblems…. I know but it is never the less something I think about. So what can be done here? I’ve got a Nest thermostat and am a reasonably competent scripter.
Here is what I put together, a quick webpage that you can hit from any device on the home network and displays the following info…
Turns out there are a few libraries that help us get off to a useful start.
- weather-cli — this one allows us to pull real time hourly weather info for the next 24 hours based on precise location.
- flask — flask can be used to quickly serve up a webpage in python
- python-nest — this is used to get/set data with Nest thermostats.
Interestingly, python-nest and the nest-API provides some hourly weather info back in a pretty useful form. However it is missing one pivotal piece of data… which is the actual weather conditions for those individual hours, is it raining? is it sunny? that very important datapoint is not present and pretty much the primary reason I’m using weather-cli here.
There’s not a ton of documentation on weather-cli but for the few things that I wanted to know that weren’t documented, there’s always the source-code, and the code was helpful here.
sudo apt-get install python-pip
sudo pip install pip --upgrade
sudo pip install setuptools --upgrade
sudo pip install weather-cli flask python-nest
Setting Up Weather-CLI
To use weather-cli, you’re going to need a Forecast.io API key, register here to obtain one. With the basic free plan, you have 999 API calls per day for free, enough to check the weather a couple hundred times per day which is more than enough for me.
To get weather-cli set up we need to use the setup argument.
pi@underhousepi ~/scripts/weather_predictor $ sudo weather-cli --setup
Enter your forecast.io api key (required):xxxxxxxxxxxxxxxxx
The script will try to geolocate your IP.
If you fill the latitude and longitude, you avoid the IP geolocation.
Use it if you want a more acurated result, use a different location,
or just avoid the IP geolocation.
Check your latitude and longitude from http://www.travelmath.com/
Enter the latitude (optional): 36.982917
Enter the longitude (optional): -75.402301
generating config file...
Once setup is complete we can simply call weather-cli and it will return data for us based on the location we used for initial setup.
The rest is writing a quick flask app. Here is the code for that in the gist below. You’ll notice the flask app calls a template. I’ve placed that index.html template in a templates directory as shown below.
Note that I’m also calling the nest api to query the target temperature of the nest device and using that as a basis for whether or not the window open. You could hard-code that value alternatively if you don’t have a nest or for testing purposes. I also have more than one nest in my home so in line 29 I’m calling devices to get to query the second nest in my environment. You may want to change this to devices or another value based on your output from the python-nest api’s documentation page.
user@raspi4 ~/scripts/window_open $ tree
│ └── index.html
Here is the code for the template too.
Hope this might save you a few minutes in your search for cool air at an affordable price.