api script to get a Nest Google account working in Domoticz
based on : https://github.com/gboudreau/nest-api
Version info:
scripts are located (in my case) in /home/pi/nest-api/\ If you use a different path, you should change it a some places, also every time you pull a new version.
This script needs php, so be shure it's installed:
to install, download the zip, or better use git: if you dan't have git, install it first:
copy nest.auth.php_example to nest.auth.php \ and change the issue-token and cookie in nest.auth.php \ to get both values take these steps:
READ THESE STEPS CAREFULLY !
Open a Chrome browser tab in Incognito Mode (or clear your cache).
Open Developer Tools (View/Developer/Developer Tools).
Click on Network tab. Make sure Preserve Log is checked.
In the Filter box, enter: issueToken
Go to https://home.nest.com, and click Sign in with Google.
A network call (beginning with iframerpc?action=issueToken) will appear in the Dev Tools window. Click on it.
In the Headers tab, under General, copy the entire Request URL. (beginning with "https://accounts.google.com", ending with "=true"). This is your $issue_token.
In the Headers tab, under Request Headers (be shure it's the request header not the other ones!!), copy the entire cookie value (include the whole string which is several lines long and has many field/value pairs - do not include the Cookie: prefix). This is your $cookies; make sure all of it is on one single line.
IMPORTANT: select de values with your mouse starting at the begin of the code to the end of the code, then click richt, and select "Copy". \ DO NOT SELECT "Copy-Value", and DO NOT selecting the string bij double-click: these both won't work!!! \ FINALY, Be shure the cookie and token values are placed between ' ' and the row ends with a ;
Create in Domoticz virtual Devices:
or if you want the combined virtual device for Tem and Hum :
for TempHum : Temp+Hum THGN122/123/132
for SetPoint : Thermostat SetPoint # I Named this device Nest, To acticate it easy from my iPhone with Siri
for ECOMode : a regular Light/Switch and change the icon tosomething nice
for AwayMode : a regular Light/Switch and change the icon tosomething nice
for Heat : a regular Light/Switch and change the icon tosomething nice
move file nest_devices.cfg-example to nest_devices.cfg note the idx nrs, as find in the devices tab, and change them below in the nest_devices.cfg file
Example: SETPOINT 492 <<< change this as example for the SETPOINT to your idx
remove or place a # for lines you don't want to use
edit in Domoticz the ECO Mode switch:
edit in Domoticz the Away Mode switch:
In the Domoticz event gui, create a new event->dzvents->device \ cut-and-paste the content of the example file dzVents_script_Nest_Setpoint.txt in it. \ My setpoint device is called "Nest", so you perhaps you need to change it. \ Better don't use spaces in the device names if you call them from dzvents event's. I had a couple of times problems with this, removing the spaces solved it.
In the Domoticz config add 127.0.0.* and your ip (or range) to local networks.
Add the following cron-entry, to get every 5 minutes the last values from Google: (crontab -e)
Or if you want a log-file:
Of course you can do it every minute, but I don't know if Google has limitation's how much call's per hour are allowed, 5 minutes is save I think, also Domoticz stores it's data every 5 minutes, so it only effects the user interface. \ I you are using logging for a longer time you need to avoid the log-file became to big, by activate log-rotating:
In case you do a 2nd schedule somwhere else, like in the Nest itself, they can conflict with each other when running exactly on the same time. \ This will result as a 5 minutes toggle of values or states, you can simple solve it to add a little delay in the cron:
if Domoticz is running on another server or is using a different port, add this to nest_devices.cfg:
In Domoticz settings, be shure the api is working, example in "Local Networks (no username/password):":
This scipt is using cachefiles e.g. /tmp/nest_phpxxxxxx, if you test this script with the root user, and Domoticz runs as user pi, don't forget to remove them after testing. (rm -rf /tmp/nest_php*)
Some people want a different location, you can change the location of these cachefiles in the php-config:
If you use Domoticz also as Alarm-System, you can add the IDX of the Alarm-Switch to the config file. If this is present, this script will enable ECO and AWAY mode if the Alarm is on, and switch them off if the Alarm changes to off. My alarm works automatically: if no mobile-phone is at home, the Alarm system will be activated, en the Nest goes to ECO and AWAY. Because kids always forget to lower the thermostat when they leave. (me too)
======= Problem Solving ======
Problems to get it working? Try the debug option, you can also run the some stuff manually:
Get the values from nest via the Google-api:
Update a value of a virtual device via the Domoticz-api, say your heat-device is 487:
If you get this error: \ PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Response to login request doesn't contain required access token. Response: {"error":"USER_LOGGED_OUT","detail":"No active session found."}' in /home/pi/nest-api/nest.class.php:1100
you have to regenerate the cookie and token again, see steps above, for some reason the token and cookie stopped working after running fine for over 6 months in my case. This can be caused if your api wasn't running for a while, or if Google restarts the api, the token and cookie will be timed-out.
General tip for debugging: if you get a lots of errors, always look at the first one! (The rest is in most cases caused by this, and not important)
succes, Roland@Breedveld.net