gboudreau / nest-api

Unofficial Nest Learning Thermostat API
GNU Lesser General Public License v3.0
300 stars 93 forks source link

Is it possible to recieve notification about target temp changes? #128

Open romanstyler opened 4 years ago

romanstyler commented 4 years ago

Hi, I want to start some scene in smart home when user changes target temp. Could you help, does your library allow to detect this moment. Probably some pooling functionality.

As I can see on official web interfate target temperature immediately changes when I spit nest ring.

gboudreau commented 4 years ago

Website probably uses some kind of polling, where the web browser initiates a HTTP request, and the server only answers that after the value requested changes, or a timeout occurs, at which point the browser re-initiate the same HTTP request. The API we use don't support pushing I'd say.

You can simply create a script that checks for the target temp, saves it in a file locally, and schedule that script to run every 5 or 10 minutes. Once the value obtained from the API is different from the one in the file, do whatever you want to trigger, then write the new value in the file.

romanstyler commented 4 years ago

Thank you for quick support! I want to integrate Nest thermostat into Fibaro Smart Home system. Idea: on every change of target temperature -> set this temperature to air conditioner. I quess Google can block me, of I request configuration too often.

This library somehow can react very fast, but this related to only HomeKit users: https://github.com/chrisjshull/homebridge-nest

Anyway thank you for information, I will continue my investigation.

bauzer714 commented 4 years ago

As a data point my scripts run every 5 minutes for several years without being blocked.

It should be noted that you are not guaranteed realtime thermostat data with each script execution. The script can only acquire the latest data nest has. My thermostat provides routine updates ranging 6-13 minutes apart.

romanstyler commented 4 years ago

Thank you. Just checked Network log from Chrome. As I can see, there are subscribe requests. Looks like, this is long pooling. image

I will try to investigate it.

romanstyler commented 4 years ago

Probably I found solution. Thanks to method from your code: ` public function getEnergyLatest($serial_number = NULL) { $serial_number = $this->getDefaultSerial($serial_number);

    $payload = array(
        'objects' => array(
            array('object_key' => "energy_latest.$serial_number")
        )
    );

    $url = '/v5/subscribe';

    return $this->doPOST($url, json_encode($payload));
}`

This allows to subscribe and load latest energy report. I used this code as example. But if I subscribe to "shared" topic like this:

$payload = array( 'objects' => array( array('object_key' => "shared.$serial_number", 'object_timestamp' => 1592332412183, 'object_revision' => 19605) ) );

This request will return latest changes of target temperature and also revision and timestamp variables. Every time I need to pass these variables into request and response will provided only when data is changed. It works almost immediately on target value change.

kueblc commented 4 years ago

You may find wiredprairie/unofficial_nodejs_nest useful, as it implements long polling subscription

romanstyler commented 4 years ago

Thank you very much! But as I can see this library uses nest account. But in my case it's Google Account. Finally, I implemented new method which allows to poll needed data. Current version allows to monitor target temperature and on change control air conditioner via IR command.