Last active
February 5, 2016 08:45
-
-
Save thehotrod/4c4c4170a735733cd745 to your computer and use it in GitHub Desktop.
Script for Logging Data from Nest and WeatherUnderground to Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet() { | |
// you only need to modify the next six lines of this code then publish web app. Replace the XXXX's with your info | |
var email = 'XXXXXXXXXX'; //what you use to login to nest | |
var password = 'XXXXXXX' ////what you use to login to nest | |
var State = 'XX'; // WeatherUnderground State Name. 2 letter State name or Country ID | |
var City = 'XXXXXXXXX'; // Enter your WeatherUnderground City Name. Or your local station ID. Should look like this 'pws:localID' | |
var APIKey = 'XXXXXXXXXXXX' // get your free WeatherUnderground APIKey and enter it here | |
var sheetid = 'XXXXXXXXXXXXXXXXXX'; //on your spreadsheet url its everything between /d/ <sheet id> /edit | |
/* to publish web app just: | |
1) Make sure the four variables are set above before you publish | |
2) Click Publish --> Deploy as web app | |
3) Describe the web app and save a new version | |
4) Execute the app as: me (your google account) | |
5) Who has access to the app: Anyone, even anonymous (this what allows the timebased() triggers to run as expected) | |
6) Click Deploy | |
7) Set your timebased() trigger to run getData() which just does a url fetch of this script and invokes doGet() | |
*/ | |
var login_auth = performLogin(email,password); | |
var headers = { | |
"Authorization" : 'Basic '+login_auth['access_token'], | |
"X-nl-user-id" : login_auth['userid'], | |
"X-nl-protocol-version" : '1', | |
'Accept-Language': 'en-us', | |
'Connection' : 'keep-alive', | |
'Accept' : '*/*', | |
}; | |
var options = { | |
'headers' : headers | |
}; | |
var request=UrlFetchApp.fetch(login_auth['urls']['transport_url']+'/v2/mobile/user.'+login_auth['userid'], options); | |
var result=JSON.parse(request.getContentText()); | |
var structure_id = result['user'][login_auth['userid']]['structures'][0].split('.')[1] | |
var device_id = result['structure'][structure_id]['devices'][0].split('.')[1] | |
var current_temp = (result["shared"][device_id]["current_temperature"] * 1.8) + 32; | |
var humidity = result["device"][device_id]["current_humidity"]; | |
var auto_away = result["shared"][device_id]["auto_away"]; | |
var heater_state = result["shared"][device_id]["hvac_heater_state"]; | |
var heater_val = 55 | |
if ( heater_state ) {heater_val = 60; //use +60 for heat on, 55 for heat off. These two valyes are chosen so my temperature line graph can be zoomed in on the 55-80F range instead of 0-80 | |
} | |
var target_temp = Math.round(result["shared"][device_id]["target_temperature"]*9/5+32); | |
var away = result["structure"][structure_id]["away"]; | |
var away_val = 0; | |
if ( away ) { | |
target_temp = Math.round(result["device"][device_id]["away_temperature_low"]*9/5+32); | |
away_val= -5; //use -5 for away==on, 0=home | |
} | |
var time = new Date(); | |
var wxrequest=UrlFetchApp.fetch('http://api.wunderground.com/api/'+ APIKey + '/conditions/q/' + State + '/' + City + '.json'); | |
var wxresult=JSON.parse(wxrequest.getContentText()); | |
var outside_temp = (wxresult["current_observation"]["temp_f"]); | |
var outside_humidity = (wxresult["current_observation"]["relative_humidity"]); | |
var wind_speed = (wxresult["current_observation"]["wind_mph"]); | |
var precip_today = (wxresult["current_observation"]["precip_today_in"]); | |
var pressure = (wxresult["current_observation"]["pressure_in"]); | |
var weather = (wxresult["current_observation"]["weather"]); | |
var solar_radiation = (wxresult["current_observation"]["solarradiation"]); | |
var ss = SpreadsheetApp.openById(sheetid); | |
var sheet = ss.getSheets()[0]; | |
// Appends a new row with 13 columns to the bottom of the | |
// spreadsheet containing the values in the array | |
sheet.appendRow( [ time, current_temp, target_temp, outside_temp, heater_val, humidity, outside_humidity, pressure, weather, wind_speed, precip_today, solar_radiation, heater_state, auto_away ] ); | |
} | |
function performLogin(email, password) { | |
var payload = { | |
"username" : email, | |
"password" : password | |
}; | |
var options = { | |
"method" : "post", | |
"payload" : payload | |
}; | |
var response = JSON.parse(UrlFetchApp.fetch('https://home.nest.com/user/login', options).getContentText()); | |
if ('error' in response) { | |
throw "Invalid login credentials"; | |
} | |
return response | |
} | |
function getData(){ | |
var url = ScriptApp.getService().getUrl(); | |
var response = UrlFetchApp.fetch(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment