Bry
April 15, 2019, 4:43pm
1
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?
123
(Taras)
April 15, 2019, 4:56pm
2
Yes, it’s possible. What do you want to extract from it to use as the sensor’s value?
Bry
April 15, 2019, 5:12pm
3
Hi
For example, from " PAPP 00490", l would like a sensor named PAPP to indicate 490.
123
(Taras)
April 15, 2019, 5:20pm
4
OK, that’s easily done but which one? Your sample data contains two instances of PAPP 00490
.
Will every payload have repeated data?
123
(Taras)
April 15, 2019, 5:30pm
5
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.
Bry
April 15, 2019, 5:45pm
6
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.
123
(Taras)
April 15, 2019, 5:49pm
7
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.
Bry
April 15, 2019, 7:20pm
8
Thanks
Sorry i can’t try this right away, it is late
With a bit of luck, I’ll have time tomorrow
Bry
Bry
April 16, 2019, 9:49am
9
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…
Bry
April 16, 2019, 1:32pm
10
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 }}
123
(Taras)
April 16, 2019, 2:04pm
11
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).
Rico
(rico)
December 11, 2019, 5:29am
12
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