Skip to content

Instantly share code, notes, and snippets.

@beezly
Last active April 19, 2023 13:14
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save beezly/9b2de3749d687fdbff3f to your computer and use it in GitHub Desktop.
Save beezly/9b2de3749d687fdbff3f to your computer and use it in GitHub Desktop.
Log Nest temperatures into a Google Spreadsheet. Update the username and password values and create a resource trigger to call "getData" at regular intervals.
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 login_auth = performLogin('<YOUR NEST USERNAME>','<YOUR NEST 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"];
Logger.log("Current Temp: "+current_temp+", Target Temp: "+ target_temp +", Humidity: "+ humidity*100 + "%" );
var time = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
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, humidity, heater_state, auto_away ] );
}
@davidrea
Copy link

Very cool! Thanks for this... I added an outside temperature fetch from OpenWeatherMap.org here: https://gist.github.com/davidrea/17ba3aebfa65c933daca (don't think Gists support PRs though...)

@N8-KC
Copy link

N8-KC commented May 18, 2015

Any idea why im getting this error?
5/17/15 1:26 PM getData SyntaxError: Empty JSON string (line 37, file "Code") time-based 5/17/15 1:28 PM
5/17/15 1:41 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 1:42 PM
5/17/15 1:43 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 1:44 PM
5/17/15 1:56 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 1:57 PM
5/17/15 2:11 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 2:12 PM
5/17/15 2:26 PM getData Unexpected error: https://transport04-rts08-iad01.transport.home.nest.com:443/v2/mobile/user.44835 (line 36, file "Code") time-based 5/17/15 2:28 PM
5/17/15 2:41 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 2:42 PM
5/17/15 2:56 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 2:57 PM
5/17/15 3:11 PM getData SyntaxError: Empty JSON string (line 37, file "Code") time-based 5/17/15 3:13 PM
5/17/15 3:26 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 3:27 PM
5/17/15 3:41 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 3:42 PM
5/17/15 3:56 PM getData SyntaxError: Empty JSON string (line 37, file "Code") time-based 5/17/15 3:57 PM
5/17/15 4:11 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 4:12 PM
5/17/15 4:26 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 4:27 PM
5/17/15 4:41 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 4:42 PM
5/17/15 4:56 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 4:57 PM
5/17/15 5:11 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 5:12 PM
5/17/15 5:26 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 5:27 PM
5/17/15 5:41 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 5:42 PM
5/17/15 5:56 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 5:57 PM
5/17/15 6:26 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 6:27 PM
5/17/15 6:41 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 6:42 PM
5/17/15 6:56 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 6:57 PM
5/17/15 7:11 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 7:12 PM
5/17/15 7:35 PM getData Request failed for https://home.nest.com/user/login returned code 429. Truncated server response: Too many requests (use muteHttpExceptions option to examine full response) (line 12, file "Code") time-based 5/17/15 7:36 PM
5/17/15 7:56 PM getData SyntaxError: Empty JSON string (line 37, file "Code") time-based 5/17/15 7:58 PM
5/17/15 8:26 PM getData Request failed for https://home.nest.com/user/login returned code 429. Truncated server response: Too many requests (use muteHttpExceptions option to examine full response) (line 12, file "Code") time-based 5/17/15 8:27 PM
5/17/15 8:41 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 8:42 PM
5/17/15 8:56 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 8:57 PM
5/17/15 9:11 PM getData SyntaxError: Empty JSON string (line 37, file "Code") time-based 5/17/15 9:13 PM
5/17/15 9:26 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 9:27 PM
5/17/15 9:41 PM getData Unexpected error: https://home.nest.com/user/login (line 12, file "Code") time-based 5/17/15 9:42 PM
5/17/15 10:26 PM getData SyntaxError: Empty JSON string (line 12, file "Code") time-based 5/17/15 10:27 PM

@p10tyr
Copy link

p10tyr commented Nov 10, 2015

Possibly the entire site got changed and this no longer works

@taylorbrinton
Copy link

I just got this code setup and I have 2 nest in two homes under one login. I wondering if you know how I would tell it what device ID to look at. It is just picking one and its not the one I want. I don't understand what part of the code I would update to pick the device. Any help would be awesome. Thanks

@BBh8ter
Copy link

BBh8ter commented Jul 13, 2016

Like @taylorbrinton, I have two Nests and all variants of this code I've found so far seem to ignore that potential. Not sure which one it's keying off yet, or if that would potentially change in the future at random.

@brywhi
Copy link

brywhi commented Aug 28, 2018

Getting an error now. Anyone else seeing this?
Request failed for https://home.nest.com/user/login returned code 429. Truncated server response: Too many requests (use muteHttpExceptions option to examine full response) (line 40, file "nestscript", project "neststuff")

@ivanbojer
Copy link

@brywhi You are over your hourly rate limit however I cannot find what are the imposed limits.

@plinley
Copy link

plinley commented Jan 15, 2019

Does this still work?
Sorry, I'm a newbie with scripts... do I enter the authentication like this...?

I assume the var payload section below is for my google username and password??
var payload = {
"here@there.com" : email,
"MyP@ssw0rd" : password

Then the var login auth is for my nest auth as follows:
function getData() {
var login_auth = performLogin('here@there.com','MyP@ssw0rd');

Thanks

@smosab
Copy link

smosab commented Jan 14, 2020

Does this still work?
Sorry, I'm a newbie with scripts... do I enter the authentication like this...?

I assume the var payload section below is for my google username and password??
var payload = {
"here@there.com" : email,
"MyP@ssw0rd" : password

Then the var login auth is for my nest auth as follows:
function getData() {
var login_auth = performLogin('here@there.com','MyP@ssw0rd');

Thanks

I'm having the same issue @plinley. I don't think this script will work if you migrated your account to Google.

@beezly
Copy link
Author

beezly commented Jan 14, 2020

I've never tried with a Google migrated account. It might need changing to get it working.

@davelalande
Copy link

I'm having the same issue with a "migrated to Google" account.

Thanks

@AHThomas
Copy link

Migrated accounts broke the login code used here. I don't have a fix, but here's a thread that discusses possible fixes/workarounds: gboudreau/nest-api#95

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