Json parsing help?

I have an endpoint for a smart battery capacity tester device.

Currently it’s outputting the values of the 4 tester slots for 1 unit. I have some control over the endpoint but the code is VERY simplified and just using some array → json conversion. Below is an example of the json data.

[{"CDModuleViewID":"741","CDUnitID":"245","CDModuleID":"1096","CycleState":"7","CycleTime":"6492","BatteryBarcode":"B01987","RestartCycle":"0","MilliOhms":"145","Capacity":"1869","BatteryInitialTemp":"0","BatteryHighestTemp":"0","BatteryCurrentTemp":"0","DischargeAmps":"0","BatteryInitialVoltage":"4.14","BatteryCurrentVoltage":"4.15","BatteryFaultCodeID":"0","LastCommunicationTime":"2022-02-17 14:22:41"},{"CDModuleViewID":"742","CDUnitID":"245","CDModuleID":"1097","CycleState":"7","CycleTime":"6475","BatteryBarcode":"B01985","RestartCycle":"0","MilliOhms":"131","Capacity":"1933","BatteryInitialTemp":"0","BatteryHighestTemp":"0","BatteryCurrentTemp":"0","DischargeAmps":"0","BatteryInitialVoltage":"4.14","BatteryCurrentVoltage":"4.14","BatteryFaultCodeID":"0","LastCommunicationTime":"2022-02-17 14:22:41"},{"CDModuleViewID":"743","CDUnitID":"245","CDModuleID":"1098","CycleState":"7","CycleTime":"7774","BatteryBarcode":"B01991","RestartCycle":"0","MilliOhms":"263","Capacity":"2081","BatteryInitialTemp":"20","BatteryHighestTemp":"26","BatteryCurrentTemp":"16","DischargeAmps":"0","BatteryInitialVoltage":"4.16","BatteryCurrentVoltage":"4.13","BatteryFaultCodeID":"0","LastCommunicationTime":"2022-02-17 14:22:41"},{"CDModuleViewID":"744","CDUnitID":"245","CDModuleID":"1099","CycleState":"7","CycleTime":"6642","BatteryBarcode":"B01989","RestartCycle":"0","MilliOhms":"142","Capacity":"2054","BatteryInitialTemp":"18","BatteryHighestTemp":"25","BatteryCurrentTemp":"17","DischargeAmps":"0","BatteryInitialVoltage":"4.16","BatteryCurrentVoltage":"4.12","BatteryFaultCodeID":"0","LastCommunicationTime":"2022-02-17 14:22:41"}]

What I am trying to do is create 1 “device” for each CDUnitID (in this case it’s 245, the only tester reporting) so I could do 1 device per endpoint or if i could get multiple devices to create if i have more units, i could do that. But for now, lets keep it simple. It’s 1 unit. There are 4 slots on unit 1. I would like to have the various slots be attributes on said device.

So my device → attributes structure would look like.

Unit1

  • Slot1 State = the value of cycle state for the json row with CDModuleID 1096 or [0] in the elements.
  • Slot2 State = the value of cycle state for the json row with CDModuleID 1097 or [1] in the elements.
  • Slot3 State = the value of cycle state for the json row with CDModuleID 1098 or [2] in the elements.
  • Slot4 State = the value of cycle state for the json row with CDModuleID 1099 or [3] in the elements.

I have tried to create a rest sensor. Below is my failed attempt.

rest:
  - resource: http://<removed>/
    sensor:
      - name: "Tester 1"
        json_attributes_path: "$"
        value_template: "OK"
        json_attributes:
          - "CycleState"
          - "BatteryBarcode"
          

But I get a very odd error in the log.

2022-01-23 05:17:21 ERROR (MainThread) [homeassistant.components.rest.data] Error fetching data: failed with

no idea what comes after “with” because there isn’t anything.

If i exec into the homeassistant container and run curl it returns data.

Any suggestions? Thanks!

rest:
  - resource: http://<removed>/
    sensor:
      - name: "Tester 1"
        json_attributes_path: "$[0]" # <- path changed to this
        value_template: "OK"  # Why do you not put a template for a state here?
        json_attributes:
          - "CycleState"
          - "BatteryBarcode"

This may assist you in future:

https://jsonpathfinder.com/

I have multiple slots I need to get the values from. Was trying to keep it all as 1 device with attributes but I can do 16 individual devices in the end if I have to go that route.

You can have a state for the entity as well as attributes, at the moment you just have “OK” as the state whereas you could have the most important attribute showing as the state. Also with the new rest integration you can make as many sensors as you want from the one call to the rest resource.

e.g.

rest:
  - resource: http://<removed>/
    sensor:
      - name: "Tester 1"
        json_attributes_path: "$[0]"
        value_template: "{{ vlaue_json[0].CycleState }}"
        json_attributes:
          - "BatteryBarcode"

      - name: "Tester 2"
        json_attributes_path: "$[1]"
        value_template: "{{ vlaue_json[1].CycleState }}"
        json_attributes:
          - "BatteryBarcode"

Attributes can be a pain to use in certain places later on (e.g. history graphs don’t support attributes). So having your most used variable as the state can have an advantage.

1 Like

Interesting! Any thoughts on how to get more info on that error that just ends as “with “ but no error message lol

Raise the logging level to DEBUG. Don’t leave it there. It’s very chatty.

Ok. i upped the logging and found for some reason it was timing out. But i set a much longer timeout. So now it’s connecting. I tried your example with the multiple sensors. That seems to be working, but i ran into an issue on the json_attributes. Seems “Tester 2” in your example, the batterybarcode value is the same for both. Can I specify the [0] style element selector in the json attributes?

ex:

- name: "Tester 1 (Slot 2)"
        json_attributes_path: "$[0]"
        value_template: "{{ value_json[1].CycleState }}"
        json_attributes:
          - "{{ value_json[1].BatteryBarcode }}"
          - "CycleTime"
          - "MilliOhms"
          - "Capacity"

EDIT: I’m dumb. I didn’t update the json_attributes_path

- name: "Tester 1 (Slot 2)"
        json_attributes_path: "$[1]"
        value_template: "{{ value_json[1].CycleState }}"
        json_attributes:
          - "BatteryBarcode"
          - "CycleTime"
          - "MilliOhms"
          - "Capacity"

This is now working

This will help me keep better tabs on testing all these batteries for capacity/resistance ect






Grat that you got it working but… the way those batteries are stored :scream: