0
votes

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
        }``
3
It sounds like we're solving an xy problem here by iterating the list. I think we'll need the use case to fully understand. For example, why are we searching by room id in the first place? If the room ids are guaranteed unique, then why not use them as the keys for the thermostats? If they are not, then searching by room id is not reliable.Kato

3 Answers

2
votes

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.

1
votes

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.

0
votes

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.