Author Topic: Nest Thermostat Python script  (Read 7492 times)

0 Members and 1 Guest are viewing this topic.

Soda97

  • Contributor
  • ***
  • Posts: 69
  • Karma: 1
    • View Profile
Nest Thermostat Python script
« on: January 11, 2015, 01:39:05 PM »
There's a python script for Nest Thermostat here (https://github.com/smbaker/pynest/blob/master/nest.py) and I'm not sure can it adapt with Voxcommando.

I run the Python script in windows like this, python nest.py --user user email --password password curtemp

And I get something like 60.

Can I get this same value with the script in VoxCommando?

Sorry I dont know much about coding.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Nest Thermostat Python script
« Reply #1 on: January 11, 2015, 02:12:40 PM »
If you can actually do that on the Windows command line then yes you can adapt this to VC using the Launch.Capture action.

We have tried to adapt this python code to the VC python plugin but for some reason this code is not compatible with iron python.  One theory is that iron python has trouble with https (secure) web pages.

We are currently working on a solution using only VC actions (scrape, regex, maps etc.) and we are making excellent progress, so just hang on for a few days and we should have something that you can use to query or set the temperature, home/away mode, and fan mode.  :D

The solution also involves some updates to the main VC program so it will be necessary to install the latest VC version when we release it.

Keep an eye on our twitter feed, or Google plus community to know when the new version is available.

Soda97

  • Contributor
  • ***
  • Posts: 69
  • Karma: 1
    • View Profile
Re: Nest Thermostat Python script
« Reply #2 on: January 11, 2015, 02:29:36 PM »
Excellent!  Thank you all for the great work!  Can't wait to see what you guys can do!

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Nest Thermostat Python script
« Reply #3 on: January 11, 2015, 04:36:55 PM »
If you can actually do that on the Windows command line then yes you can adapt this to VC using the Launch.Capture action.

We have tried to adapt this python code to the VC python plugin but for some reason this code is not compatible with iron python.  One theory is that iron python has trouble with https (secure) web pages.

We are currently working on a solution using only VC actions (scrape, regex, maps etc.) and we are making excellent progress, so just hang on for a few days and we should have something that you can use to query or set the temperature, home/away mode, and fan mode.  :D

The solution also involves some updates to the main VC program so it will be necessary to install the latest VC version when we release it.

Keep an eye on our twitter feed, or Google plus community to know when the new version is available.

Great news ... I hope it will include support for nest protect  8) 8) (on side topic nest bought revolv which was a hub supporting most of the common protocols Zigbee,z-wav...etc. so it seems big change to home automation is coming )

at some point I was working to create a python to read nest info ...
here is my progress if it will give any help (code written in pythonista, but I think it will run in VC PY)
I was trying to understand the data structure ... I am not sure if there is way to receive a notification from nest in VC

Code: [Select]
import urllib

import urllib2

import sys

 

try:

   import json

except ImportError:

   try:

       import simplejson as json

   except ImportError:

       print "No json library available. I recommend installing either python-json"

       print "or simpejson."

       sys.exit(-1)

 

class Nest:

    def __init__(self, username, password, serial=None, index=0, units="C"):

        self.username = username

        self.password = password

        self.serial = serial

        self.units = units

        self.index = index

 

    def loads(self, res):

        if hasattr(json, "loads"):

            res = json.loads(res)

        else:

            res = json.read(res)

        return res

 

    def login(self):

        data = urllib.urlencode({"username": self.username, "password": self.password})

 

        req = urllib2.Request("https://home.nest.com/user/login",

                              data,

                              {"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4"})

 

        res = urllib2.urlopen(req).read()

 

        res = self.loads(res)

 

        self.transport_url = res["urls"]["transport_url"]

        self.access_token = res["access_token"]

        self.userid = res["userid"]   

##        print 'login succeeded'

 

    def get_status(self):

        req = urllib2.Request(self.transport_url + "/v2/mobile/user." + self.userid,

                              headers={"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4",

                                       "Authorization":"Basic " + self.access_token,

                                       "X-nl-user-id": self.userid,

                                       "X-nl-protocol-version": "1"})

 

        res = urllib2.urlopen(req).read()

##        print res

        print

        res = self.loads(res)

        print res

        print '-------------------- root keys -------------------------'

        print res.keys()

       

       

        self.user_alert_dialog_id = res["user_alert_dialog"].keys()[0]

        self.user_alert_dialog_id_version = res["user_alert_dialog"][self.user_alert_dialog_id]['$version']

        self.user_alert_dialog_id_timestamp = res["user_alert_dialog"][self.user_alert_dialog_id]['$timestamp']

       

        print

        print 'user_alert_dialog: keys ', res["user_alert_dialog"].keys()

        print self.user_alert_dialog_id,': keys ',res["user_alert_dialog"][self.user_alert_dialog_id].keys()

        print 'version: ', self.user_alert_dialog_id_version,', timestamp: ', self.user_alert_dialog_id_timestamp

        print

       

       

        self.user_settings_id = res["user_settings"].keys()[0]

        ##self.user_settings_version = res["user_settings"][self.user_settings_id]['$version']

        ##self.user_settings_timestamp = res["user_settings"][self.user_settings_id]['$timestamp']

       

        print

        print 'user_settings: keys ',res["user_settings"].keys()

        print self.user_settings_id,': keys ',res["user_settings"][self.user_settings_id].keys()

        ##print 'version: ' + str (self.user_settings_version),  'timestamp: ' + str (self.user_settings_timestamp)

        print

       

       

        self.user_id = res["user"].keys()[0]

        self.user_id_structure_memberships = res["user"][self.user_id]['structure_memberships']

        self.user_id_name = res["user"][self.user_id]['name']

        self.user_id_version = res["user"][self.user_id]['$version']

        self.user_id_structures = res["user"][self.user_id]['structures']

        ##self.user_settings_timestamp = res["user_settings"][self.user_settings_id]['$timestamp']

       

        print

        print 'user: keys ', res["user"].keys()

        print self.user_id,': keys ',res["user"][self.user_id].keys()

        print 'struc memb: ', self.user_id_structure_memberships

        print 'name: ', self.user_id_name

        print 'version: ', self.user_id_version

        print 'structures: ', self.user_id_structures

        print

       

       

        self.widget_track_id = res["widget_track"].keys()[0]

        self.widget_track_id_version = res["widget_track"][self.widget_track_id]['$version']

        self.widget_track_id_last_ip = res["widget_track"][self.widget_track_id]['last_ip']

        self.widget_track_id_last_connection = res["widget_track"][self.widget_track_id]['last_connection']

        self.widget_track_id_timestamp = res["widget_track"][self.widget_track_id]['$timestamp']

        self.widget_track_id_online = res["widget_track"][self.widget_track_id]['online']

       

        print

        print 'widget_track: keys ', res["widget_track"].keys()

        print 'widget_track_id: keys ', res["widget_track"][self.widget_track_id].keys()

        print 'version: ', self.widget_track_id_version,', last ip: ', self.widget_track_id_last_ip, ' last connection: ', self.widget_track_id_last_connection, ' timestamp: ', self.widget_track_id_timestamp

        print 'online: ', self.widget_track_id_online

        print

       

        self.message_center_id = res["message_center"].keys()[0]

       

        print 'message center: keys ', self.message_center_id

        print res["message_center"][self.message_center_id].keys()

        messages = res["message_center"][self.message_center_id]["messages"]

        print 'there are '+ str(len(messages) )

        print

        print 'first message:    ', messages[0]

        print

       

        sys.exit()

       

       

 

 

 

        self.structure_id = res["structure"].keys()[0]

        print 'structure: ' + self.structure_id

        print res["structure"][self.structure_id].keys()

        print res["structure"][self.structure_id]["measurement_scale"][0]

        print

        self.occupancy_id = res["occupancy"].keys()[0]

        print 'occupancy: ' + self.occupancy_id

        print res["occupancy"][self.occupancy_id].keys()

        ##print '     '+ res["occupancy"][self.occupancy_id]["occupancy"][0]

 

        self.topaz_id = res["topaz"].keys()[0]

        print 'topaz: ' + self.topaz_id

        print res["topaz"][self.topaz_id].keys()

        ##self.topaz_smoke_status = res["topaz"].keys()[3]

        ##self.topaz_smoke_status = res["topaz"].keys()[]

       

        print

        self.utility_id = res["utility"].keys()[0]

        print 'utility: ' + self.utility_id

        print res["utility"][self.utility_id].keys()

        print

       

       

       

 

##        if (self.serial is None):

##            self.device_id = res["structure"][self.structure_id]["devices"][self.index]

##            self.serial = self.device_id.split(".")[1]

 

##        self.status = res

 

 

    def temp_in(self, temp):

        if (self.units == "F"):

            return (temp - 32.0) / 1.8

        else:

            return temp

 

    def temp_out(self, temp):

        if (self.units == "F"):

            return temp*1.8 + 32.0

        else:

            return temp

 

    def show_status(self):

        shared = self.status["shared"][self.serial]

        device = self.status["device"][self.serial]

 

        allvars = shared

        allvars.update(device)

 

        for k in sorted(allvars.keys()):

             print k + "."*(32-len(k)) + ":", allvars[k]

 

    def show_curtemp(self):

        temp = self.status["shared"][self.serial]["current_temperature"]

        temp = self.temp_out(temp)

 

        print "%0.1f" % temp

 

    def set_temperature(self, temp):

        temp = self.temp_in(temp)

 

        data = '{"target_change_pending":true,"target_temperature":' + '%0.1f' % temp + '}'

        req = urllib2.Request(self.transport_url + "/v2/put/shared." + self.serial,

                              data,

                              {"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4",

                               "Authorization":"Basic " + self.access_token,

                               "X-nl-protocol-version": "1"})

 

        res = urllib2.urlopen(req).read()

 

        print res

 

    def set_fan(self, state):

        data = '{"fan_mode":"' + str(state) + '"}'

        req = urllib2.Request(self.transport_url + "/v2/put/device." + self.serial,

                              data,

                              {"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4",

                               "Authorization":"Basic " + self.access_token,

                               "X-nl-protocol-version": "1"})

 

        res = urllib2.urlopen(req).read()

 

        print res

 

 

