JSON extract help

i’ve a JSON extract from a heatpump URL that gives me the below (truncated extract)

“heatpump”: [
“Topic”: “TOP0”,
“Name”: “Heatpump_State”,
“Value”: “1”,
“Description”: “On”
“Topic”: “TOP1”,
“Name”: “Pump_Flow”,
“Value”: “0.13”,
“Description”: “l/min”
“Topic”: “TOP2”,
“Name”: “Force_DHW_State”,
“Value”: “0”,
“Description”: “Disabled”

I’ve got so far in my code:

  • platform: rest
    name: “Heatpump Data”
    json_attributes_path: “$.heatpump”

    • TOP1
    • TOP2
      scan_interval: 60
      value_template: “{{ value_json.heatpump[0].Value }}”
  • platform: template
    friendly_name: “Force DHW State”
    value_template: “{{ state_attr(‘sensor.heatpump_data’, ‘2’)[‘Value’] }}”

    friendly_name: “Pump Flow”
    value_template: “{{ state_attr(‘sensor.heatpump_data’, ‘1’)[‘Value’] }}”

but I get no values returned, what am I missing, any pointers appreciated, thx.

Please format your code correctly, using the </> button. With it formatted like that, I cannot copy-paste it for experimentation without fixing it first.

Your REST sensor json_attributes setup is wrong, because heatpump is a list not a dict. Even if that had worked, your (legacy-format) template sensors are looking for non-existent attributes called '1' and '2'.

As a better alternative to a REST sensor and a pair of template sensors, try this RESTful integration setup, under rest: not under sensor: (and you’ll need a full restart if this is your first rest: entry):

  - resource:
    scan_interval: 60
      - name: "Heatpump State"
        value_template: >
          {{ value_json['heatpump']
             |map(attribute='Value')|first == '1' }}
        device_class: running
      - name: "Force DHW State"
        value_template: >
          {{ value_json['heatpump']
             |map(attribute='Value')|first }}
      - name: "Pump Flow"
        value_template: >
          {{ value_json['heatpump']
             |map(attribute='Value')|first }}
        unit_of_measurement: "L/min"
        device_class: volume_flow_rate

Consider whether the “Force DHW State” should also be a binary sensor. I don’t know the details of what it represents to be able to make that judgement, although I did for the Status one…

Testing those templates after cleaning up your unformatted code:

{% set value_json = {"heatpump":
                     [ {"Topic": "TOP0","Name": "Heatpump_State","Value": "1","Description": "On"},
                       {"Topic": "TOP1","Name": "Pump_Flow","Value": "0.13","Description": "l/min"},
                       {"Topic": "TOP2","Name": "Force_DHW_State","Value": "0","Description": "Disabled"},
                     ] } %}

Force DHW state: {{ value_json['heatpump']|selectattr('Name','eq','Force_DHW_State')|map(attribute='Value')|first }}
Pump Flow:       {{ value_json['heatpump']|selectattr('Name','eq','Pump_Flow')|map(attribute='Value')|first }}
Status:          {{ value_json['heatpump']|selectattr('Name','eq','Heatpump_State')|map(attribute='Value')|first == '1' }}

Dear Troon, thank you so much for your help with this, and it works perfectly, and also a helpful lesson for the future.

1 Like