A smart home
The difference between a Connected Home and a Smart home resides in the “smart” part. A smart home does not only contain connected and remote controlled devices. It is a home where devices can:
- Learn from their environment to adapt their “behavior”,
- Delegate decision making to sophisticated back-end logic that can leverage historical data, provide processing capacity and execute workflows, resulting in improved user experience.
With its series of smart devices (smart thermostat, smoke detector and recently, home camera), Nest is targeting the first of the aforementioned points. scriptr.io, provides you with the second part and, thanks to its Nest connector, you can now easily connect your scripts to any of your Nest devices.
Developing apps for Nest with scriptr.io
scriptr.io’s connector for Nest wraps Nest’s REST APIs so they can natively be used from within your scripts.
Create a Nest application
Before you can start playing around with the connector, you first need to sign-up to the Nest developer website and create an application there.
Nest device simulator
Whether or not you already own Nest devices, you might be interested in using the simulator they offer as an extension to Google chrome. This latter allows you to create virtual devices in a virtual home, send instructions to them (home + devices) and follow-up on their status using a nice user interface. In the remainder of this article, we will consider that you installed the simulator and signed up to the Nest Home service and we will use the simulator.
Create virtual devices
If you installed the Nest simulator as suggested in the preceding paragraph, open your Google chrome browser (if needed) and open the “Developer tool”. You should see a new “Nest” tab on the right corner. Clicking on it opens a simple dashboard that allows you to add/modify virtual devices and make some basic settings on your “home”.
Obtain an OAuth access token from Nest and configure the scriptr.io connector
- Retrieve the connector’s script from Github and deploy them into a “nest” folder in your scriptr.io account.
- Using a REST client, issue a POST request to
https://api.scriptrapps.io/nest/authorization/getRequestCodeUrl
, passing your scriptr.io authentication token and a timestamp. The call should return something similar to the below:{ "response": { "metadata": { "requestId": "4963402e-aae2-4bff-8811-e8a64876ea33", "status": "success", "statusCode": "200" }, "result": "https://home.nest.com/login/oauth2?client_id=c245921b-0467-4677-879e-b25660e5d7f3&state=2ffb81" } }
- Copy the URL in the “result” field and paste it in a browser. This should route you to an authorization wizard at Nest’s premises.
- Enter your credentials and authorize the application. If everything is OK, you will be automatically redirected to the
nest/getAccessToken
script, of which execution results in displaying the value of your Nest OAuth token, as in the below:{"response": { "metadata": { "requestId": "806a02b7-b00f-4b7b-97d2-a1125bd866c0", "status": "success", "statusCode": "200" },"result": { "access_token": "c.js0BgJaVnfnIChyyzqou2j1wslSH6hXeFjM6Qveo3PjnZ9JBJQnmU9ORIOTqYZjwDBGfrFifwL4yPo4iHKqUfyTq3udfQJZiADq5jkfrn5LycL2zTHmYAfme9uhDcPl3HBMbeja9Mj7Qwaes", "expires_in": 315360000 } }
- In the
nest/config
script, replace theclient_id
andclient_secret
variables respectively to the value of theCLIENT ID
andCLIENT SECRET
fields of the OAUTH SETTINGS section - In the
nest/config
script, copy the values of theaccess_token
and expires_in obtained above into thetoken
variable.
Try it!
From scriptr.io’s workspace, create new script. In the latter, first import the Nest connector module as follows:
var clientModule = require("modules/nest/nestClient.js");
Now create an instance of the Nest client:
var nest = new clientModule.NestClient(true);
Using the client, you can now retrieve information from your devices and control them remotely. For example, let’s shift the target temperature of the “Den” thermostat (id = ‘A6dAEP1gLizQKLBd_M3Ml2Yp’) from 72 to 74 Fahrenheit degrees:
var deviceId = "A6dAEP1gLizQKLBd_M3Ml2Yp-ibWxUCQ"; nest.setTargetTemperature(deviceId,74,'f');
Let’s now assume that we need to switch the thermostat to “heat mode”. That’simple:
client.switchToHeatMode("A6dAEP1gLizQKLBd_M3Ml2Yp-ibWxUCQ");
There are many more useful methods that you can leverage in your applications. Check them all on Github.