0

Using Google Apps script to attempt to talk to my thermostat using the Ecobee API. In the below code, the value of json is "{ "status": { "code": 0, "message": "" } }". For the correct, full response, see the results of curl later in the question.

For some reason response is only getting the final element (status) out of the three results that are returned by the server (page, thermostatList, and status).

What am I missing?

Google Apps Script is as follows:

function getSettings() {
  var response = fetchSettings();
  var json = response.getContentText();
  var data = JSON.parse(json);
  // response, json, and data only show `status` result
}

function fetchSettings() {
  try {
    var headers = {
      'Authorization': 'Bearer AUTH_TOKEN'
    };
    var payload = { 
      'selection' : { 
        'selectionType':'registered',
        'selectionMatch': '',
        'includeRuntime': 'true'
      } 
    };

    var options = {
      'method': 'get',
      'headers': headers,
      'contentType': 'text/json',
      'payload': JSON.stringify(payload);
    };
    var URL = 'https://api.ecobee.com/1/thermostat?format=json'
    return UrlFetchApp.fetch(URL,options);
  } catch(e) {
    return e;
  }
}

Using curl as follows:

curl -s -H 'Content-Type: text/json' -H 'Authorization: Bearer AUTH_TOKEN' 'https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"",includeRuntime":true\}\}'

I get full results.

{
  "page": {
    "page": 1,
    "totalPages": 1,
    "pageSize": 2,
    "total": 2
  },
  "thermostatList": [
    {
      "identifier": "12345",
      "name": "Downstairs",
      "thermostatRev": "160528163253",
      "isRegistered": true,
      "modelNumber": "athenaSmart",
      "brand": "ecobee",
      "features": "",
      "lastModified": "2016-05-28 16:32:53",
      "thermostatTime": "2016-05-28 11:57:00",
      "utcTime": "2016-05-28 16:57:00",
      "runtime": {
        "runtimeRev": "160528165546",
        "connected": true,
        "firstConnected": "2015-08-20 21:57:53",
        "connectDateTime": "2016-05-26 08:56:18",
        "disconnectDateTime": "2016-05-26 00:00:00",
        "lastModified": "2016-05-28 16:55:46",
        "lastStatusModified": "2016-05-28 16:55:46",
        "runtimeDate": "2016-05-28",
        "runtimeInterval": 200,
        "actualTemperature": 703,
        "actualHumidity": 49,
        "desiredHeat": 670,
        "desiredCool": 810,
        "desiredHumidity": 44,
        "desiredDehumidity": 56,
        "desiredFanMode": "on"
      }
    },
    {
      "identifier": "310166836750",
      "name": "Upstairs",
      "thermostatRev": "160528162656",
      "isRegistered": true,
      "modelNumber": "athenaSmart",
      "brand": "ecobee",
      "features": "",
      "lastModified": "2016-05-28 16:26:56",
      "thermostatTime": "2016-05-28 11:57:00",
      "utcTime": "2016-05-28 16:57:00",
      "runtime": {
        "runtimeRev": "160528165523",
        "connected": true,
        "firstConnected": "2015-09-28 13:33:41",
        "connectDateTime": "2016-05-26 08:55:35",
        "disconnectDateTime": "2016-05-26 00:00:00",
        "lastModified": "2016-05-28 16:55:23",
        "lastStatusModified": "2016-05-28 16:55:23",
        "runtimeDate": "2016-05-28",
        "runtimeInterval": 201,
        "actualTemperature": 712,
        "actualHumidity": 49,
        "desiredHeat": 600,
        "desiredCool": 840,
        "desiredHumidity": 36,
        "desiredDehumidity": 60,
        "desiredFanMode": "auto"
      }
    }
  ],
  "status": {
    "code": 0,
    "message": ""
  }
}
Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

1 Answers1

2

I think the problem is that UrlFetchApp doesn't support sending payload using the get method as a query string. I assume this is correct design for UrlFetchApp and poor design on the part of the Ecobee API. I made a kluge workaround by sending the JSON in a query string myself as follows.

var URL = 'https://api.ecobee.com/1/thermostat?body='+encodeURIComponent(JSON.stringify(payload));
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246