Parsing JSON Dictionary from REST request

I’m pulling some data from a REST endpoint and I’m having some issues parsing out the response. The endpoint returns what I would consider a dictionary (key-value) format with an ID as the property name.

I’m using the request to filter the data down to a single day so there should only ever be one property/key on the object. The kicker is I don’t know what the key will be each time I request making it (seemingly) impossible to use JSONPath

{
    "success": true,
    "data": {
        "98765431": {
            "runtime_thermostat_summary_id": 123456,
            "user_id": 123456,
            "thermostat_id": 123456,
            "date": "2022-01-05",
            "count": 270,
            "sum_compressor_cool_1": 0,
            "sum_compressor_cool_2": 0,
            "sum_compressor_heat_1": 21720,
            "sum_compressor_heat_2": 0,
            "sum_auxiliary_heat_1": 0,
            "sum_auxiliary_heat_2": 0,
            "sum_fan": 22080,
            "sum_humidifier": 0,
            "sum_dehumidifier": 0,
            "sum_ventilator": 0,
            "sum_economizer": 0,
            "sum_degree_days": 0,
            "avg_outdoor_temperature": 41.9,
            "avg_outdoor_humidity": 65,
            "min_outdoor_temperature": 34,
            "max_outdoor_temperature": 54.2,
            "avg_indoor_temperature": 69.1,
            "avg_indoor_humidity": 46,
            "deleted": false
        }
    }
}

Ignoring my issue above and considering I haven’t actually tested anything, I’m going for something like this:

    - resource: 'https://api.beestat.io/'
      params:
        api_key: ''
        resource: 'runtime_thermostat_summary'
        method: 'read_id'
        arguments: '{{"{\"attributes\":{\"thermostat_id\":1234,\"date\":{\"value\":\"" + now().strftime("%Y-%m-%d %H:%M:%S") + "\",\"operator\":\"=\"}}}}"}}'
      sensor:
        - name: HVAC Runtime
          json_attributes_path: '$.data[0]'
          json_attributes:
            - 'sum_compressor_heat_1'
            - 'sum_compressor_heat_2'

Sometimes it takes writing something down to figure it out…

    - resource: 'https://api.beestat.io/'
      params:
        api_key: ''
        resource: 'runtime_thermostat_summary'
        method: 'read_id'
        arguments: '{{"{\"attributes\":{\"thermostat_id\":123456,\"date\":{\"value\":\"" + now().strftime("%Y-%m-%d %H:%M:%S") + "\",\"operator\":\"=\"}}}"}}'
      sensor:
        - name: 'HVAC Cooling Stage 1 Runtime'
          value_template: >
            {% for item in value_json.data %}
              {{ value_json.data[item].sum_compressor_cool_1}}
            {% endfor %}
        - name: 'HVAC Heat Stage 1 Runtime'
          value_template: >
            {% for item in value_json.data %}
              {{ value_json.data[item].sum_compressor_heat_1}}
            {% endfor %}
        - name: 'HVAC Aux Stage 1 Runtime'
          value_template: >
            {% for item in value_json.data %}
              {{ value_json.data[item].sum_auxiliary_heat_1}}
            {% endfor %}
        - name: 'HVAC Fan Runtime'
          value_template: >
            {% for item in value_json.data %}
              {{ value_json.data[item].sum_fan}}
            {% endfor %}
        - name: 'HVAC Average Outdoor Temperature'
          value_template: >
            {% for item in value_json.data %}
              {{ value_json.data[item].avg_outdoor_temperature}}
            {% endfor %}

I’m not a big fan of the for loops, they could be very fragile. Any thoughts on improving the value_templates?

The key (“98765431”) is a random value?
Seems unlikely or it’s a very badly designed API :wink: