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…
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?
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.
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)