Extract data from messages

Hi there,

I’ve built an P1 Energy meter with Tasmota which sends the P1 Payload every few seconds over mqtt like this:

16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"/KFM5KAIFA-METER"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":""}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-3:0.2.8(42)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:1.0.0(190104170020W)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:96.1.1(4530303235303030303639363432393136)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:1.8.1(002342.060*kWh)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:1.8.2(002566.728*kWh)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:2.8.1(000000.000*kWh)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:2.8.2(000000.000*kWh)"}
16:59:39 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:96.14.0(0002)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:1.7.0(00.428*kW)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:2.7.0(00.000*kW)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:96.7.21(00000)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:96.7.9(00000)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:99.97.0(1)(0-0:96.7.19)(000101000001W)(2147483647*s)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:32.32.0(00000)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:32.36.0(00000)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:96.13.1()"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-0:96.13.0()"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:31.7.0(002*A)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:21.7.0(00.453*kW)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"1-0:22.7.0(00.000*kW)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-1:24.1.0(003)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-1:96.1.0(4730303332353631323736373836373136)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"0-1:24.2.1(190104160000W)(02949.209*m3)"}
16:59:40 MQT: tele/wemos-9/RESULT = {"SerialReceived":"!EE58"}

Ref on Tasmota Wiki

So the plan is to add multiple mqtt sensors which show the appropriate value. I figured that each sensor needs some kind of value_template to only match on it’s intended value (Current usage kWH, Total usage kWH).

So, I thought I should add a regex match in the value template in the mqtt sensor to match and extract the value, which is the underlined part:
{“SerialReceived”:“1-0:1.7.0(00.428kW)”}

With my limited regex knowledge I came up with this:

- platform: mqtt
  name: Huidig stroomverbruik
  state_topic: "EnergyMeter/tele/RESULT"
  value_template: '{{ value | regex_findall_index("({\"SerialReceived\":\"\d-\d:1\.7\.0.+?)|......|(\*kW\))") }}'

But it seems to match to the first part and then default back to (", ")
01
06

And now I’m a bit stuck. Can anyone give me some pointers, I have the feeling I’m nearly there :slight_smile:

Thanks!

List item

save your self some regex hassel and do this:

- platform: mqtt
  name: Huidig stroomverbruik
  state_topic: "EnergyMeter/tele/RESULT"
  value_template: >
    {% set found = value_json.SerialReceived | regex_findall_index("1-0:1.7.0(\(\d+.\d+\*kW\))") %}
    {{ found | first | replace('(','') | replace('*kW)','') }}
  unit_of_measurement: kW

You don’t need to do \1.-\0, you can just hardcode that whole string. This will still output

Also, use this site to help w/ regex