Control Nest Thermostat From the Command Line Using Nest API

I have a Nest thermostat, which is a wonderful piece of technology, however that meant that I had to get my phone out of my pocket, unlock it, launch the Nest app, wait for it to connect, and then adjust the temperature.  I wondered if it was possible to control my Nest from the command line on my laptop, which I am always working in anyhow.  The answer is YES, and this is HOW:

 

Step 1 – Create a Nest Developer Product

Nest Developer Product ScreenYou need to create a Nest Developer account and product to get the Nest API access you need to control your thermostat.
Create a Nest Developer account here – https://developer.nest.com/

Then create a Product.  Mine is called “Devons Command Line Interface”.  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 a Nest 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 Command Line Aliases

Now you have everything you need!  I use ZSH, but this should work just the same in BASH or the shell of your choice.  I created four aliases in my .zshrc (use your .bashrc or preferred file).  Each command will need your Access Token, and either your Device ID or your Structure ID.

nestset() {
curl -L -X PUT "https://developer-api.nest.com/devices/thermostats/%YOUR_DEVICE_ID%/target_temperature_f?auth=%YOUR_ACCESS_TOKEN%" -H "Content-Type: application/json" -d "$1"
}

nestget() {
curl -L https://developer-api.nest.com/devices/thermostats/%YOUR_DEVICE_ID%/target_temperature_f\?auth\=%YOUR_ACCESS_TOKEN%
}

nestaway() {
curl -L -X PUT "https://developer-api.nest.com/structures/%YOUR_STRUCTURE_ID%/away?auth=%YOUR_ACCESS_TOKEN%" -H "Content-Type: application/json" -d '"away"'
}

nesthome() {
curl -L -X PUT "https://developer-api.nest.com/structures/%YOUR_STRUCTURE_ID%/away?auth=%YOUR_ACCESS_TOKEN%" -H "Content-Type: application/json" -d '"home"'
}

 

Step 6 – Control Your Nest Thermostat From the Command Line with the Nest API!

Now that you’ve defined those aliases you are ready to use them!

nestget returns the current target temperature your Nest is set to:
[fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][devon:~]$ nestget
71%

nestset takes a single argument which is the new target temperature in Fahrenheit:
[devon:~]$ nestset 72
72%

nestaway sets your Nest into Away mode:
[devon:~]$ nestaway
"away"%

nesthome sets your Nest into Home mode:
[devon:~]$ nesthome
"home"%
[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

 

 

 

Translation into Portuguese for https://www.homeyou.com/~edu/


Posted

in

by

Comments

9 responses to “Control Nest Thermostat From the Command Line Using Nest API”

  1. Mike Avatar
    Mike

    Cool idea, but seems to be failing for me on Step 4 currently, doing a redirect (HTTP 307) to a long firebase URL and then getting no output.

    1. Devon Avatar

      Unfortunately I no longer have a Nest so I cannot re-test this whole process:( Hopefully someone can help!

  2. Wieger van der Burgt Avatar
    Wieger van der Burgt

    @ Mike: i was able to test it and seems to work fine. Maybe check the steps again? anyway: Thanks for the guide Devon!

  3. Iskandar Avatar

    Hey Devon,

    Great stuff! Feels like magic to be able to control my Nest like this.

    I’ve got it all working, but I’m fairly new to this kind of stuff. I’m on a Mac and I can’t seem to find out how to save the lines at Step 5 somewhere, so I don’t have to copy paste this every time I want to use the commands.

    Thanks!

    1. Devon Avatar

      I’m glad it’s working out for you!! You need to edit (or create) a file ~/.bashrc and paste those lines from step 5 in there. Then you can use the aliases in the terminal as shown in step 6.

  4. Joseph LaFerla Avatar
    Joseph LaFerla

    Hi, I just purchased a Nest 3rd gen. Does your approach work with it? Can you perform other Nest functions such as switch to heat/cool, set setpoints etc? I am going to give this a try, it looks very interesting. Thank you.

    1. Devon Avatar

      It *should* work with the Nest 3rd gen, however I no longer have a Nest to test with. The other functions are not currently supported by my simple script, however the API supports those features so you could extend my stuff to handle those actions as well. Without a Nest in my house I won’t be able to make/test those changes myself though.

  5. Joseph LaFerla Avatar
    Joseph LaFerla

    I’m not that good at api scripts. If you wish, you can write the scripts and I will test them on mine and let you know! Contact me directly

  6. RR Avatar
    RR

    Thanks for this – it works great. I was trying to use nest-cli via npm, but it kept getting hung up, but this is much simpler. I just followed this and it works great for communicating with several Nest v3 stats in my home, so I can confirm that it even works when there is more than one device ID in a structure. If you do the query from step 4, you get a huge list of variable-value pairs. You can see how this data changes (after repeated reads) while you make changes on your phone app or the nest website to make changes to your thermostats. The away/home stuff is a little different now with eco mode, and that’s all done under the devices/thermostats/ API. Just use the curl commands to read and write (POST) that are in the nestget and nestset shortcuts, and target hvac_mode, or other settings besides target_temperature_f.

Leave a Reply

Your email address will not be published. Required fields are marked *

PHP Code Snippets Powered By : XYZScripts.com