Assistance in parsing JSON

Hi! I am pretty new to HA and tried to query my Fan Control Unit for hours without success. Therefore I kindly ask for your assistance.

The unit returns something like the following when accessing http://ipadress/data/live:

[{"hours":16,"minutes":4,"day":10,"month":12,"year":2023,"temp_in":20.56,"temp_out":19.50,"hum_in":34.00,"hum_out":34.00,"abs_hum_in":6.07,"abs_hum_out":5.71,"on":0,"operating_hours":0,"message":air not dry enough}]

Firefox means it’s not a valid JSON string:

SyntaxError: JSON.parse: unexpected character at line 1 column 199 of the JSON data

I see 2 things: the square brackets at the beginning/end and the spaces in the last value (message).
However I cannot get it running.
Either no sensor is getting created when adding it to the yaml or the state stays at “unknown”.

I tried the following (and various similar ones):

  - scan_interval: 30
    resource: http://ipaddress/data/live
    sensor:
     -  name: "temperature outside"
        value_template: "{{ value_json['temp_out'] }}"
        unit_of_measurement: '°C'

Probably an easy one for a more experienced user :slight_smile:
Thank you!

The root of the JSON is an array, so try

value_template: "{{ value_json[0]['temp_out'] }}"

Note however the the JSON you posted is invalid.
“message:” value needs double quotes

"message": "air not dry enough"

Hi and thanks for the reply.

I have added the [0] as you suggested but the sensor’s state is still unknown even after restarting HA.

value_template: "{{ value_json[0]['temp_out'] }}"`

I understand your explanation why the JSON is invalid, however I cannot change the device’s output so I just hoped I can use the other values at least. I was able to successfully add other device’s APIs with the same lines, however it does not work for this device :confused:

Thanks and best regards

Try this version.

value_template: "{{ value | regex_findall(find='temp_out\":(\d+.\d+)') | first | default('unknown') }}"

It handles the received data as a raw string, as opposed to JSON, and uses a regex pattern to get the temperature value.

Screenshot from Template Editor

Hi!
Thanks for that hint to regex, however the file editor check says: unknown escape sequence.

I am now considering to build a regex that just extracts me the values from the first colon to the next comma for hours, from the second colon to the next comma for minutes and so on, so I should be able to get these values.
Wish me luck :smiley:

Best regards

What “file editor check” are you referring to? If possible,
post a screenshot of it.

You can see from the screenshot I posted above, from the Template Editor, that there are no complaints and regex_findall is successfully able to use the supplied regex pattern to get the value of temp_out.

Hi, thanks for your assistance.

After wrestling with Regex I ended up with these patterns which are fine for me, example for numeric values:

value_template: >
          {{ value | regex_findall_index(find='\"operating_hours\":(\d+)', index=0, ignorecase=False) }}
        unit_of_measurement: 'h'

For the text:

value_template: >
          {{ value | regex_findall_index(find='"message":\s*([^,}]+)', index=0, ignorecase=False) }}

Best regards