Thursday 19 June 2014

How to make your Nest Thermostat email you when "auto-away"

While Nest are still to release details of their API, some clever people have already deconstructed the methods by which your Nest Thermostat (and Nest Protect) talks back to Nest Cloud HQ and have released a PHP script which can pull data from Nest Cloud HQ, allowing you to do with it, whatever you want!

Some examples of the things that I am currently doing:

1. Graphing my internal Nest temperature reading using PRTG


2. Capturing and emailing me when Nest thinks I'm "away" and when i'm back.

3. Monitoring the internal battery voltage of the thermostat - could potentially be used to notify me of a power cut whilst away from home (for example, if the voltage drops below a certain level).

Gathering and using all this data first relies on capturing it with the improvised Nest API.

Here's what you'll need to do:

1. Visit the link above and download the nest.class.php file
2. Publish the above on a web server that supports PHP, and has Internet access
3. Create another PHP file that "includes" the above class and gathers and presents the data you want.  There are examples on GitHub, and I've included my own hacked together example here:


 <?php  
 header('Content-Type: text/xml');  
 require_once('nest.class.php');  
 // Your Nest username and password.  
 $username = 'your@nest.email.address';  
 $password = 'YourNestPassword';  
 // The timezone you're in  
 // See http://php.net/manual/en/timezones.php for the possible values.  
 date_default_timezone_set('Europe/London');  
 $nest = new Nest($username, $password);  
 // Get the device information:  
 $infos = $nest->getDeviceInfo();  
 // Start XML Output  
 echo "<nest>";  
 // Print the current temperature  
 printf("<nesttemp> %.01f </nesttemp> \n", $infos->current_state->temperature, $infos->scale);  
 // Print the current humidity  
 printf("<nesthumidity> %.01f </nesthumidity> \n", $infos->current_state->humidity, $infos->scale);  
 //Print Auto Away 0 or 1  
 printf("<autoaway>%.0f</autoaway>", $infos->current_state->auto_away);  
 //Print Manual Away 0 or 1  
 printf("<manualaway>%.0f</manualaway>", $infos->current_state->manual_away);  
 //Print Battery Level  
 printf("<battery>%.02f</battery>", $infos->current_state->battery_level);  
 //Close XML  
 echo "</nest>";  
 ?>  

When you GET this php file, it shows XML output like this:

 <nest>  
 <nesttemp>24.6</nesttemp>  
 <nesthumidity>39.0</nesthumidity>  
 <autoaway>0</autoaway>  
 <manualaway>0</manualaway>  
 <battery>3.94</battery>  
 </nest>  

4. Now using something like PRTG (free for a licence of 10 sensors) and the HTTP XML Sensor type, you can GET your PHP script every few minutes (I wouldn't recommend more than once every 5 minutes), and have PRTG automatically interpret the XML into sensor values.  All you need to do is tell PRTG the full web address of your php file and also which XML tags you want to capture.  The sensors it produces will look like the ones shown below:



5.  Lastly, once you have the data captured, graphed etc, you can set-up a notification within PRTG to send an email when a certain value is seen on a sensor.  In our case, to email us when the Auto Away sensor = 1


I've found that setting a single threshold trigger when the value = 1 and then another notification when the condition clears seems to work best - rather than 2 separate threshold triggers for 1 and 0.

There are numerous other things you could do with this depending on what you want to monitor and the Nest API allows you to SET certain parameters as well as GET them.