So I am pretty new to this and searched the forum for the following “IndexError: list index out of range”
I have the following mqtt Message:
'ESP_Easy/Watermeter/P1': '!Serial#/CTA5ZIV-METER
1-3:0.2.8(50)
0-0:1.0.0(240401182519S)
0-0:96.1.1(4530303930303030373331343231333233)
1-0:1.8.1(003491.351*kWh)
1-0:1.8.2(001754.853*kWh)
1-0:2.8.1(000295.076*kWh)
1-0:2.8.2(000729.567*kWh)
0-0:96.14.0(0001)
1-0:1.7.0(01.885*kW)
1-0:2.7.0(00.289*kW)
0-0:96.7.21(00013)
0-0:96.7.9(00010)
1-0:99.97.0(4)(0-0:96.7.19)(230922112108S)(0000004157*s)(230918162050S)(0000000339*s)(230822142827S)(0000000214*s)(230801123219S)(0000002519*s)
1-0:32.32.0(00000)
1-0:52.32.0(00000)
1-0:72.32.0(00000)
1-0:32.36.0(00014)
1-0:52.36.0(00009)
1-0:72.36.0(00012)
0-0:96.13.0()
1-0:32.7.0(234.0*V)
1-0:52.7.0(237.0*V)
1-0:72.7.0(235.0*V)
1-0:31.7.0(006*A)
1-0:51.7.0(001*A)
1-0:71.7.0(001*A)
1-0:21.7.0(01.609*kW)
1-0:41.7.0(00.000*kW)
1-0:61.7.0(00.276*kW)
1-0:22.7.0(00.000*kW)
1-0:42.7.0(00.296*kW)
1-0:62.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(4730303732303034323130343432323232)
0-1:24.2.1(240401182500S)(00733.001*m3)
!F9B8'
And probably I am missing the point on how to process P1 data that is published to that specific topic (maybe I need to do it differently but totally new to HA).
So I created the following sensors:
- name: "MQTT-P1 High Tariff Return"
state_topic: "ESP_Easy/Watermeter/P1"
value_template: >
{% set raw_value = trigger.payload %}
{% if raw_value %}
{% set corrected_value = raw_value | regex_replace("!Serial#|\\)\\n", '') %}
{% set regex_matches = corrected_value | regex_findall_index("1-0:2.8.2\(([\d.]+)\*kWh\)", 0) %}
{% if regex_matches %}
{% set value = regex_matches[0] %}
{{ value.split('*')[0] | float if value | float > 0 else none }}
{% else %}
none
{% endif %}
{% else %}
none
{% endif %}
unique_id: mqtt_high_tariff_return
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
# -------------------------------------------------------------------------------------------------------
- name: "MQTT-P1 Gas Consumption"
state_topic: "ESP_Easy/Watermeter/P1"
value_template: >
{% set raw_value = trigger.payload %}
{% if raw_value %}
{% set corrected_value = raw_value | regex_replace("!Serial#|\\)\\n", '') %}
{% set regex_matches = corrected_value | regex_findall_index("0-1:24\.2\.1\(([\d.]+)\*m3\)", 0) %}
{% if regex_matches %}
{% set value = regex_matches[0] %}
{{ value.split('*')[0] | float if value | float > 0 else none }}
{% else %}
none
{% endif %}
{% else %}
none
{% endif %}
unit_of_measurement: "m³"
device_class: gas
state_class: total_increasing
icon: mdi:meter-gas
unique_id: mqtt_gas
Effectively this should extract the values from the provided data and not end up with the following in the log:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 484, in wrapper
msg_callback(msg)
File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 43, in wrapper
msg_callback(msg)
File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 278, in message_received
_update_state(msg)
File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 226, in _update_state
payload = self._template(msg.payload, PayloadSentinel.DEFAULT)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/mqtt/models.py", line 316, in async_render_with_possible_json_value
self._value_template.async_render_with_possible_json_value(
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 753, in async_render_with_possible_json_value
render_result = _render_with_context(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2364, in _render_with_context
return template.render(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 3, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2135, in regex_findall_index
return regex_findall(value, find, ignorecase)[index]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
IndexError: list index out of range
Anyone that can assist ??