JSON Parsing Error?

Hi Everyone,

I’ve got the following YAML in my sensor file:

- platform: rest
  name: "Daily Water Draw"
  resource: 'https://summary.ekmmetering.com/summary?key=1234565&meters=132456&format=json&report=dy&limit=1&offset=0&timezone=America~Chicago&fields=Pulse_Cnt_1*'
  method: GET
  unit_of_measurement: "pulses"
  value_template: '{{ value_json.Pulse_Cnt_1_Diff | int }}'
  scan_interval: 900

The idea is it’s supposed to return the value 63, from this JSON output:

[
   {
      "Start_Time_Stamp_UTC_ms": 1646460031227,
      "End_Time_Stamp_UTC_ms": 1646526571221,
      "End_Date": "Sat Mar 05 2022 18:29:31 GMT-0600 (GMT)",
      "Start_Date": "Sat Mar 05 2022 00:00:31 GMT-0600 (GMT)",
      "Meter": 350002412,
      "Protocol": "v4",
      "Count": 1137,
      "rejected_bad": 0,
      "rejected_duplicates": 0,
      "Pulse_Cnt_1_Last": 285975,
      "Pulse_Cnt_1_Average": 285935.65,
      "Pulse_Cnt_1_StdDev": 1.02,
      "Pulse_Cnt_1_DeltaMin": 0,
      "Pulse_Cnt_1_DeltaMax": 3,
      "Pulse_Cnt_1_Min": 285912,
      "Pulse_Cnt_1_Max": 285975,
      "Pulse_Cnt_1_Diff": 63
   }
]

Regrettably, the sensor in HA is giving me State " unknown" error. I was able to use “value_json.Pulse_Cnt_1_Diff” in the Developer Tools to return 63.

My JSON is very bad, Im assuming the error is in my varaible reference. Any thoughts or kind helping hands out there?

You json is enclosed in a list [ ]. You need to select the first element of the list:

value_template: '{{ value_json[0].Pulse_Cnt_1_Diff }}'

Also there is no need for the |int filter as you are not doing any math on the value and the state will be stored as a sting no matter what you do (unless you store it in an attribute).

For future reference you can use this tool to find json paths:

https://jsonpathfinder.com/

Nice - I’ll put that in my bookmarks - sometimes I get confused with json :grinning_face_with_smiling_eyes: