Read state (JSON) from website with multiple values - catch one value and create a sensor

Dear community
I need your assistance. I would like to gather one specific value from a control system (evcc, based on a raspbery). With the url: “http://192.168.xxx.xxx/api/state” the system sends all values a a JSON (I guess):

{“result”:{“auth”:{“vehicles”:{}},“batteryConfigured”:false,“batteryDischargeControl”:false,“batteryMode”:“unknown”,“bufferSoc”:0,“bufferStartSoc”:0,“currency”:“EUR”,“greenShareHome”:1,“greenShareLoadpoints”:1,“gridConfigured”:true,“gridPower”:-6358,“homePower”:229,“loadpoints”:[{“chargeCurrent”:0,“chargeCurrents”:[0,0,0],“chargeDuration”:4440000000000,“chargePower”:0,“chargeRemainingDuration”:0,“chargeTotalImport”:456.809,“chargedEnergy”:5644.310945,“chargerFeatureHeating”:false,

I only need the value of “homePower”, in the example value 229. I tried with:

sensor:

  • platform: rest
    resource: http://ip.jsontest.com
    name: External IP
    value_template: “{{ value_json.homePower }}”

but no success. Could anyon help me how to so this? Maybe with scrape?
Thank you for you feedback!

Regards
Christoph

Absolutely not with scrape.

Please format code properly see here. Because you haven’t done that, I cannot copy/paste your JSON to test it, as the forum inserts “smart quotes” in what it thinks is normal text.

I think you want:

sensor:
  - platform: rest
    resource: http://192.168.xxx.xxx/api/state
    name: External IP
    value_template: "{{ value_json['result']['homePower'] }}"

Also:

Thank you for you assistance. With the code above I was able to read the value of homePower. Unfortunately there is no result reading the value of: vehicleSoc (should have the value “80” in the dump below). I used the code: value_template: “{{ value_json[‘result’][‘vehicleSoc’] }}”

"result":{"auth":{"vehicles":{}},"batteryConfigured":false,"batteryDischargeControl":false,"batteryMode":"unknown","bufferSoc":0,"bufferStartSoc":0,"currency":"EUR","greenShareHome":1,"greenShareLoadpoints":1,"gridConfigured":true,"gridPower":-3387,"homePower":1751,"loadpoints":[{"chargeCurrent":0,"chargeCurrents":[0,0,0],"chargeDuration":22240000000000,"chargePower":0,"chargeRemainingDuration":0,"chargeTotalImport":510.482,"chargedEnergy":27122.28305,"chargerFeatureHeating":false,"chargerFeatureIntegratedDevice":false,"chargerIcon":null,"charging":false,"connected":true,"connectedDuration":0,"disableThreshold":0,"effectiveLimitSoc":90,"effectiveMaxCurrent":16,"effectiveMinCurrent":6,"effectivePlanSoc":0,"effectivePlanTime":"0001-01-01T00:00:00Z","effectivePriority":0,"enableThreshold":0,"enabled":false,"guardAction":"inactive","guardRemaining":0,"limitEnergy":0,"limitSoc":90,"maxCurrent":16,"minCurrent":6,"mode":"off","phaseAction":"inactive","phaseRemaining":0,"phases1p3p":true,"phasesActive":2,"phasesConfigured":3,"phasesEnabled":3,"planEnergy":32.3,"planOverrun":false,"planProjectedStart":"0001-01-01T00:00:00Z","planTime":"2023-12-20T06:00:00Z","priority":0,"pvAction":"inactive","pvRemaining":0,"sessionCo2PerKWh":null,"sessionEnergy":27122.28305,"sessionPrice":null,"sessionPricePerKWh":null,"sessionSolarPercentage":85.9149315251213,"title":"Carport","vehicleClimaterActive":false,"vehicleDetectionActive":false,"vehicleName":"e-Up","vehicleOdometer":3123,"vehicleRange":219,"vehicleSoc":82.78761523948035,"vehicleTargetSoc":0}],"prioritySoc":0,"pv":[{"power":5138}],"pvConfigured":true,"pvEnergy":0,"pvPower":5138,"residualPower":0,"siteTitle":"Wallpoint","smartCostActive":false,"smartCostLimit":0,"smartCostType":null,"sponsor":"me","sponsorTokenExpires":0,"statistics":{"30d":{"avgCo2":0,"avgPrice":0,"chargedKWh":137.97111695166666,"solarPercentage":46.169661810136986},"365d":{"avgCo2":0,"avgPrice":0.08000000000000004,"chargedKWh":482.532929434352,"solarPercentage":26.94204918070882},"total":{"avgCo2":0,"avgPrice":0.08000000000000004,"chargedKWh":482.532929434352,"solarPercentage":26.94204918070882}},"vehicles":{"e-Up":{"title":"VW e-Up","icon":"car","capacity":32.3,"minSoc":20,"limitSoc":80}},"version":"0.124.1 (de1fb86d)"}}

Please could you give one hint?

Paste your JSON (including the missing { at the start) into here:

Then click JSON Viewer, and a “tree” will appear on the RH pane, if the input is valid JSON. You can then find the value you want:

image

So the value you want is:

{{ value_json['result']['loadpoints'][0]['vehicleSoc'] }}

You can also test this out in the Template Editor:

Note that there are no quotes in [0] because that is referencing “the first item” not “the item named ‘0’”.

If I format and cut down the JSON a bit, you can see why you needed a different solution to get the two values:

{
  "result": {
    "homePower": 1751,
    "loadpoints": [
      {
        "vehicleSoc": 82.78761523948035,
      }
    ]
  }
}

homePower is a “direct child” of result, whereas vehicleSoc is within the first (and only) list element of loadpoints.