简体   繁体   中英

Controlling multiple Nest Thermostats using Firebase

Is there an easy way to discover and differentiate thermostat devicesIDs? I would have expected to be able to find the deviceId somewhere in the device interface or by using the Firebase library, but I haven't found either to be the case.

My Firebase object is created as such (url ending at thermostats):

Firebase fb = new Firebase("https://developer-api.nest.com/devices/thermostats/");

To change a property on the thermostat, my code looks like this...

/*This method does an http get (not firebase) to get the entire thermostats data structure as a JSON Object. It then iterates through each thermostat deviceId structure looking for a matching room name. When it finds a match, it breaks and returns the deviceID as a string. The device id is needed to change all data specific to that thermostat device using firebase. */

deviceId = getDeviceIdFromRoomName(roomName); 

//I then perform an action like so to change the "target_temperature_f" to a new value.

fb.child(deviceId).child(NestConstants.TARGET_TEMP_F).setValue(newValue);

It seems like there should be an easier, more reliable way to do this using the Firebase library, but I haven't been able to find it. Any suggestions?

To help visualize, this is the data structure that I'm parsing and I'm looking for suggestions on a better way to acquire "CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5" (if it exists).

{
    "thermostats": {
        "CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5": {
            "locale": "en-US",
            "temperature_scale": "F",
            "is_using_emergency_heat": false,
            "has_fan": true,
            "software_version": "4.1",
            "has_leaf": true,
            "device_id": "CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5",
            "name": "Master Bedroom",
            "can_heat": true,
            "can_cool": false,
            "hvac_mode": "heat",
            "target_temperature_c": 18.5,
            "target_temperature_f": 66,
            "target_temperature_high_c": 24,
            "target_temperature_high_f": 75,
            "target_temperature_low_c": 20,
            "target_temperature_low_f": 68,
            "ambient_temperature_c": 12,
            "ambient_temperature_f": 54,
            "away_temperature_high_c": 24,
            "away_temperature_high_f": 76,
            "away_temperature_low_c": 12.5,
            "away_temperature_low_f": 55,
            "structure_id": "xF7P6wyrR7-8VHerOAcHYDMBc_odGpO2CIpQO_g5EpM13LINO9Oxdg",
            "fan_timer_active": false,
            "name_long": "Master Bedroom Thermostat",
            "is_online": true
        }``

Disclaimer: I haven't worked with the NEST API. My answer below is based on experience with the regular Firebase API.

From the way it looks thermostats is a Firebase list . In that case you can easily handle all children using something like this:

var thermostats = new Firebase("https://developer-api.nest.com/devices/thermostats/");
thermostats .on('child_added', function(snapshot) {
  var thermostat = snapshot.val();
  alert(thermostat.device_id+' is '+thermostat.name);
});

I'm assuming you're using the JavaScript API here btw.

I'm not sure I'm understanding you correctly here, but have you tried calling the structure ID? When I do that, it lists all thermostats associated with that structure. Once you have all the thermostats, could you call them and store the associated name? If I'm way off, let me know and I'll try to look at it again.

Greg,

The structure contains a list of associated device ids. I suggest to start listening on structures if discoverability is your intent. You need to do that only once as the device ids don't change. Once you traverse structures and get your desired device ids you may never need to perform that step ever again.

Lev.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM