Tuesday, June 11, 2013

Home Automation

We hope you love your new Nest. - Tony Faddell
For our two-year anniversary Mrs.Chaos got me a Nest home thermostat.  It has the simple benefits of just being a super cool home thermostat with neat features about setting complex schedules and auto-detecting when I leave the house to switch to away mode.  We live in the age when robots should be doing so much more than washing our dishes and clothes - they should be vacuuming and mopping our homes, mowing our lawns, and so on.
I think my favorite "feature" is the undocumented API that is available.  So I can read various settings from the Nest and then do things based on it.  Recently I wrote a script that checks the temperature of the house through the Nest API and then checks the temperature outside using Forecast.io's API.  As soon as it cools down outside below the inside temperature it sends a notification to me to open my windows!
The other script I want to build is to get a few more temperature gauges so that if one part of the house (office, bedroom) is more than five degrees different from the others it I can just kick on the fan-mode to circulate the air.  Awesome, eh?  Yeah!

$forecast = new ForecastIO($api_key);
$nest = new Nest();
$windowStatefile = getenv("HOME") . "/Dropbox/scripts/nest/windowstate.txt";  
 $windowStateString = rtrim(file_get_contents($windowStatefile));  
 $windowState = "closed";  
 if($windowStateString == "open") {  
  $windowState = "open";       
 // Get the device information:  
 $mode = $nest->getDeviceInfo()->current_state->mode;  
 $insideTemp = $nest->getDeviceInfo()->current_state->temperature;  
 // Get the NEST reported tempurature  
 $locationArray = $nest->getUserLocations();  
 $nestOutsideTemp = $locationArray[0]->outside_temperature;   
 $outsideTemp = nestOutsideTemp;  
 $condition = $forecast->getCurrentConditions($latitude, $longitude);  
 $forecastIoOutsideTemp = $condition->getTemperature();  
 $outsideTemp = $forecastIoOutsideTemp;  
 if($mode == "cool"  
   && $windowState == "closed"  
   && $outsideTemp < $insideTemp ) {  
  $windowState = "open";  
  exec("/usr/local/bin/growlnotify -s --image ~/Dropbox/scripts/nest/nest.icns -n \"Nest\" -p Moderate -m \"It's cooling off, open your windows.\"");  
 } elseif ($mode == "cool"  
   && $windowState == "open"  
   && $outsideTemp > $insideTemp ) {  
  $windowState = "closed";  
  exec("/usr/local/bin/growlnotify -s --image ~/Dropbox/scripts/nest/nest.icns -n \"Nest\" -p Moderate -m \"It's warming up, close your windows.\"");  
 file_put_contents($windowStatefile, $windowState);