One of the local news stations in Grand Rapids has a lighted weather ball that gives you a quick snapshot of the coming day's forecast by changing colors. It over looks an intersection of freeways and is pretty hard to miss when driving into downtown.When I first moved here I'd never seen anything like it. It was a weird, cheesy looking thing. But over time I came to appreciate the sight. Seeing the weather ball meant I was almost home.

blue - which means the temperature tomorrow will be cooler

So when we got the Philips Hue light bulbs the first project Chris suggested was a weather ball (well, more like lamp). We wanted a visual cue in the morning before work as to what the weather would be like that day. So the Hue Weather Lamp basically indicates four things:
  • If the temperature for today is cooler than yesterday, the lamp will turn blue
  • If the temperature for today is warmer than yestrerday, the lamp will turn red
  • If the temperature for today is the same as yesterday, the lamp will turn green
  • If there is a 70% or higher chance of precipitation the lamp will be purple, regardless of a temperature increase or decrease.
green - which means the temperature tomorrow will be the same

The whole thing is pretty simple. All of the files can be found here. Here's what I did.

First, I installed the python library for the Philips Hue which can be found on github here: phue. Setting up the Hue python library is fairly straight forward, as long as a username has already been established with the Hue Hub. If not, that will need to be done first (this is how I did it)

Once the python module is installed (or a work around like I did) all that needs to be done is to set up authentication with the b.connect() command. The basic steps are:
  • Press of the "connect" button on the Philip Hue Hub
  • Run the python script with b.connect() (examples are in the phue github)
  • Voila! Authenticated.
purple - which means a 70% or higher chance of precipitation tomorrow

Next, once I had access to the Hue, I started work on a python script to check the weather and then adjust the lamp accordingly. I started with the python script from the Kindle Weather Display and adapted it to pull both the weather and chance of precipitation from NOAA.

Since the NOAA page doesn't keep yesterday's forecast, I had to figure out a way to log and save yesterday's temp for comparison purposes the next day. For my purposes, I only need the lamp to turn on once a day, in the morning, so I decided writing the value to a simple text file was the easiest way to achieve this. 

The python script goes through these basic steps:
  • reads a text file for yesterday's temp
  • pulls weather forecast from NOAA
  • compares yesterday's temp from the text file to today's foretasted temp from NOAA
  • assigns the appropriate color based on temperature difference
  • write today's temperature over yesterday's temperature in the text file so the data is ready for tomorrow morning
  • turn the lamp off after one hour
Now, the text file needs to be manually set with yesterday's temperature before the first script run. After that the python script will continue to write to the text file and be ready for the next day.

red - which means the temperature tomorrow will be warmer

After the python script was complete, the next step was to set up a schedule on the web server to run the python script at a certain interval. Since the intent was to see a quick glance in the morning before work, I set up a cron job to run in the morning. Here's what I'm using on my web server.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

#execute by typing crontab
# m h  dom mon dow   command
  0 7 * * *    /usr/bin/python /PATH/TO/FILE/HueWeatherLamp.py

# this is an alternative cron job example if there are any issues running the python script directly in cron
#  0 7 * * *    /PATH/TO/FILE/weatherballpy.sh


There are a few additional things I'd like to play around with at some point, but most importantly I think I'd like to adjust the python script to count count days +/-5 degrees as the same day. A single degree difference warmer or cooler isn't cause for celebration, but five or ten degrees would be. I'll probably let this run as-is for awhile before making that adjustment though.

UPDATE 1/7/2013: I had some issues running the script directly from a cron job. It would execute the python script partially (turn the lamp on) but not change the color. I created a shell script to execute the python script and that works fine. I've updated the below download files with the shell script and a modified cron job example.

NOTE: as noted in the comments by the phue author: You do not need to have a registered username prior to using the library. The library will register itself, for example you can call
b = Bridge(IPADDR) then call connect() and it will register the python_hue app on the bridge and save the md5 key to your home folder in .python_hue
Downloads
Github: Hue Weather Lamp
of if you prefer - dropbox link: Hue Weather Lamp

Chris has wanted the Philips Hue light bulbs for awhile. They're wifi-controlled LED light bulbs that you can control locally or via the internet with your iOS (or android) device. So for Christmas he received a starter kit that came with three bulbs and he's been enjoying changing the lights in the rooms to different colors. Honestly having the two different colors in the same room is somewhat irksome.

One of the really nice things about the Hue is it's hackability, and I wouldn't have even known about that if not for this awesome ars technica review. There's a pretty vibrant hacking community and the ars review does a really nice job laying out how to do everything. There's even a nice javascript file to set everything up for yourself. While I don't personally have any experience in javascript, Chris does. And the internet has an answer for anything we can't figure out.

So armed with the information from the ars review and information from rsmck, who has been pioneering hue hacking, I got to work.
The first order of business was to get access to the Philips Hue Hub so I could make requests (changing the colors, turning lights on and off). The way to do this is to make a HTTP POST request of the Philips Hue hub to establish a username. That username is then used to make changes to the various light bulbs.

I tried a few ways yesterday to get a POST request to work in windows with python and javascript, based on some examples I saw online and stuff Chris helped me with. No luck.
So tonight I logged into the Raspberry Pi I have running a web server for the Kindle, and tried my hand at cURL.

Not surprisingly, I'm not the only one who tried this and I found a pretty handy example of what to do. Here's what worked for me via the command line in linux:

curl -d "{\"username\": \"PLACE YOUR USERNAME HERE\", \"devicetype\": \"PLACE YOUR DEVICE NAME HERE\"}" http://PLACE YOUR HUB IP ADDRESS HERE/api

As noted on rsmck's website, the first thing I saw was an error saying the link on the hub wasn't pressed. So...I pressed it and tried again.

Success!
Next I downloaded and opened up the javascript file Lee Hutchinson from ars technicia provided in the Hue review. All that was needed was a simple tweak to the file to add my hub's IP address and the username I just established. Then I dropped the file on the Pi web server and away I went!
We can now control the light bulbs via the web server if we'd like. Sure, I could have just done that with the iOS app, but there's a lot more potential here. With access to the Hue Hub I can schedule the lights to act as a sunrise lamp on weekdays but not weekends, or only turn on on certain days of the week. Since the Philips app wont let you schedule anything based on weekdays or weekends (or even indefinitely) the whole concept of scheduling a lamp to slowly go on or off is out the window. And since I know Chris has always wanted a sunrise lamp, that's exactly what I needed.

I look forward to tweaking this over the next week while I'm off work. And the really great part is that Chris knows javascript and he's pretty excited to set up things how he wants them. Plus he can code and design the website and make it really nice.