How to escape colon (:) in Item REGEX transformation

I have an MQTT message which looks like this:

TIME:0,INTE:21.90,INHU:33.68

I am trying to bind the INTE key’s value (in this example 21.90) to a Number item using a REGEX transformation. It wasn’t working, so I made it a String, and matched the entire message with REGEX:

String main_temperature "mainT [%.1f]" {mqtt="<[seal:weather:state:REGEX((.*))]" }

That worked, i.e. it gave the entire message. I added more of the message outside the capture group, and eventually got the value I wanted with this REGEX:

String main_temperature "mainT [%.1f]" {mqtt="<[seal:weather:state:REGEX(.*INTE.(.*),.*)]" }

Notice the single character pattern match “.” just after INTE to match the colon in the message. I would rather have an explicit colon, but this binding

String main_temperature "mainT [%.1f]" {mqtt="<[seal:weather:state:REGEX(.*INTE:(.*),.*)]" }

gives me an error, I think becase it sees the colon and thinks I am on to the next clause in the MQTT binding,

2017-12-28 13:25:29.196 [ERROR] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.
java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 6  
(.*),.*)

And if I try escaping the colon like this “\:”

String main_temperature "mainT [%.1f]" {mqtt="<[seal:weather:state:REGEX(.*INTE\:(.*),.*)]" }

I get an error about bad configuration:

2017-12-28 13:27:50.993 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'mqtt.items' has errors, therefore ignoring it: [4,46]: mismatched character ':' expecting set null
[4,82]: mismatched input '(' expecting RULE_STRING
[4,87]: mismatched input '.' expecting RULE_ID
[4,91]: mismatched character '<EOF>' expecting '"'

How do I exactly match a colon in MQTT binding REGEX?

have you tried to use %3A (or %%3A) ? I don’t use MQTT with that but it works for the HTTP binding.

Good idea, but neither of those worked. They both give a NULL result, I guess because the message never matches?

I don’t think this can be done. The MQTT config parser, as far as I’ve been able to tell, has no way to escape the : inside the REGEX. So when it sees the : it thinks you are done with that field and have moved on to the next one which results in an invalid MQTT config.

You can narrow it down a bit from . and have it only match characters that are not white space, digits, or letters:

String main_temperature "mainT [%.1f]" {mqtt="<[seal:weather:state:REGEX(.*INTE[^\w\s](.*),.*)]" }

Thank you both. I have something workable, so not the biggest issue.

If anyone else comes across this, don’t forget to switch back to Number after getting the pattern matching right. And consider using a test item name, especially if you want to persist the item.

Have you tried this: https://www.regular-expressions.info/refcharacters.html?
There should be a number of ways to escape the colon, although I don’t know which, if any, work in OpenHAB. Also found this, that might be of interest.

A double backslash works to escape the colon. I have this Item binding now working:

Number mainfloor_temperature "mainT [%lf]" {mqtt="<[seal:weather:state:REGEX(.*TIME.0,INTE\\:(.*),.*)]" }

Thank you for the clue, Mikael. Seems like openhab uses java’s regexp language, which makes sense.

1 Like