Template value should be a string for dictionary value error

I have added the following sensor to my configuration yaml which works as expected. Unfortunately Home Assistant shows an error in my configuration settings.

    - name: "Kitchen Occupancy Sensor"
      state_topic: "tele/occupancy/downstairs/kitchen/SENSOR"
      value_template: "{{ value_json.Time }}"
      json_attributes_topic: "tele/occupancy/downstairs/kitchen/SENSOR"
      json_attributes_template:
        LD2410_Distance_1: "{{ value_json.LD2410.Distance[0] | default('N/A') }}"
        LD2410_Distance_2: "{{ value_json.LD2410.Distance[1] | default('N/A') }}"
        LD2410_Distance_3: "{{ value_json.LD2410.Distance[2] | default('N/A') }}"
        LD2410_Energy_1: "{{ value_json.LD2410.Energy[0] | default('N/A') }}"
        LD2410_Energy_2: "{{ value_json.LD2410.Energy[1] | default('N/A') }}"

I have set up an ESP32 Tasmota device to use a LD2410 mmwave occupancy sensor which saves 3 and 2 values which are comma delimited as follows;

 {"Time":"2024-01-30T22:33:36","LD2410":{"Distance":[83.0,104.0,94.0],"Energy":[51,100]}}

I get Error: template value should be a string for dictionary value @ data[ā€˜json_attributes_templateā€™].

Using ChatGPT as a guide I have tried every way I can to eliminate the error including putting the code in the ā€˜developer tools template checkerā€™ and that shows the values as expected.
I also tried

- state_topic: "tele/occupancy/downstairs/kitchen/SENSOR"
  name: "Kitchen Occupancy Sensor"
  json_attributes_topic: "tele/occupancy/downstairs/kitchen/SENSOR"
  value_template: "{{ value_json.Time }}"
  attribute_templates:
    LD2410_Distance_1: "{{ value_json.LD2410.Distance[0] }}"
    LD2410_Distance_2: "{{ value_json.LD2410.Distance[1] }}"
    LD2410_Distance_3: "{{ value_json.LD2410.Distance[2] }}"
    LD2410_Energy: "{{ value_json.LD2410.Energy }}"

This gave a different error of ā€œextra keys not allowed @ data[ā€˜attribute_templatesā€™].ā€

Both sets of code work but show errors. Is this a bug or my error as the code seems to work as expected?
Thanks

As the error states, json_attributes_template expects a string (in this case a json string)ā€¦ you are giving it a YAML dictionary.

That is generally a bad ideaā€¦ the overwhelming majority of the source data for LLMs about HA configuration is either out of date or from people who have posted configurations that donā€™t work to forums like this oneā€¦

The Template tool only ā€œunderstandsā€ templating, it does not know if your YAML is valid at any level.

- name: "Kitchen Occupancy Sensor"
  state_topic: "tele/occupancy/downstairs/kitchen/SENSOR"
  value_template: "{{ value_json.Time }}"
  json_attributes_topic: "tele/occupancy/downstairs/kitchen/SENSOR"
  json_attributes_template: |
    {% set val = value_json.LD2410 %}
    {{ { "LD2410_Distance_1": val.Distance[0],
      "LD2410_Distance_2": val.Distance[1],
      "LD2410_Distance_3": val.Distance[2],
      "LD2410_Energy": val.Energy } | to_json }}

Thank you for your help. This now fixes the error. I also did ask ChatGPT to explain the syntax which now has helped me understand the answer and hopefully stop taking up time of helpful people such as yourselfā€¦ Its a real shame there is so much poor data regarding the problem in ChatGPT.
Just for completeness this was the response

This YAML statement appears to be configuring a json_attributes_template in a YAML file, likely for a home automation system or a similar application. The template seems to be written in Jinja2, a templating language commonly used in configuration files.

Here's a breakdown of the YAML statement:

yaml

json_attributes_template: |
  {% set val = value_json.LD2410 %}
  {{ { 
    "LD2410_Distance_1": val.Distance[0],
    "LD2410_Distance_2": val.Distance[1],
    "LD2410_Distance_3": val.Distance[2],
    "LD2410_Energy": val.Energy 
  } | tojson }}

    json_attributes_template: This specifies a template for generating JSON attributes.

    |: The pipe symbol (|) indicates that a block scalar follows, allowing the multiline representation of the template.

    {% set val = value_json.LD2410 %}: This line sets a variable val to the value of value_json.LD2410. It seems like value_json is a JSON object, and the template is extracting the LD2410 property from it.

    {{ ... }}: This is Jinja2 syntax for rendering expressions. The content inside the double curly braces is an expression that will be evaluated.

    {"LD2410_Distance_1": val.Distance[0], ...}: This part creates a dictionary with keys corresponding to different distance properties (LD2410_Distance_1, LD2410_Distance_2, LD2410_Distance_3, LD2410_Energy) and values extracted from the val variable.

    | tojson: The entire dictionary is then converted to a JSON string using the tojson filter.

In summary, this YAML statement defines a template for generating JSON attributes. It extracts specific values from a JSON object (value_json.LD2410) using Jinja2 syntax and constructs a new JSON object with a subset of these values. This resulting JSON object is then converted to a JSON string.

Thanks again

Hi,
Do you have solutions? I also got stuck with this.

We have solutions if you explain the problem. Show your code (formatted properly), the MQTT topic content, and explain what you are trying to do.

1 Like

Dear Troon,
Many thanks for your willingness.
My problems is that:
I try to connect LD2410 to tasmota and itā€™s done, have values as belows (form console window)

05:57:30.087 MQT: tele/tasmota_ir/SENSOR = {"Time":"2024-07-31T05:57:30","LD2410":{"Distance":[523.0,534.0,500.0],"Energy":[20,100]}}

But I am unable to get information from tasmota to Home Assisstant, I tried to create sensors like this but failed:

sensor:
  - platform: mqtt
    name: "LD2410 Distance"
    state_topic: "tele/tasmota_ir/SENSOR"
    value_template: "{{ value_json.LD2410.Distance[0] }}"
    unit_of_measurement: "cm"

  - platform: mqtt
    name: "LD2410 Energy"
    state_topic: "tele/tasmota_ir/SENSOR"
    value_template: "{{ value_json.LD2410.Energy[0] }}"
    unit_of_measurement: "unit"

HA does not create new sensors as expected.
Can you have a look on this and figure out whatā€™s wrong?
Thank you so much
Minh Chau

That is an old config. See the current documentation:

mqtt: top level.

This is working with my sensor:

mqtt:

  sensor:
    - name: "LD2410 Moving Distance"
      state_topic: "tele/tasmota_ir/SENSOR"
      value_template: "{{ value_json.LD2410.Distance[0] }}"
      unit_of_measurement: "cm"
    - name: "LD2410 Static Distance"
      state_topic: "tele/tasmota_ir/SENSOR"
      value_template: "{{ value_json.LD2410.Distance[1] }}"
      unit_of_measurement: "cm"
    - name: "LD2410 Detected Distance"
      state_topic: "tele/tasmota_ir/SENSOR"
      value_template: "{{ value_json.LD2410.Distance[2] }}"
      unit_of_measurement: "cm"
    - name: "LD2410 Energy Motion"
      state_topic: "tele/tasmota_ir/SENSOR"
      value_template: "{{ value_json.LD2410.Energy[0] }}"
      unit_of_measurement: "unit"
    - name: "LD2410 Energy Still"
      state_topic: "tele/tasmota_ir/SENSOR"
      value_template: "{{ value_json.LD2410.Energy[1] }}"
      unit_of_measurement: "unit"

Thank you!
I switched to Esphome