Skip to content

Instantly share code, notes, and snippets.

@jbutters
Forked from davidrea/gist:17ba3aebfa65c933daca
Last active August 21, 2019 20:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbutters/bece2fffe85080fe4314 to your computer and use it in GitHub Desktop.
Save jbutters/bece2fffe85080fe4314 to your computer and use it in GitHub Desktop.
function doGet() {
// you only need to modify the next four lines of this code then publish web app
var email = 'email address'; //what you use to login to nest
var password = 'password' ////what you use to login to nest
var zip = 'zip code';
var sheetid = 'sheet id'; //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"];
var target_temp = result["shared"][device_id]["target_temperature"];
var humidity = result["device"][device_id]["current_humidity"]/100;
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?q=' + zip + '&units=imperial');
var wxresult=JSON.parse(wxrequest.getContentText());
var outside_temp = (wxresult["main"]["temp"]);
var ss = SpreadsheetApp.openById(sheetid);
var sheet = ss.getSheets()[0];
// Appends a new row with 3 columns to the bottom of the
// spreadsheet containing the values in the array
sheet.appendRow( [ time, current_temp, target_temp, outside_temp, humidity, 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);
}
@SergeantScar
Copy link

There are a few places right off the bat that I noticed that semi-colons are missing.. I wasn't able to get this to work for me. I am trying to recreate what you are doing here though. I kept getting log-in failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment