REGEX in MQTT channel

Why is my item configuration not working in MQTT ?

I have following under channels in the .thing file

Type string : bsblan_heating_circuit_1_operating_mode “Heating circuit 1 operating mode” [ stateTopic=“BSB-LAN/700”, transformationPattern=“REGEX: - (.*)” ]

And this is the item definition

String bsblan_heating_circuit_1_operating_mode “Heating circuit 1 operating mode” {channel=“mqtt:topic:synology:sensors:bsblan_heating_circuit_1_operating_mode”}

The MQTT input is 3 - Comfort
I’m trying to strip out the Comfort and have Comfort as value for the item.

What’s wrong with my REGEX in the channel definition ?

Try: (?<= - ).*

This catches everything after " - ", but without that, so the result would be “Comfort”

@Oggerschummer

Thomas thanks a lot. I tried it in both the thing

Type string : bsblan_heating_circuit_1_operating_mode      "Heating circuit 1 operating mode" [ stateTopic="BSB-LAN/700", transformationPattern="REGEX:(?<= - ).*" ]

as well as using it as a profile in the item configuration

String bsblan_compressor_toestand_item                   "Compressor toestand" (gBSBLan) {channel="mqtt:topic:synology:sensors:bsblan_compressor_toestand" [profile="transform:REGEX", function="(?<= - ).*"]}

and neither works. The item is either NULL or empty.

I tested your regex code in regex101 and it is indeed correct.
I’m a bit puzzled :slight_smile:

With the UI config REGEX is working fine for me (I do no longer use .things-Files).
You may have run into this issue reported:

Just try to model one of these channels with a separate testing Thing in the UI - if it works you know the reason…

Try

.*\s\-\s(.*)

or

.*?([a-zA-Z]+)

to avoid spaces within your regular expression.

@Ap15e thanks a lot almost there :slight_smile:

The second one works. Apparently OH seems to have issues with spaces and with the backslashes in the first regex you provided as well.

The only issue left is the second regex splits every space which isn’t really delivering the desired result.

The input
5 - Charged, nominal temperature

return temperature rather than
Charged, nominal temperature

So I now only need to figure out a REGEX that provide the part after the - without leading space without using a backslash and without using a space in the regex.

From the docs

As the escape character of strings is the backslash this has to be escaped additionally.

1 Like

that did the trick. I was confuse by RegEx - Transformation Services | openHAB but as there are examples but also there the text indeed mentions the need for escape.

One last question.

What’s the difference between

transformationPattern="REGEX:.*\\s-\\s(.*)"

and

transformationPattern="REGEX:.*\\s\\-\\s(.*)" 

so the regex

.*\s\-\s(.*) and .*\s-\s(.*)

both work so wondering what the bacslash in front of the - does in the expression (just out of curiosity to better understand regex)

Thanks, that did not come to mind… Escaping is such an IT anachronism, but often required. I wonder if using the UI this can be prevented (Does the UI escape automatically when storing the REGEX statement?). Will have to try it.

- is a reserved character for range definitions (within […]). Escaping means: this is a literal hyphen, not a range definition. In your case it doesn’t matter because the - isn’t inside a […].