Special MQTT Regex

Hi,

I am trying to match the temperature in this MQTT message via REGEX.

{"co2":"817","temp":" 14.83 ","hum":"63","lux":"737"}

JSONPATH is not possible, since the temperature is whitespace padded (who knows why) and I don’t want to make a conversion rule.

This REGEX works like a charm in regex101

temp":" ([0-9.]{3,5})

So this is what the Thing looks like

Type number : temp "Temperatur" [ stateTopic="sensors/Ampel_1", transformationPattern="REGEX:.*temp":" ([0-9.]{3,5}).*" ]

However, it does not in Openhab.
Maybe openhab has problems with the quotation marks and the dot in the squared brackets?

Daniel

Almost certainly. You can’t nest quotation marks of the same type, because a second mark just closes the first one.

Try using a single quote mark for the nested phrase. I’m not sure if that works in OH as I’ve never done it, but I know it works in other cases.

Perhaps an easier approach: use JSONPATH to extract the value with its extra padding, and then a simpler REGEX chained afterwards to strip the whitespace?

1 Like

I am still struggling. this is the current version:

transformationPattern="JSONPATH:$.temp∩REGEX: ([0-9.]*) "

However, I do not get the regex to work properly, now it only throughs out 4.7 in case the temperature is 14.73 degrees.

Which regex rules is OpenHab using? How can I escape common characters like whitespace, dot, quotation marks?

Daniel

I used the JSONPATH transformation only into a proxy item and then used a rule to deliver into the final item.

The problem with your Regex may be that you need to represent the entire string in your Regex expression, even the parts you are not interested in. The approach of using a Regex chained to a Jsonpath expression should be the best solution. Try again with this slightly modified Regex:

transformationPattern=JSONPATH:$.temp∩REGEX: *([0-9.]*) *

Also make sure the “spaces” in your JSON are actually “spaces” and not some other character, that just gets printed as space. Perhaps try

transformationPattern=JSONPATH:$.temp∩REGEX:.*([0-9.]*).*

if the first expression does not work.
I use chained expressions for some of my Tasmota MQTT things and they work.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.