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:
sensor:

  • platform: rest
    name: “Heatpump Data”
    resource: http://192.168.1.193/json
    json_attributes_path: “$.heatpump”
    json_attributes:

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

    pump_flow:
    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):

rest:
  - resource: http://192.168.1.193/json
    scan_interval: 60
    binary_sensor:
      - name: "Heatpump State"
        value_template: >
          {{ value_json['heatpump']
             |selectattr('Name','eq','Heatpump_State')
             |map(attribute='Value')|first == '1' }}
        device_class: running
    sensor:
      - name: "Force DHW State"
        value_template: >
          {{ value_json['heatpump']
             |selectattr('Name','eq','Force_DHW_State')
             |map(attribute='Value')|first }}
      - name: "Pump Flow"
        value_template: >
          {{ value_json['heatpump']
             |selectattr('Name','eq','Pump_Flow')
             |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