Help with REGEX transformation for mqtt channel

I have a mqtt channel and i use a chained tranformation to extract a temperature setpoint value

REGEX:(.*ATCExpect0.*)∩JSONPATH:$.NSPanel.ATCExpect0

so for a payload like

{"NSPanel":{"ATCMode":0,"ATCExpect0":20}}

i get the number 20.So far so good
A weird bug happens to the device(tasmota) and when it is set to “21” the payload goes like this

{"NSPanel":{"ATCMode":0,"ATCExpect0":21}B}}

an extra }B ruins the payload and i get log error

[WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: An error occurred while transforming JSON expression.

I read REGEX documentation and so i tried several versions of my incoming transformation like

REGEX:(.*ATCExpect0.*)∩REGEX:(s/}B}/}/)∩JSONPATH:$.NSPanel.ATCExpect0

with no success…Any ideas?

You’re probably going to have to resort to a JS transformation or a rule to process these messages. For what ever reason tasmota is sending invalid JSON and the REGEX transformation probably won’t get you to where you can correct it.

can you point me to an example how to remove }B characters with a JS transformation?I read the documentation but cant found out where to start…

That specifically? Not really. It’s standard ECMAScript 5.1 so any JavaScript tutorial on how to process strings will do. Put that into a filename.js file under $OH_CONF/transform and wrap your code un a function like the example in the docs.

(function(i) {
    // Your code goes here
    return value;
})(input)

Remove the parentheses for the substitution:

REGEX:(.*ATCExpect0.*)∩REGEX:s/}B}/}/∩JSONPATH:$.NSPanel.ATCExpect0
1 Like

thnx mate it is working fine now!