I have setup MQTT sensor, it is working but i get lots of errors.
Error look like this:
2024-08-22 15:12:51.756 WARNING (MainThread) [homeassistant.components.mqtt.mixins] Erroneous JSON:
2024-08-22 15:12:52.053 WARNING (MainThread) [homeassistant.components.mqtt.mixins] Erroneous JSON:
2024-08-22 15:12:52.054 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'AA' is undefined (value: {"bus":"0","type":"rx","ts":14552,"frame":[{"id":2029,"dlc":8,"rtr":false,"extd":false,"data":[4,98,116,72,4,170,170,170]}]}, template: {% if value_json.frame[0].id == 1999 %}
{% set PID = value_json.frame[0].data[0:5] %}
{% if PID == [16,9,98,29,218] %}
{% set AA = value_json.frame[0].data[6] %}
{% endif %} {% endif %} {% if AA | round(0) == 20 %}
in
{% else %}
out
{% endif %})
Setup for sensors are like this
- name: e-Up_charging_status
unique_id: eup_charging_status
icon: mdi:ev-plug-type2
state_topic: "wican/e-up/can/rx"
payload_on: "C"
payload_off: "B"
value_template: >-
{% if value_json.frame[0].id == 2029 %}
{% set PID = value_json.frame[0].data[0:4] %}
{% if PID == [4,98,116,72] %}
{% set AA = value_json.frame[0].data[4] %}
{% endif %}
{% endif %}
{% if AA | round(0) == 0 %}
B
{% elif AA | round(0) == 4 %}
C
{% endif %}
- name: "e-Up!_odo1"
unique_id: "eup_odo1"
state_topic: "wican/e-up/can/rx"
unit_of_measurement: "km"
state_class: "measurement"
value_template: >-
{% if value_json.frame[0].id == 2029 %}
{% set PID = value_json.frame[0].data[0:5] %}
{% if PID == [16,13,98,2,189] %}
{% set AA = value_json.frame[0].data[6] %}
{% set BB = value_json.frame[0].data[7] %}
{{ (AA*65536) + (BB*256) }}
{% endif %}
{% endif %}
json_attributes_topic: "wican/e-up/can/rx"
json_attributes_template: >-
{% if value_json.frame[0].id == 2029 %}
{% set PID = value_json.frame[0].data[0:5] %}
{% if PID == [16,13,98,2,189] %}
{% set time = value_json.ts %}
{ "ts": {{ time }} }
{% endif %}
{% endif %}
Same topic is receiving lots of JSON messages, that have slightly different id
and data
values.
Here is example of one message
{
"bus": "0",
"type": "rx",
"ts": 39329,
"frame": [
{
"id": 2029,
"dlc": 8,
"rtr": false,
"extd": false,
"data": [
4,
98,
116,
72,
4,
170,
170,
170
]
}
]
}
I need for my odo1 sensor, value from ts
to confirm odo2 sensor (not shown) uses right message (compare timestamps).
I get these errors for each new message puplished in that topic.
Erroneous JSON:
error is caused by { "ts": {{ time }} }
line in odo1 json_attribute_template
but i dont know what is wrong whit it, attribute shows correctly in sensor in UI.
How i can get these error corrected?