This one is probably going to be dead obvious to someone else - Regex rules

I have set up a simple rules (after giving up doing this via Exec / Http bindings) - but have learnt that the issue actually works if I exclude my regex transform.

When I test my regex on RegexBuddy or Regex101 it works perfectly, but when I run it using Exec / Http or just a normal rule based transform, it fails:

Here is a sample rule:

rule "test Rule"
when
//   Item test_switch received command 
   Item GetRainUpdate_Output received command 
then
logInfo("execTest", "START")

    var	 shortened = transform( "REGEX", ".div.class..mc.summary..[\s]*<p>(.*)<\/p>", GetRainUpdate_Output)
end

Here is a WORKING regex101 and a few alternative working regexes for my string parse…

https://regex101.com/r/wvwy7C/1
https://regex101.com/r/wvwy7C/2
https://regex101.com/r/wvwy7C/3

I suspect the issue has something to do with the escaping of backslashes (and potentially the supported

Anyone able to spot my (more than likely) obvious mistake?

There is a whole host of ways this can fail. Care to give us a hint? Does it return the wrong value? Does it throw and error? Does openHAB fail to parse the Rule entirely?

Thanks for the response Rich,

I have gotten quite a lot further now.

Learnt that the regex is adding ^ and $ to show start and of of string - eventually got to:

REGEX(.*.div.class..mc.summary..[^<]*<p>([\w| ]*).*)

which I was able to use direct in the Exec binding’s transform config.

I now strip down the piece of info I need.

Next step is to extract the number from my result looking like:


Periods of rain ending in 45 min

Right I have gotten further now:

I have an output string that says:

Periods of rain ending in 39 min

and I am trying to run the following to extract just the number:

rule "Track next Rain Rule"
when
//   Item test_switch received command 
Item GetRainUpdate_Output changed
then
logInfo("execTest", "START - Track next Rain Rule: " + GetRainUpdate_Output.state)
 val outputStr = new String(transform("REGEX", ".* (\\d+).*", GetRainUpdate_Output.state))
// val Integer outputInt = Integer::parseInt(outputStr.toString())
 logInfo("execTest", "Calculated" + outputStr)
   
end

but my logs say:


2018-05-25 15:06:45.236 [INFO ] [ipse.smarthome.model.script.execTest] - START - Track next Rain Rule: Periods of rain ending in 39 min

2018-05-25 15:06:45.241 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Track next Rain Rule': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.core.transform.actions.Transformation.transform(java.lang.String,java.lang.String,java.lang.String) on instance: null

First, the easy thing. there is no reason to create a new String. the trasnform returns a String already.

I’m pretty sure you do not want to double escape the \d. The error is most likely that the transform is not matching anything and therefore returning null which cannot be passed to the String constructor.

Thanks for the response Rich, but the double escape was due to an earlier bug:

When the regex is \d instead of \\d, I get:

2018-05-25 16:03:40.040 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'rainTracker.rules' has errors, therefore ignoring it: [7,41]: Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )

(Code as follows:)

val outputStr = transform("REGEX", ".* (\d+).*", GetRainUpdate_Output.state)

Furthermore, stripping the \d entirely with:

val outputStr = transform("REGEX", ".* ([0-9]+).*", GetRainUpdate_Output.state)

returns:

2018-05-25 16:10:11.028 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Track next Rain Rule': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.core.transform.actions.Transformation.transform(java.lang.String,java.lang.String,java.lang.String) on instance: null

we know that the inbound value is not null, since we are able to log it in the preceding bit of code (see previous post with full rule and log sample)

Try GetRainUpdate_Output.state.toString

Sometimes the Rules DSL has difficulty recognizing how to convert things to the proper type.

Thanks Rich - a combination of the above tricks got me going!
Have a super weekend