MQTT Sensor from JSON Array

Hi,

i have a mqtt json array with topic

v1/devices/me/telemetry

wich holds Data from BMS System - i want to use
array looks

{
	"cell16.Type": 101,
	"cell16.Voltage": 3.226,
	"cell16.Temperature": 9.1,
	"cell16.lvp": 0,
	"cell16.ovp": 0,
	"cell16.bal": 0
}

the cell Number is from 1 to 16 within 1 or 2 seconds
i have a mqtt tool wich automaticall expand json array - there i get all separated Entries
in single topic but i can´t forward to home assitant

Template Editor shows the right value 3.166

{% set new_test = {"cell16.Type":101, "cell16.Voltage":3.166, "cell16.Temperature":8.4, "cell16.lvp":0, "cell16.ovp":0, "cell16.bal":0} %}

{{new_test["cell16.Voltage"]}}

if failed to extract this value from json

mqtt sensor:
- name: ecs_cell1_v
    unique_id: ecs_cell1_v
    state_topic: v1/devices/me/telemetry
    value_template: "{{ value_json_cell16.Voltage }}"

i´ve already tried a lot of things and spent a lot of hours for solution

any help on this?
thank you

try:

value_template: "{{ value_json.cell16.Voltage }}"

dot instead of underscore between “json”
and “cell”

no - won´t work - had that before
result unkown

Hmm…
value_template: "{{ value_json['cell16.Voltage'] }}"
?

thank you - works
i get crazy i thought i test all possibilities

additional questing

i didn´t get values from other cells aka cell1 through 15
cell16 is the last entry of that topic
it cycle through the entries very fast
the history of mqtt explores show the other values

i tested a little bit

when i listen within Home Assitant to that topic i get the following expanded json for 16 cells
here only 2 to shorten

Message 169 received on v1/devices/me/telemetry at 11:23:
{
    "cell16.Type": 101,
    "cell16.Voltage": 3.268,
    "cell16.Temperature": 10.1,
    "cell16.lvp": 0,
    "cell16.ovp": 0,
    "cell16.bal": 0
}
QoS: 0 - Retain: false
Message 168 received on v1/devices/me/telemetry at 11:23:
{
    "cell15.Type": 101,
    "cell15.Voltage": 3.266,
    "cell15.Temperature": 11.9,
    "cell15.lvp": 0,
    "cell15.ovp": 0,
    "cell15.bal": 0
}
QoS: 0 - Retain: false
Message 167 received on v1/devices/me/telemetry at 11:23:

but when i use the following code for extracting

 - name: ecs_cell14_v
    unique_id: ecs_cell14_v
    state_topic: v1/devices/me/telemetry
    value_template: "{{ value_json['cell14.Voltage'] }}"    
  - name: ecs_cell15_v
    unique_id: ecs_cell15_v
    state_topic: v1/devices/me/telemetry
    value_template: "{{ value_json['cell15.Voltage'] }}"
  - name: ecs_cell16_v
    unique_id: ecs_cell16_v
    state_topic: v1/devices/me/telemetry
    value_template: "{{ value_json['cell16.Voltage'] }}"

i only get the last cell16 Value the other values are empty

any hints?

the tool you mentioned in the initial message, can it provide the information for each cell in a different topic for HA? like
v1/devices/me/telemetry/cell1
v1/devices/me/telemetry/cell2

v1/devices/me/telemetry/cell16

unfortunately no
but it should be possible to extract but i didn´t find the key

for testing i increase the delay time between the 16 Entries
i can see the incoming values for each cell but with the next cell the before cell will be “”

@chabro Which tool are you using to expand the array?

template:
  trigger:
    - platform: mqtt
      topic: v1/devices/me/telemetry
  sensor:
    - name: ecs_cell_1
      state: "{{ json_value['cell1.Voltage'] if json_value['cell1.Voltage'] is defined else states('sensor.ecs_cell_1') }}"
      device_class: voltage
      state_class: measurement
      unique_id: ecs-bat-a4af5a96-02f6-418b-8e33-a80e7c7242b0
    - name: ecs_cell_2
      state: "{{ json_value['cell2.Voltage'] if json_value['cell2.Voltage'] is defined else states('sensor.ecs_cell_2') }}"
      device_class: voltage
      state_class: measurement
      unique_id: ecs-bat-54fa7a4f-c7d6-47fc-80bb-0922fdbf8e97

repeat for all 16

sorry for the late answer didn´t see your answer earlier

wont´t work - i get unavailable

error log

Logger: homeassistant.helpers.sensor
Source: helpers/trigger_template_entity.py:208
First occurred: 18:10:34 (272 occurrences)
Last logged: 18:18:32

  • Error rendering state template for sensor.ecs_cell_1_voltage: UndefinedError: ‘json_value’ is undefined
  • Error rendering state template for sensor.ecs_cell_2_voltage: UndefinedError: ‘json_value’ is undefined

here output from “expanding tool”

thanks

tool is maybe the wrong word
i have home automation system loxone and for this software enhancement loxberry
within loxberry there ist a mqtt plugin for access mqtt topic within loxone
in that plugin i can choose expand json wich shows me all expanded topics automatically without anything else todo

Sorry it’s value_json not json_value

thank you for the quick answer

still unavailable

  • Error rendering state template for sensor.ecs_cell_1: UndefinedError: ‘value_json’ is undefined
  • Error rendering state template for sensor.ecs_cell_2: UndefinedError: ‘value_json’ is undefined

Please post the full sensor code so we can see if there is anything else that needs fixing.

template:
  trigger:
    - platform: mqtt
      topic: v1/devices/me/telemetry
  sensor:
    - name: ecs_cell_1
      state: "{{ value_json['cell1.Voltage'] if value_json['cell1.Voltage'] is defined else states('sensor.ecs_cell_1') }}"
      device_class: voltage
      state_class: measurement
      unique_id: ecs-bat-a4af5a96-02f6-418b-8e33-a80e7c7242b0
    - name: ecs_cell_2
      state: "{{ value_json['cell2.Voltage'] if value_json['cell2.Voltage'] is defined else states('sensor.ecs_cell_2') }}"
      device_class: voltage
      state_class: measurement
      unique_id: ecs-bat-54fa7a4f-c7d6-47fc-80bb-0922fdbf8e97

i´ve made copy / paste from your code

OK, we are really going to need a raw MQTT packet, because the error value_json is undefined, suggests that the data that was received was not JSON - ie it could not be decoded to a JSON object.

I don’t have any experience with “the expanding tool” I don’t know what that is, but it looks like the path is actually v1/devices/me/telemetry/cell1 or possibly v1_devices_me_telemetry_cell1 :man_shrugging:

EDIT:
If you use a tool like MQTT Explorer - we might be able to better understand what is going on.

in mqtt explorer i see json format


in HA MQTT listen to topic i see

the json entries(16) come in within a few seconds with the same topic v1/devices/me/telemetry

i think the other tool make a kind of caching ?!?