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