8

I have a Nest learning thermostat. I think that it tries to "autodetect" when I'm away using motion sensors, or some such. In any case, it's not very accurate, and more importantly, its algorithm is not transparent.

Rather, I think there's a much better way to tell if I'm in the house or not...

The Nest draws an IP address on the same subnet as my phone.

I'd like to:

  • tell Nest the IP address(es) of my phone (and my wife's phone)
  • ask Nest to periodically (maybe every 10 or 15 minutes) ping those IP addresses for a presence test
  • have Nest update it's own home/away accounting mechanism based on those objective tests

Is this possible through the Nest API?

Does this violate any of the Nest API Prohibitions?

Does a client like this already exist?

Dustin Kirkland
  • 5,323
  • 3
  • 36
  • 34
  • 1
    I vaguely remember somebody doing something similar to this based off the ARP requests at the router. IFTT is an option here as well because you can have location generated behaviors sent to the client. It's all quite doable and it's on my long-term to do list... – jfudgeelder Dec 17 '14 at 19:38

3 Answers3

2

Actually, I figured out how to do this exactly, using the Nest API. It works perfectly!

I've created a pair of Python scripts, /usr/bin/nest-home and /usr/bin/nest-away which are part of the uhome package that I've uploaded to Ubuntu. There's also the /usr/bin/uhome script, which handles the first piece of my question -- reading an input list of IP/MAC addresses, iterating through them to see if any are pingable, and based on the results, marking us home or away.

The real magic in talking to the Nest is in 3 subsequent API calls:

  1. Login and establish an auth token
  2. Retrieve the Structure ID
  3. Update the structure's away boolean

Complete script below, or you can find it in Launchpad and Github.

#!/usr/bin/python
#
# nest-home, nest-away
#
# Copyright 2014 Dustin Kirkland <dustin.kirkland@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import requests
import time
import urllib
import urllib2
import sys
import json
import yaml

# login
with open(os.path.expanduser("~") + "/.uhome/nest.yaml", "r") as stream:
    credentials = yaml.load(stream)
#headers = {"user-agent": "Nest/1.1.0.10 CFNetwork/548.0.4", "X-nl-protocol-version": "1"}
headers = {"X-nl-protocol-version": "1"}
response = requests.post("https://home.nest.com/user/login", credentials)
response = json.loads(response.content)
transport_url = response["urls"]["transport_url"]
userid = response["userid"]
headers["Authorization"] = "Basic " + response["access_token"]
headers["X-nl-user-id"] = userid
# get structure id
request = urllib2.Request(transport_url + "/v3/mobile/user." + userid, headers=headers)
response = urllib2.urlopen(request).read()
response = json.loads(response)
structure_id = response["structure"].keys()[0]
# set away, if necessary
currently_away = response["structure"][structure_id]["away"]
if "away" in sys.argv[0] and not currently_away:
    data = '{"away_timestamp":' + str(time.time()) + ',"away":true,"away_setter":0}'
    new_away = "True"
elif not "away" in sys.argv[0] and currently_away:
    data = '{"away_timestamp":' + str(time.time()) + ',"away":false,"away_setter":0}'
    new_away = "False"
else:
    data = ""
    new_away = str(currently_away)
if data:
    structure_url = transport_url + "/v2/put/structure" "." + structure_id
    request = urllib2.Request(structure_url, data, headers)
    try:
        urllib2.urlopen(request).read()
    except urllib2.URLError:
        print "Put operation failed"
    print "Nest updated -->  away=" + new_away
else:
    print "No need to update Nest --> away=" + new_away
Dustin Kirkland
  • 5,323
  • 3
  • 36
  • 34
  • Nice solution! I'm trying to do the same thing as the OP. Fails for me on Python 2.7.9 (OS X). I get `urlopen error [Errno 8] _ssl.c:507: EOF occurred in violation of protocol` on line 40. – richardtallent Jan 11 '15 at 06:23
0

IFTTT can do this for a single person with an android device. You can create a recipe that will set your Nest to home or away when your phone connects or disconnects to your home wifi network.

InsaneOstrich
  • 119
  • 5
  • 13
-1

no.

Home and Away are set via user interaction in the Nest app or on the Nest Thermostat. Clients that set Home or Away must prompt and require a user confirmation for each Home/Away transition.

rod
  • 9
  • 1
    I read that in Nest's pretty ridiculous API and Marketing guidelines. While that's what some legalese says, there's actually a very straightforward technical solution, which I just posted as the answer. Thanks. – Dustin Kirkland Jan 09 '15 at 21:21