n = Nest('em@il', 'password')

n.login()

n.get_status()





When Voice command gets tough, use hand gestures

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Nest Thermostat Python script
« Reply #4 on: January 11, 2015, 04:46:25 PM »
Right. I think you originally linked to that Python code when you first asked about Nest integration, no?

Simon whatever-his-nameis's Python code works fine in a native Python environment.  We've had no problems getting his queries to work. The problem is integrating it into IronPython so that VC can actively interact with it.

 I think you'll find that problematic. At least, I did. When you try to use that code in IronPython you'll find that Nest closes the connection on you.

But all he's doing is sending post and get requests to the Nest using Python. As you know, we can already do that in VC. So that's the solution we've gone with. It works fine.

... Getting Nest to independently send notifications to VC is a different animal. Maybe if they have an email alert system or some kind of push notification that could be used. Buddy's Python script doesn't address anything like that -- it's all query-based.
« Last Edit: January 11, 2015, 04:49:50 PM by nime5ter »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Nest Thermostat Python script
« Reply #5 on: January 11, 2015, 04:52:24 PM »
Actually, you could use IFTTT to send Nest notifications to VC: https://ifttt.com/p/nest/shared
« Last Edit: March 13, 2017, 10:18:53 AM by nime5ter »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Nest Thermostat Python script
« Reply #6 on: January 11, 2015, 05:13:11 PM »
on side topic nest bought revolv which was a hub supporting most of the common protocols Zigbee,z-wav...etc. so it seems big change to home automation is coming )

It seems that every month there are 4 or 5 new products released for controlling your home based on Z-wave or Zigbee.  The month when no new devices are released THAT will be a big change.  Based on what I've seen of Nest's api and data structure, I would steer clear of any of their new home automation offerings for the time being.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Nest Thermostat Python script
« Reply #7 on: January 12, 2015, 01:09:46 AM »
Right. I think you originally linked to that Python code when you first asked about Nest integration, no?

Simon whatever-his-nameis's Python code works fine in a native Python environment.  We've had no problems getting his queries to work. The problem is integrating it into IronPython so that VC can actively interact with it.

 I think you'll find that problematic. At least, I did. When you try to use that code in IronPython you'll find that Nest closes the connection on you.

But all he's doing is sending post and get requests to the Nest using Python. As you know, we can already do that in VC. So that's the solution we've gone with. It works fine.

... Getting Nest to independently send notifications to VC is a different animal. Maybe if they have an email alert system or some kind of push notification that could be used. Buddy's Python script doesn't address anything like that -- it's all query-based.
True I linked to it somewhere on the forums a while ago... As well all nest Python script link only with the Thermostate which I do not have yet ... But I have nest protect and I found one script in PHP that deals with nest protect... And that is the reason I started to work on Python to integrate nest protect

Actually, you could use IFTTT to send Nest notificaitons to VC: https://ifttt.com/p/nest/shared
That is what I am using now... IFTTT create txt files in a folder that VC watch... The txt file has a trigger event and payload ... The unconvinience remains with IFTTT is that it is sometimes late in running it is actions

The script I posted was a try to understand how nest data is organized, as apart from setting or retrieving values, there is messages as well

If you ever need testing with nest protect I can do that ...

It seems that every month there are 4 or 5 new products released for controlling your home based on Z-wave or Zigbee.  The month when no new devices are released THAT will be a big change.  Based on what I've seen of Nest's api and data structure, I would steer clear of any of their new home automation offerings for the time being.

Totally agree, and am afraid I made the wrong choice to go with insteon.. For now I am putting all investment in HA on hold till things clear out ... I think next 2 years will witness a big leap ... Everything is getting smart, oral b launched tooth brush with blue tooth to give feedback on how we brush!!!! Wemo is lunching smart slow cooker with wifi !!!!
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Nest Thermostat Python script
« Reply #8 on: January 17, 2015, 01:22:39 PM »
We are currently working on a solution using only VC actions (scrape, regex, maps etc.) and we are making excellent progress, so just hang on for a few days and we should have something that you can use to query or set the temperature, home/away mode, and fan mode.  :D

The solution also involves some updates to the main VC program so it will be necessary to install the latest VC version when we release it.

Ta da

http://voxcommando.com/forum/index.php?topic=1965.0