JSONDecodeError with Sensor-Template with speedtest

Hello,
I have the following configuration for a sensor-template in my configuration.yaml:

template:
  - sensor:
      - name: "SpeedTest CLI Ping"
        unique_id: speedtest_cli_ping
        icon: mdi:speedometer
        # TIME_MILLISECONDS: Final = "ms"
        unit_of_measurement: ms
        state_class: measurement
        state: "{{ (states('sensor.speedtest_cli_data') | from_json).ping | round(2) }}"
      - name: "SpeedTest CLI Download"
        unique_id: speedtest_cli_download
        icon: mdi:speedometer
        # DATA_RATE_MEGABITS_PER_SECOND: Final = "Mbit/s"
        unit_of_measurement: Mbit/s
        state_class: measurement
        state: "{{ ((states('sensor.speedtest_cli_data') | from_json).download * 8 / 1000 / 1000) | round(2) }}"
      - name: "SpeedTest CLI Upload"
        unique_id: speedtest_cli_upload
        icon: mdi:speedometer
        unit_of_measurement: Mbit/s
        state_class: measurement
        state: "{{ ((states('sensor.speedtest_cli_data') | from_json).upload * 8 / 1000 / 1000) | round(2) }}"

sensor:
  - platform: command_line
    name: "SpeedTest CLI Data"
    unique_id: speedtest_cli_data
    command: "/config/shell_commands/SpeedTest-TCP --output json"
    scan_interval: 14400
    command_timeout: 60
    value_template: >-
      {{ 
        { 
          "ping": value_json.ping, 
          "download": value_json.download, 
          "upload": value_json.upload
        }
        | to_json 
      }}

And i have the following json:

{"client":{"ip":"123.123.123.123","lat":"123.123","lon":"123.123","isp":"SomeISP"},"servers_online":"10","server":{"name":"Frankfurt","sponsor":"Verizon.com","distance":"123.123","latency":"13","host":"somehost.net:8080"},"ping":"13","jitter":"14","download":"92146173.270521","upload":"32001713.157847","_":"all ok"}

If i look into my entities there are the entities but with “Not available”
In the log i can see following:



Logger: homeassistant.helpers.event
Source: helpers/template.py:569
First occurred: 16:57:37 (4 occurrences)
Last logged: 17:04:36

    Error while processing template: Template<template=({{ (states('sensor.speedtest_cli_data') | from_json).ping | round(2) }}) renders=2>
    Error while processing template: Template<template=({{ ((states('sensor.speedtest_cli_data') | from_json).download * 8 / 1000 / 1000) | round(2) }}) renders=2>
    Error while processing template: Template<template=({{ ((states('sensor.speedtest_cli_data') | from_json).upload * 8 / 1000 / 1000) | round(2) }}) renders=2>
    Error while processing template: Template<template=(mushroom-shape-icon { {% set vol_level = state_attr(config.entity, 'volume_level') | float %} --card-mod-icon: {% if state_attr(config.entity, 'is_volume_muted') == true %} mdi:volume-mute {% else %} {% if vol_level > 0.66 %} mdi:volume-high {% elif vol_level < 0.33 %} mdi:volume-low {% else %} mdi:volume-medium {% endif %} {% endif %}; }) renders=2>

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 567, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2305, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2160, in from_json
    return json_loads(value)
           ^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/json.py", line 43, in json_loads
    return orjson.loads(__obj)  # type:ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
orjson.JSONDecodeError: unexpected character: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 693, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: JSONDecodeError: unexpected character: line 1 column 1 (char 0)

The problem seems like:

JSONDecodeError: unexpected character: line 1 column 1 (char 0)

What is wrong with my json?

1 Like

Take that into the developer - template area and tweak on it until you get what you want. I suspect something like:

from_json).client.upload

But I have not tested that.