Could use some help with regex

That removed the “Unclosed group” error! Thx!
Don’t get any data into the DateTime item, most likely due to the \r\n. No matter what I do I either get an error in the expression, or no data :frowning:

Neither does the rant (to me) :rofl:

This may sound silly but would it be easier, maybe to change the firmware on your mqtt device (I assume it’s an ESP or arduino) so that it sends 2 messages ot that the 2 values are separated by something else than /r/n. Or chage the format to JSON.
It would then be VERY easy on OH
Changing firmware to change 2 character on string.Or change to JSON - 5 minutes
Upload firmware - 5 minutes
JS or JSONPATH transformation instead of REGEX for the two items in OH - 10 minutes
TOTAL 20 minutes

Total amount of time lost on REGEX so far: 4 days

In an ideal world that would be the solution. However the MQTT setup in the case is fixed and cannot be altered, so I’m stuck with what I have.
For those skilled in Rexex it’s probably very easy, but as I’ve said it’s too complicated for me to fix myself :confused:

You have the first one working to retrieve the value, I gather
The date part is fixed length so we can use a JS transform to retrieve it

Create a file called test.js in your conf.transform with the following content:

(function(i) {
    var dateString = i.slice(-28);
    var d = new Date(dateString);
    return d.getTime() / 1000;

Your DateTime item becomes:

DateTime Power_Time { mqtt="<[mosquitto:Electricity/EL/Energy/Import/kWh:state:JS(test.js)]" }
  1. Your regex seems not to be the same as as posted from @rlkoshak.

  2. I think as your regex is embedded into a string you have to properly escape the backslash.

  1. As it seems that you want to match a multiline string you need something like

Explain in detail in this post.

I did not test it but it should be something like

Number Power { mqtt="<[mosquitto:Electricity/EL/Energy/Import/kWh:state:REGEX((\\d*.\\d*).*)]" }
DateTime Power_Time { mqtt="<[mosquitto:Electricity/EL/Energy/Import/kWh:state:REGEX(s/.*\\s(.*)/$1/g)]" }

Thanks for your links and suggestion. I’m out travelling but will try it when I get back. If the Regex method doesn’t work for me, then I’ll try to persuade the developer into changing his format to JSON instead :slight_smile:

@Josar The power item is working as it should :). Thank you for that. Unfortunately the DateTime item results in '2018-05-15 21:42:11.745 [WARN ] [] - given new state is NULL, couldn’t post update for ‘Power_Time’ :frowning:. Soooo close. Any idea to why?

I was a little worried about this. 2018-05-09T20:55:00.0000000Z is a little fishy. After going down the rabbit hole, I didn’t get that far, while that is ISO 8601 format, from the Wikipedia article:

However, the number of decimal places needs to be agreed to by the communicating parties. For example, in Microsoft SQL Server, the precision of a decimal fraction is 3, i.e., “yyyy-mm-ddThh:mm:ss[.mmm]”.

I’m willing to bet that Joda or Date also only supports three decimal places. And if some of those zeros are supposed to denote the time zone offset, then it is missing the + sign and it is not valid ISO 8601 format.

But, however you look at it, I think you need to strip the . and the seven zeros off of the date which you should be able to do with something like:


which will only match the date up to the ..

Thx @rlkoshak. This gives me:

2018-05-15 22:10:34.553 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Electricity.items’ has errors, therefore ignoring it: [14,34]: mismatched character ‘.’ expecting set null
[14,105]: mismatched input ‘.’ expecting RULE_STRING
[14,109]: missing ‘}’ at ‘1’
[14,110]: extraneous input ‘/’ expecting RULE_ID
[20,121]: mismatched character ‘’ expecting ‘"’

I feel really noobish…

You probably have to double escape the .


Thx! Will try this evening. I’m beginning to understand why people who entered the pyramids were confused when looking at the hieroglyphs for the first time :slight_smile:

Thanks guys for trying to help me out. I never managed to get the time/date to work so in the end I gave up. I’ve contacted the developer and suggested that he implements JSON instead, that seems to be much easier to work with :slight_smile:

Have him implement ISO 8601 dates time format with only three values for the milliseconds and the propper formatting for the utc offset with a + at the end or else even with JSON formatting you will have difficulty assigning the date to a DateTime Item.

Hi everyone. I am new to regex, and I am trying to use a string from the serial1 binding that catches the temperature and the energy consumption at this moment. Every second, it returns the following string:


From this, I would like to extract the temperature (the value within <tmpr> and </tmpr>) and the energy consumption (the value within <watts> and </watts>). To extract the temperature, I used the following line:

String CurrentCostTemp				{ serial="/dev/ttyUSB0@57600, REGEX(.*<tmpr>(.*?)</tmpr>.*)" }

However, the Regex expression is not valid (unrecognized), and I don’t understand why. I also tried replacing the capture group by this: (\\d*.\\d*), but without success. Can anybody help?

You should be able to use the xml transform on that string

Thank you but the Serial binding does not support XSLT. I had tried with the following:

String CurrentCostTemp				{ serial="/dev/ttyUSB0@57600, XSLT(CurrentCostTemp.xsl)" }

But disregarding the content of the xsl file, the error message was: Unrecognized transform: XSLT(CurrentCostTemp.xsl)

So it seems the Serial binding does not use a generic transformation API but only the ones it supports explicitly.

Did you install the XSLT transform?

With the XPATH transform


the result is 23.8


1 Like

And for Watts the XPATH transform is /msg/ch1/watts/text()