Skip to main content

Control Nest from linux shell (or other programs)

The steps below let’s you create a shell script to control the Nest Thermostat from the linux shell. Once created the shell script you can control your Nest Thermostat in other applications like PiMatic, Domoticz…

Step 1 – Create a Nest Developer account and product

Nest Developer Product ScreenYou need to create a Nest Developer account and product to get the API access you need to control your thermostat.
You must create a Nest Developer account here if you don’t have one – https://developer.nest.com/

Then create a Product.  Mine is called “Control Nest from shell”.  Make sure you grant read/write permissions for the Thermostat and Away.

Copy down the Product ID and Product Secret values.  You will need them later.  You will also need the Authorization URL in the next step so don’t close this window!

Step 2 – Get your PIN Code for your Nest Thermostat

Nest Thermostat Authorization ScreenCopy the Authorization URL shown on the right side of the Nest Developer Product Details page and paste it into a new browser window.  It should prompt you to allow your Product to connect to your personal Nest account.  Click on Continue.

It should then give you a PIN Code.  Copy this down carefully!!!

 

Step 3 – Get your Access Code

Now that you have a PIN Code, you need to generate an API Access Code using your PIN Code and the Product ID and Product Secret from your Nest Developer Product Details Page.

curl -X POST "https://api.home.nest.com/oauth2/access_token?client_id=%YOUR_PRODUCT_ID%&code=%YOUR_PIN_CODE%&client_secret=%YOUR_PRODUCT_SECRET%&grant_type=authorization_code"

Replace the %YOUR_*% values with the correct values for your application the Nest PIN. And replace & with &.  Then execute this in your command line.  You should get back a long access token that starts with a “c.”.  This long string is your Access Token and will be used for authorization for API calls.

Step 4 – Get Your Nest Thermostat and Structure IDs

Now that you have your Access Token you can retrieve the IDs for your Thermostat and Structure (Home) which you’ll need to setup the command line API aliases.

curl -L https://developer-api.nest.com/devices/thermostats\?auth\=%YOUR_ACCESS_TOKEN%

This command will return a big block of JSON data.  What you are looking for in there are two values: the device_id and the structure_id.  Copy those values, you will need them soon.

Step 5 – Create the shell script

Now you have your Access Token, Device ID and Structure ID it’s time to create the shell script to control your Nest Thermostat. Create a file with the data below, save it in /usr/bin and make it executable:

#!/bin/bash
DEVICEID=[YOUR DEVICE ID]
ACCESSTOKEN=[YOUR ACCESS TOKEN]
STRUCTUREID=[YOUR STRUCTURE ID]

case $1 in
"read.humidity")
curl -s -L https://developer-api.nest.com/devices/thermostats/${DEVICEID}/humidity\?auth\=${ACCESSTOKEN}
;;

"read.temp")
curl -s -L https://developer-api.nest.com/devices/thermostats/${DEVICEID}/ambient_temperature_c\?auth\=${ACCESSTOKEN}
;;

"read.targettemp")
curl -s -L https://developer-api.nest.com/devices/thermostats/${DEVICEID}/target_temperature_c\?auth\=${ACCESSTOKEN}
;;

"set.targettemp")
curl -s -L -X PUT "https://developer-api.nest.com/devices/thermostats/${DEVICEID}/target_temperature_c?auth=${ACCESSTOKEN}" -H "Content-Type: application/json" -d "$2"
;;

"set.presence")
if [ "$2" == "away" ];
then
curl -s -L -X PUT "https://developer-api.nest.com/structures/${STRUCTUREID}/away?auth=${ACCESSTOKEN}" -H "Content-Type: application/json" -d '"away"'
fi

if [ "$2" == "home" ];
then
curl -s -L -X PUT "https://developer-api.nest.com/structures/${STRUCTUREID}/away?auth=${ACCESSTOKEN}" -H "Content-Type: application/json" -d '"home"'
fi

if [ -z $2 ];
then
echo "Presence not set! Must be set to home or away."
fi
;;

*)
echo "Parameters missing, usage:"
echo "nest read.temp Reads actual temperature from Nest"
echo "nest read.humidity Reads humidity from Nest"
echo "nest read.targettemp Reads target temperature from Nest"
echo "nest set.targettemp 20 Sets target temperature on Nest to 20"
echo "nest set.presence away Sets presence on Nest to away"
echo "nest set.presence home Sets presence on Nest to home"
;;
esac

DIY – Arduino KAKU 433mhz PIR sensor for PiMatic

The Arduino project below can be used to create a K(lik)A(an)K(lik)U(it) PIR sensor for monitoring presence in PIMatic. This project is using the 433mhz libraries for Arduino (link).

To build this cool Arduino Project you’ll need the following parts:
Arduino (any kind)
PIR Sensor (Ebay)
433 transmitter (Ebay)
Some jumperwires
And the most important… You’ll need a working PIMatic installation with a Homeduino connected!

Arduino Sketch

//--- START ---
#include <RemoteTransmitter.h>
KaKuTransmitter kaKuTransmitter(11);
int statePin = LOW;
volatile byte motion;
void setup()
 {
 attachInterrupt(0, motion_detect, RISING);//Initialize the interrupt pin for PIR Sensore (Arduino digital pin 2)
 }
void loop()
 {
 
 }
void motion_detect()//This function is called whenever presence is detected by the arduino
 {
 kaKuTransmitter.sendSignal('M',25,2,true);//Sends KAKU singal
 }
//---- END ----

If you want to create multiple Arduino KAKU 433mhz PIR sensors you can change the number 25 in to change the ID of the sensor (example below):

kaKuTransmitter.sendSignal('M',25,2,true);
Unit: 25
ID: 26
kaKuTransmitter.sendSignal('M',26,2,true);
Unit: 24
ID: 26
kaKuTransmitter.sendSignal('M',27,2,true);
Unit: 25
ID: 10

When your Arduino KAKU 433mhz PIR sensor is ready you’ll need to create a device in the PIMatic config file. You’ll need to create a new device (example below):

    {
      "id": "433_PIR01",
      "name": "PIR Gang",
      "class": "HomeduinoRFPir",
      "protocols": [
        {
          "name": "pir1",
          "options": {
            "unit": 25,
            "id": 26
          }
        }
      ]
    },

Once created the device you can use it in a rule (example below):

    {
      "id": "beweging-op-trap",
      "name": "Beweging op trap",
      "rule": "if presence of 433_PIR01 is equal to present and it is after 20:00 and before 23:59 or it is after 00:00 and before 02:00 then execute \"/usr/bin/hyperion snake\"",
      "active": true,
      "logging": true
    },