pythonjsonflaskhoneywell

need to access nested values in json.load


I am using Hufman's Honeywell Python code to pull data from the MyTotalConnectComfort website. Code is here. I have modified the code for 2.7. I am using Flask as well.

When i make the call to client.locations() i get back a json file that has 6 keys. I pare down the data inside the json to get just the data behind the single key i want which is 'Devices'. I use the following code to do this:

Honeywelldata = client.locations()
newlist2 = []
dada = {}
for keyl, value in Honeywelldata[0].items():
    if keyl == "Devices":
        dada[keyl] = value
        print "dadakey", dada[keyl]
newlist2.append(dada)
print "newlist2 keys", newlist2[0].keys()
print "newlist2", json.dumps(newlist2, sort_keys=True,indent=4,separators=(',', ': '))

I get back the data and formatted list at the bottom of this thread. I am trying to be able to pass this json to an html and then use the following html code to post values from this list. I have tried many things and I cannot get to the child values of 'Devices'.

{{ honeywell.Devices.ThermostatData.Outdoorhumidity | safe }}

I keep getting this error UndefinedError: 'list object' has no attribute 'Devices'

Do i need to re-key this list or beside being a newbie what am i doing wrong. Thanks.

Here is the newlist2 keys & data (jsonlint.com shows this as a valid json)

newlist2 keys [u'Devices']
[{
"Devices": [{
    "AlertSettings": null,
    "DehumidifierData": null,
    "DemandResponseData": null,
    "DeviceID": 1111111,
    "DeviceType": 48,
    "FanData": null,
    "HasAlertSettings": false,
    "HasDehumidifier": false,
    "HasFan": false,
    "HasHumidifier": false,
    "HasUIData": true,
    "HoldUntilCapable": true,
    "HumidifierData": null,
    "IsAlive": true,
    "IsUpgrading": false,
    "LocationID": 1111111,
    "MacID": "000000000000",
    "Name": "THERMOSTAT",
    "Schedule": null,
    "ScheduleCapable": true,
    "ThermostatData": {
        "AllowedModes": [
            3,
            1,
            2
        ],
        "CoolRate": null,
        "CoolSetpoint": null,
        "Deadband": 0.0,
        "DisplayUnits": 1,
        "EquipmentOutputStatus": null,
        "HeatRate": null,
        "HeatSetpoint": null,
        "IndoorHumidity": 39.0,
        "IndoorHumidityStatus": 0,
        "IndoorTemperature": 74.0,
        "IndoorTemperatureStatus": 0,
        "IsCommercial": false,
        "IsInVacationHoldMode": false,
        "MaxCoolSetpoint": 99.0,
        "MaxHeatSetpoint": 90.0,
        "MinCoolSetpoint": 50.0,
        "MinHeatSetpoint": 40.0,
        "Mode": null,
        "NextTime": null,
        "OutdoorHumidity": 13.0,
        "OutdoorHumidityAvailable": true,
        "OutdoorHumidityStatus": 0,
        "OutdoorTemperature": 103.0,
        "OutdoorTemperatureAvailable": true,
        "OutdoorTemperatureStatus": 0,
        "ScheduleCoolSp": 75.0,
        "ScheduleHeatSp": 62.0,
        "Status": null,
        "VacationHold": null,
        "VacationHoldCancelable": true
    }
}]

}]


Solution

  • In the following expression (I make the assumption it is a Django template fragment and-or a Jinja2 expression:

    {{ honeywell.Devices.ThermostatData.Outdoorhumidity | safe }}
    

    The variable honeywell appears to be a list. You can try:

    {{ honeywell[0].Devices[0].ThermostatData.Outdoorhumidity | safe }}
    

    To get the first element of this list.