Skip to content

Instantly share code, notes, and snippets.

@dickverbunt
Last active January 23, 2017 20:37
Show Gist options
  • Select an option

  • Save dickverbunt/9fe75def907de1d12e2d to your computer and use it in GitHub Desktop.

Select an option

Save dickverbunt/9fe75def907de1d12e2d to your computer and use it in GitHub Desktop.
Nest data Google Docs
function doGet() {
// you only need to modify the next four lines of this code then publish web app
var email = ''; //what you use to login to nest
var password = '' ////what you use to login to nest
var CityID = ''; //Openweather prefers the 6 digit city code to the zipcode. Find your city and check the url for the 6 digit city id code.
var AppID = '' // get your free Openweather AppID and enter it here
var sheetid = ''; //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': 'nl-nl',
'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"];
var target_temp = result["shared"][device_id]["target_temperature"];
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 time = new Date();
var wxrequest=UrlFetchApp.fetch('http://api.openweathermap.org/data/2.5/weather?id=' + CityID + '&APPID=' + AppID + '&units=metric');
var wxresult=JSON.parse(wxrequest.getContentText());
var outside_temp = (wxresult["main"]["temp"]);
var outside_humidity = (wxresult["main"]["humidity"]);
var max_out_tmp = (wxresult["main"]["temp_max"]);
var min_out_tmp = (wxresult["main"]["temp_min"]);
var avg_out_tmp = (max_out_tmp + min_out_tmp)/2;
//pressure is recorded in mm of mercury and recorded in inches
var pressure = (wxresult["main"]["pressure"]);
var weather = (wxresult["weather"][0]["main"]);
var outside_clouds = (wxresult["clouds"]["all"]);
var ss = SpreadsheetApp.openById(sheetid);
var sheet = ss.getSheets()[0];
// Appends a new row with 14 columns to the bottom of the
// spreadsheet containing the values in the array
// Remember heater_val is 5 for running and 0 for not running, this helps for those who want to graph data.
// Remove any items you don't want in the output. I'm still testing.. the max, min, avg...I may end up removing this data.
sheet.appendRow( [ time, current_temp, target_temp, outside_temp, heater_state, humidity, outside_humidity, weather, outside_clouds] );
}
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