Need help to translate this json

Am I wrong or is homeassistant not able to translate this type of json response:

"{\"DeviceResponse\":[{\"DeviceID\":\"

Cheers

Could you please provide a bit more context? That sample snippet is clearly not valid JSON.

1 Like

it’s not json, it starts with a " its a string. Most likely there isn’t valid json later on in the response, but you only posted the first 50 characters. If that’s the entire response, then it’s definitely not valid for many reasons.

You can always use this website to check if json is valid before event attempting to use it in home assistant.

Also, WTH is not for support. Moving this to configuration.

That looks like a string that has encapsulated JSON within it. Fairly common in web responses but certainly not ideal. You’ll need to take the string pass it into a JSON parser.

This is the whole response:

"{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"10/1/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"982\",\"ActualVolumePercent\":\"54\",\"Battery\":\"2.9\",\"Temperature\":\"68\",\"MessData\":\"96\"}]}"

I’m trying to get the data out of this with the following yaml:

    name: 'proteus level sensor'
    value_template: "OK"
    json_attributes_path: "$.deviceresponse"
    json_attributes:
      - DeviceID
      - DeviceName
      - Medium
      - LastUpdate
      - MaxVolume
      - ActualVolume
      - ActualVolumePercent
      - Battery
      - Temperature
    scan_interval: 120

Getting this error:

Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:170
Integration: RESTful (documentation, issues)
First occurred: 16:54:14 (1 occurrences)
Last logged: 16:54:14
JSON result was not a dictionary or list with 0th element a dictionary

because your response starts with “”, that’s not a dictionary its’ a string. Fix the response or use commandline to remove the first and last quote.

thx for your help! the response is generated from the sensor api. so no idea how or if i can fix this.

any hint on how to use commandline to remove the quotes?

didn’t made any progress as of today.
Logger says:
Data fetched from resource:

"{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/11/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1697\",\"ActualVolumePercent\":\"91\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"27\"}]}"

yaml looks like this:

  - platform: rest
    resource: http://aes-gate-ecofrog.azurewebsites.net/Api/FuncDevice
    method: POST
    headers:
      Content-Type: application/json
    payload: '{DeviceRequest: {"Username": "xyz", "Email": "xyz", "SecurityStamp": "xyz", "DeviceID": "xyz"}}'
    name: 'proteus level sensor'
    value_template: '{{ value_json }}'

And State in HA:

Can anyone help me with the correct value template to get the sensor states properly.

Put this in configuration.yaml. Do not put it in the sensor section.

rest:
  - resource: http://aes-gate-ecofrog.azurewebsites.net/Api/FuncDevice
    method: POST
    headers:
      Content-Type: application/json
    payload: '{DeviceRequest: {"Username": "xyz", "Email": "xyz", "SecurityStamp": "xyz", "DeviceID": "xyz"}}'
    sensor:
    - name: Heizung Max Volume
      device_class: volume
      unit_of_measurement: ftÂł
      value_template: >
        {% set name = 'Heizung' %}
        {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %}
        {{ device.get('MaxVolume') }}

    - name: Heizung Actual Volume
      device_class: volume
      unit_of_measurement: ftÂł
      value_template: >
        {% set name = 'Heizung' %}
        {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %}
        {{ device.get('ActualVolume') }}

    - name: Heizung Actual Volume Percent
      unit_of_measurement: %
      value_template: >
        {% set name = 'Heizung' %}
        {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %}
        {{ device.get('ActualVolumePercent') }}

    - name: Heizung Battery
      device_class: battery
      value_template: >
        {% set name = 'Heizung' %}
        {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %}
        {{ device.get('Battery') }}

    - name: Heizung Temperature
      device_class: temperature
      unit_of_measurement: °F
      value_template: >
        {% set name = 'Heizung' %}
        {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %}
        {{ device.get('Temperature') }}

    - name: Heizung Mess Data
      value_template: >
        {% set name = 'Heizung' %}
        {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %}
        {{ device.get('MessData') }}

Thank you very much for your help!!!
Sadly there’s still something wrong :roll_eyes:

Logger says:

2022-11-12 20:56:09.128 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: "{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/12/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1683\",\"ActualVolumePercent\":\"90\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"28\"}]}"
2022-11-12 20:56:09.128 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'DeviceResponse' when rendering '{% set name = 'Heizung' %} {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %} {{ device.get('MaxVolume') }}'
2022-11-12 20:56:09.130 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: "{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/12/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1683\",\"ActualVolumePercent\":\"90\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"28\"}]}"
2022-11-12 20:56:09.131 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'DeviceResponse' when rendering '{% set name = 'Heizung' %} {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %} {{ device.get('ActualVolume') }}'
2022-11-12 20:56:09.134 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: "{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/12/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1683\",\"ActualVolumePercent\":\"90\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"28\"}]}"
2022-11-12 20:56:09.134 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'DeviceResponse' when rendering '{% set name = 'Heizung' %} {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %} {{ device.get('ActualVolumePercent') }}'
2022-11-12 20:56:09.136 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: "{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/12/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1683\",\"ActualVolumePercent\":\"90\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"28\"}]}"
2022-11-12 20:56:09.137 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'DeviceResponse' when rendering '{% set name = 'Heizung' %} {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %} {{ device.get('Battery') }}'
2022-11-12 20:56:09.139 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: "{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/12/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1683\",\"ActualVolumePercent\":\"90\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"28\"}]}"
2022-11-12 20:56:09.139 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'DeviceResponse' when rendering '{% set name = 'Heizung' %} {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %} {{ device.get('Temperature') }}'
2022-11-12 20:56:09.141 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: "{\"DeviceResponse\":[{\"DeviceID\":\"5167877\",\"DeviceName\":\"Heizung\",\"Medium\":\"Ă–l\",\"LastUpdate\":\"11/12/2022 8:00:02 AM\",\"MaxVolume\":\"2000\",\"ActualVolume\":\"1683\",\"ActualVolumePercent\":\"90\",\"Battery\":\"2.9\",\"Temperature\":\"69\",\"MessData\":\"28\"}]}"
2022-11-12 20:56:09.142 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'str object' has no attribute 'DeviceResponse' when rendering '{% set name = 'Heizung' %} {% set device = value_json.DeviceResponse | select('DeviceName', 'eq', name) | list | first | default({}) %} {{ device.get('MessData') }}'

This goes back to my original response, you need to fix the incoming data. It’s not json.

Custom integration from alexiosc:
https://github.com/alexiosc/ecofrog