REST sensor and JSON parsing

Hi, I’m trying to port a JSON API from my jeedom to HA. I have the following in my configurayion.yaml

  - resource: https://www.apicall2.com/api/measures/total?authToken=AAA&deviceId=BBB&measureType=one_phase
    scan_interval: 300
    sensor:
      - name: "MyLight_energy_state"
        value_template: "{{ value_json.status }}"
      - name: "MyLight_energy"
        value_template: "{{ value_json.measure.values.0 | selectattr('type', 'equalto', 'energy') | map(attribute='value') | first }} }}"
        unit_of_measurement: "Ws"

this api call return the following JSON

{
    "status": "ok",
    "measure": {
        "values": [
            {
                "type": "energy",
                "value": 7.950845176516003E10,
                "unit": "Ws"
            },
            {
                "type": "produced_energy",
                "value": 4.063679999352002E10,
                "unit": "Ws"
            },
            {
                "type": "electricity_meter_energy",
                "value": 3.887165177164002E10,
                "unit": "Ws"
            },
            {
                "type": "green_energy",
                "value": 2.6588956860719963E10,
                "unit": "Ws"
            },
            {
                "type": "grid_energy",
                "value": 5.291949490443997E10,
                "unit": "Ws"
            },
            {
                "type": "msb_charge",
                "value": 1.0328050313999996E10,
                "unit": "Ws"
            },
            {
                "type": "msb_discharge",
                "value": 1.0328040039599997E10,
                "unit": "Ws"
            },
            {
                "type": "msb_loss",
                "value": 0.0,
                "unit": "Ws"
            },
            {
                "type": "positive_index",
                "value": 0.0,
                "unit": "kWh"
            },
            {
                "type": "negative_index",
                "value": 0.0,
                "unit": "kWh"
            },
            {
                "type": "grid_sans_msb_energy",
                "value": 4.259145486483997E10,
                "unit": "Ws"
            },
            {
                "type": "autonomy_rate",
                "value": 46.431537881481795,
                "unit": "%"
            },
            {
                "type": "available_power_rate",
                "value": 0.0,
                "unit": "%"
            },
            {
                "type": "self_conso",
                "value": 100.0,
                "unit": "%"
            }
        ]
    }
}```

I easily retreive the status thanks to

 - name: "MyLight_energy_state"
        value_template: "{{ value_json.status }}"

but I struggle to get the energy value.

I have been trying to use JSON Path finder (https://jsonpathfinder.com/)

but none of the bellow seems to return any value

value_template:"{{ value_json.measure.values.0.value}}"

or as proposed by JSON Path Finder 

value_template:"{{ value_json.measure.values[0].value}}"
or
value_template:"{{ value_json.measure.values.[0].value}}"

or, as proposed by ChatGPT

value_template: "{{ value_json.measure.values.0 | selectattr('type', 'equalto', 'energy') | map(attribute='value') | first }} }}"
        unit_of_measurement: "Ws"


I’m running out of idea

Question 2 : as I’m changing my configurayion.yaml, I need to restart HA to test. Is there an easier way to test my code without having to retart HA all the time

Any help would be very welcome

Fred

Try this:

value_template:"{{ value_json['measure']['values'][0]['value'] }}"

or this:

value_template: "{{ value_json['measure']['values'][0] | selectattr('type', 'equalto', 'energy') | map(attribute='value') | first }} }}"

“values” is a key word and my be confusing things.

Give your sensor a unique_id if it is a supported option (in this case it is). You should then be able to use the Developer Tools → YAML → reload (integration type). You still need to restart the first time you use any new integration though.

1 Like

Then you did not supply the correct data above for us to work with:

It definitely contains values as part of the path.

1 Like

Sorry, but either what you posted above is incorrect or the template actually isn’t working.

If your data truly is:

{
    "status": "ok",
    "measure": {
        "values": [
            {
                "type": "energy",
                "value": 7.950845176516003E10,
                "unit": "Ws"
            },
            ...

The only template that would work would be this to get the first item.

{{ value_json.measure['values'][0] }}

The reason you can’t use

is because values is a reserved property on dictionaries, which forces you to use ['values'] to get an attribute with that name.

I’m removing the solution check mark until you can prove that your data matches the outcome or that the template works.

If you altered your resource to change the data format (possible depending on the API), then update your post and the solution checkbox will be allowed again.

A simple way to prove this would be to make a rest command and run the action in the developer tools → actions. Run the command and post the output.

rest_command:
  get_meter_values:
    url: https://www.apicall2.com/api/measures/total?authToken=AAA&deviceId=BBB&measureType=one_phase

Then in the action caller, call rest_command.get_meter_values and your response should appear at the bottom with the format that rest see’s it in.

You’re right. I’ve been messing so much with the code that indeed the proper solution is the one proposed by @tom_I . I’ll delete my last post