REGEX problem

Guys I’m going crazy with REGEX!!!

I’ve a switch that should update it’s status from command received through UDP.
Here is the switch item:

Switch Light_Test "Light Test" <light> { udp="<[ON:*:'REGEX(relay6ON)'], <[OFF:*:'REGEX(relay6OFF)']" }

But on the log I see this:

Can not parse input relay6ON to match command OFF on item Light_Test the given regular expression '^relay6ON$' doesn't contain a group. No content will be extracted and returned! Can not parse input relay6ON to match command ON on item Light_Test

And the same for the relay6OFF command received.
Why does it read the “^” at the beginning and the “$” at the end?

If in the item file I use dounble brackets for the REGEX:

Switch Light_Test "Light Test" <light> { udp="<[ON:*:'REGEX((relay6ON))'], <[OFF:*:'REGEX((relay6OFF))']" }

I still get these errors:

Can not parse input relay6ON to match command OFF on item Light_Test Can not parse input relay6ON to match command ON on item Light_Test

Why, why whyyyy, oh my god why!

A string like “relay6ON” cannot be converted to an OnOffType. The extra parentheses in a REGEX transformation are a regular expression match group. Only the matched group will be returned from the REGEX transformation. In your case, REGEX(relay6(ON)) and REGEX(relay6(OFF)) should work. See Java regex documentation for more details.

Thanks Steve for your prompt reply.
I’ll go studying the Java regex documentatio.
In the meanwhile your suggestion works even if in the log I see rivers of errors whenever a UDP commands arrive to OH:

2016-01-04 16:19:43.733 [WARN ] [t.protocol.internal.UDPBinding] - Can not parse input relay6ON to match command OFF on item Light_Test 2016-01-04 16:19:43.744 [ERROR] [t.protocol.internal.UDPBinding] - transformation throws exception [transformation=null, response=relay6ON] java.lang.NullPointerException: null at java.util.regex.Matcher.getTextLength( ~[na:1.8.0] at java.util.regex.Matcher.reset( ~[na:1.8.0] at java.util.regex.Matcher.<init>( ~[na:1.8.0] at java.util.regex.Pattern.matcher( ~[na:1.8.0] at org.openhab.binding.tcp.protocol.internal.UDPBinding.splitTransformationConfig( [bundlefile:na] at org.openhab.binding.tcp.protocol.internal.UDPBinding.transformResponse( [bundlefile:na] at org.openhab.binding.tcp.protocol.internal.UDPBinding.parseBuffer( [bundlefile:na] at org.openhab.binding.tcp.AbstractDatagramChannelBinding.parseChanneledBuffer( [bundlefile:na] at org.openhab.binding.tcp.AbstractDatagramChannelBinding.execute( [bundlefile:na] at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute( [org.openhab.core_1.7.1.jar:na] at org.openhab.core.service.AbstractActiveService$ [org.openhab.core_1.7.1.jar:na]

Hi Andrea

This could be because OH tries to parse both values but could only find a match for on (either ON or OFF).

Without knowing exactly what might work is:
Switch Light_Test "Light Test" <light> { udp="<['REGEX(relay6(ON|OFF))'] }


Thanks Dieter! Unfortunately this didn’t work either. Now it throws this error:

transformation throws exception [transformation=null, response=relay6ON]

Another problem is that if I put in the same switch the UDP out and UDP in, it process only the last one in the line:

Switch Kitchen_light "Kitchen_light" <light> { udp=">[ON:'MAP('], >[OFF:'MAP(']", udp="<[*:'REGEX(relay6(ON|OFF))']" }

In this case it processes only the incoming UDP, if I put last the UDP>, it processes only the UDP out command.

Hi Andrea
Sorry for that.
Even if the expression is correct (will return either ON or OFF depending on the response), I must admit that I haven’t used Regexp in Openhab myself. So there might be another problem.

Sorry that I couldn’t help you whit this.


No problem, you helped me anyway!!!

Do you know why if I have this item

Switch Light_Test "Light Test" <light> { udp=">[ON:'MAP('], >[OFF:'MAP('], <[ON:*:'REGEX(relay6ON)'], <[OFF:*:'REGEX(relay6OFF)']" }

it receve the commands from the UDP but it doesn’t send anything? If I remove the receiving part:

Switch Light_Test "Light Test" <light> { udp=">[ON:'MAP('], >[OFF:'MAP(']" }

it works without any problems?

@steve1, do you know if there is a temporary workaround here, like spaces instead of commas?

It’s not clear to me that this is the problem with spaces vs. commas between binding subconfigs. Are there any errors in the log? Which binding version is being used?

Ah, I notice above references to 1.7.1 in the stack trace, so that points away from that theory (but possibly not conclusively). Thanks for replying.


did you solve this problem? I’m having the exactly the same problem (OH2).

Switch LichtKueche “LichtKueche” {tcp=">
[ON:‘MAP(’] >[OFF:‘MAP(’]
<[ON:‘REGEX(.*value 30.*)’]
<[OFF:‘REGEX(.*value 0.*)’]"}

If i strip the incoming part, the switch works as expected.

The incoming parts do not begin with ON or OFF (see the example at the bottom of the wiki page). Also, the incoming parts have to result in strings ON or OFF in order to update a Switch item. Your REGEX transformations do not capture the input, and even if they did (by doing REGEX((.*value 30.*)) for example), they don’t result in an ON or OFF. If the complete incoming message is the same every time, you might be able to use a MAP transformation like

ON=value 30
OFF=value 0

but that would have to be the entire incoming message. Another alternative would be to use a JavaScript transformation, where you can have more logic in what produces the ON or OFF you’re after. Something like


  if (str.includes("value 30")) return "ON";
  if (str.includes("value 0")) return "OFF";
  return "UNDEF";

If that worked, your item definition would be:

Switch LichtKueche "LichtKueche" { tcp=">[ON:'MAP('] >[OFF:'MAP(' <['JS(lightValue.js)']" }