Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created September 10, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hrbrmstr/2da2312170a84340c14f to your computer and use it in GitHub Desktop.
Save hrbrmstr/2da2312170a84340c14f to your computer and use it in GitHub Desktop.
working with the Nest API in R - code for http://rud.is/b/2015/09/10/a-better-way-to-read-nest-data-into-r/
library(httr)
# Go to https://developer.nest.com/clients to register a new Nest client
# Nest OAuth 2.0 endpoints
nest <- oauth_endpoint(
request=NULL,
authorize="https://home.nest.com/login/oauth2?state=login",
access="https://api.home.nest.com/oauth2/access_token"
)
# Store your secret in NEST_CONSUMER_SECRET in .Renviron
nest_app <- oauth_app("nest", key="a8bf6e0c-89a0-40ae-869a-943e928316f5")
# Configure your app to require a PIN. The user will have to cut/paste
# the URL in the R Console into a browser then cut/paste the PIN
# back into the R Console
nest_token <- oauth2.0_token(nest, nest_app, use_oob=TRUE, cache=TRUE)
# Nest API usage examples
# Get structures
req <- GET("https://developer-api.nest.com",
path="structures",
query=list(auth=nest_token$credentials$access_token))
stop_for_status(req)
structures <-fromJSON(content(req, as="text")
# Get devices
req <- GET("https://developer-api.nest.com",
path="devices",
query=list(auth=nest_token$credentials$access_token))
stop_for_status(req)
devices <- fromJSON(content(req, as=text))
# Get 1st thermostat readings
first_thermostat <- names(devices$thermostats)[1]
req <- GET("https://developer-api.nest.com/",
path=sprintf("devices/thermostats/%s", first_thermostat),
query=list(auth=nest_token$credentials$access_token))
stop_for_status(req)
thermo <- data.frame(fromJSON(content(req, as="text")),
stringsAsFactors=FALSE)
cat(thermo$ambient_temperature_f, "F / ", thermo$humidity, "%", sep="")
@wabmaster7
Copy link

wabmaster7 commented Mar 14, 2017

I'm having difficulty mapping the Nest API curl PUT call into this R wrapper in httr.
I'd like to change the low temperature setting from say 68F to 72F.

the Nest API doc shows the curl info as:
curl -L -X PUT
-H "Content-Type: application/json"
-H "Authorization: Bearer c.lPg4Z..."
-d "{"target_temperature_f": 72}"
'https://developer-api.nest.com/devices/thermostats/DEVICE_ID'

my code which returns a HTTP400 bad request error is:
req <- PUT("https://developer-api.nest.com/",
path=sprintf("devices/thermostats/%s", first_thermostat),
query=list(auth=nest_token$credentials$access_token, target_temperature_low_f=72))
stop_for_status(req)
thermoSet <- data.frame(fromJSON(content(req, as="text")),
stringsAsFactors=FALSE)

I'm guessing I don't have the -d "{"target_temperature_f": 72}" coded correctly in the PUT request.

@jrosen48
Copy link

Super minor, and know this is old (but I think it's still the best example in R!):

I think library(rjsion) needs to be added to the top of this script.

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