-
-
Save michaeldavie/42a63247f11ca43c98d4 to your computer and use it in GitHub Desktop.
Script to fetch Nest data and outdoor temperature, wind speed and wind direction data and log 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 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 | |
} | |
// Get Wind direction in degrees and convert to compass direction | |
var langWindVerbose = new Array( /* can be used in text for wind direction */ | |
"North", "North-Northeast", "Northeast", "East-Northeast", | |
"East", "East-Southeast", "Southeast", "South-Southeast", | |
"South", "South-Southwest", "Southwest", "West-Southwest", | |
"West", "West-Northwest", "Northwest", "North-Northwest"); | |
function windDirVerbose (winddir) // user language North-Northwest | |
// Take wind direction value, return the VERBOSE | |
// text label based upon 16 point compass -- function by beeker425 | |
{ | |
return langWindVerbose[Math.floor(((parseInt(winddir) + 11) / 22.5) % 16 )]; | |
} // END function windDirVerbose | |
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"]; | |
Logger.log("Current Temp: "+current_temp+", Target Temp: "+ target_temp +", Humidity: "+ humidity*100 + "%" ); | |
var time = new Date(); | |
var wxrequest=UrlFetchApp.fetch('http://api.openweathermap.org/data/2.5/weather?q=<YOUR ZIP CODE>'); | |
var wxresult=JSON.parse(wxrequest.getContentText()); | |
var outside_temp = (wxresult["main"]["temp"] - 273); | |
// Get wind speed in km/h | |
var wind_speed = (wxresult["wind"]["speed"] * 3.6); | |
// Get wind direction | |
var wind_direction = (wxresult["wind"]["deg"]); | |
var wind_direction = windDirVerbose (wind_direction); | |
var ss = SpreadsheetApp.openById("1iik6dc1MKBRxqaaffT7eOAJAxvztJjvwcX7ZzqpP4RY"); | |
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, wind_speed, wind_direction, humidity] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment