Sensor - How to read long MQTT payloads?

I have a really long MQTT message coming from a power meter.

Topic: tele/sonoff/RESULT
Data
{“SerialReceived”:“00000 B
PPOT 00 #
ADCO 041663061553 ?
OPTARIF BASE 0
ISOUSC 15 <
BASE 008449748 7
PTEC TH… $
IINST1 000 H
IINST2 000 I
IINST3 002 L
IMAX1 060 6
IMAX2 060 7
IMAX3 060 8
PMAX 04526 7
PAPP 00490 .
HHPHC A ,
MOTDETAT 000000 B
PPOT 00 #
ADCO 041663061553 ?
OPTARIF BASE 0
ISOUSC 15 <
BASE 008449748 7
PTEC TH… $
IINST1 000 H
IINST2 000 I
IINST3 002 L
IMAX1 060 6
IMAX2 060 7
IMAX3 060 8
PMAX 04526 7
PAPP 00490 .
HHPHC A ,
MOTDETAT 000000 B
PPOT 00 #
ADCO 041663061553 ?
OPTARIF BA”}

I want to be able to put certain values into a sensor

Has anyone an idea how this could be achieved?

Yes, it’s possible. What do you want to extract from it to use as the sensor’s value?

Hi
For example, from " PAPP 00490", l would like a sensor named PAPP to indicate 490.

OK, that’s easily done but which one? Your sample data contains two instances of PAPP 00490.

Will every payload have repeated data?

To confirm we’re on the same wavelength, paste the following into Home Assistant’s Template Editor.

{% set data = "00000 B
PPOT 00 #
ADCO 041663061553 ?
OPTARIF BASE 0
ISOUSC 15 <
BASE 008449748 7
PTEC TH… $
IINST1 000 H
IINST2 000 I
IINST3 002 L
IMAX1 060 6
IMAX2 060 7
IMAX3 060 8
PMAX 04526 7
PAPP 00490 .
HHPHC A ,
MOTDETAT 000000 B
PPOT 00 #
ADCO 041663061553 ?
OPTARIF BASE 0
ISOUSC 15 <
BASE 008449748 7
PTEC TH… $
IINST1 000 H
IINST2 000 I
IINST3 002 L
IMAX1 060 6
IMAX2 060 7
IMAX3 060 8
PMAX 04526 7
PAPP 00490 .
HHPHC A ,
MOTDETAT 000000 B
PPOT 00 #
ADCO 041663061553 ?
OPTARIF BA" %}

{{ data | regex_findall_index("PAPP (\d+) ", ignorecase=true) | int }}

The template at the very bottom is what performs the magic.

{{ data | regex_findall_index("PAPP (\d+) ", ignorecase=true) | int }}

It uses a regex pattern to find all instances of the desired text. It reports the first instance it finds which is then converted to an integer.

The regex pattern works like this:

  • PAPP means match this literal string of characters.
  • () is a capture group. Whatever is found within the parentheses is reported.
  • \d+ means match one or more numbers.

If you’re satisfied it produces the result you want, the next step is to create an MQTT Sensor that uses the template to report the desired value.

Sorry I hadn’t noticed that…
I have a linky french electricity meter connected to a tasmotorised sonoff. I am using the tasmota serial bridge to generate the mqtt msg.
It looks as if tasmota is breaking up the transmisson frames in a somewhat random manner.
Each frame should start with an ADCO item.

That’s what I thought. The sample data appeared to be a randomly selected fragment from a stream of continuous data.

I guess as long as those fragments contain at least one PAPP then the template I suggested will report its value.

Thanks
Sorry i can’t try this right away, it is late
With a bit of luck, I’ll have time tomorrow
Bry

I got your example working in the template editor OK

But when I try to configure as a sensor, I get state unknown

sensor:
  - platform: mqtt
    name: "myLinky"
    state_topic: "tele/sonoff/RESULT"
    value_template: >
      {% set data = value_json.SerialReceived %}
      {{ data | regex_findall_index("PAPP (\d+) ", ignorecase=true) | int }}

PS I corrected the serial deliminator in TASMOTA so I get individual frames now…:slightly_smiling_face:

By trial and error , I seem to have found the solution:

This seems to work

  - platform: mqtt
    name: "myLinky"
    state_topic: "tele/sonoff/RESULT"
    value_template: >
      {% set data = value %}
      {{ data | regex_findall_index("PAPP (\d+) ", ignorecase=true) | int }}

There’s no need to do this:

      {% set data = value %}

Remove that line and just do this:

      {{ value | regex_findall_index("PAPP (\d+) ", ignorecase=true) | int }}

For the purpose of experimenting with the Template Editor, I randomly chose the variable’s name to be data.

For use in value_template, the received payload’s variable name is value (or value_json if you wish to access the payload as JSON, assuming it is valid JSON).

can some one help me with this telegram of a P1 dsmr telegram.
o dont no how i can do that
im hope annybody can help me this if the telegram how most i do this ??

06:35:07 MQT: tele/test/RESULT = {“SerialReceived”:"/XMX5LGBBFG1009062887

1-3:0.2.8(42)
0-0:1.0.0(191211063511W)
0-0:96.1.1(4530303331303033313039363936313135)
1-0:1.8.1(004552.875kWh)
1-0:1.8.2(005533.126
kWh)
1-0:2.8.1(000000.002kWh)
1-0:2.8.2(000000.001
kWh)
0-0:96.14.0(0001)
1-0:1.7.0(00.089kW)
1-0:2.7.0(00.000
kW)
0-0:96.7.21(00002)
0-0:96.7.9(00000)
1-0:99.97.0(0)(0-0:96.7.19)
1-0:32.32.0(00000)
1-0:32.36.0(00000)
0-0:96.13.1()
0-0:96.13.0()
1-0:31.7.0(001A)
1-0:21.7.0(00.089
kW)
1-0:22.7.0(00.000kW)
0-1:24.1.0(003)
0-1:96.1.0(473"}
06:35:07 MQT: tele/test/RESULT = {“SerialReceived”:"303332353631323436343439393135)
0-1:24.2.1(191211060000W)(00092.955
m3)
“}
06:35:08 MQT: tele/test/RESULT = {“SerialReceived”:”!2F7B
"}

i dont no how i do this . ??
And save on mqttFX