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:192.168.20.12:*:'REGEX(relay6ON)'], <[OFF:192.168.20.12:*:'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:192.168.20.12:*:'REGEX((relay6ON))'], <[OFF:192.168.20.12:*:'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!
Thanks!!!
Andrea

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(Matcher.java:1283) ~[na:1.8.0] at java.util.regex.Matcher.reset(Matcher.java:309) ~[na:1.8.0] at java.util.regex.Matcher.<init>(Matcher.java:229) ~[na:1.8.0] at java.util.regex.Pattern.matcher(Pattern.java:1093) ~[na:1.8.0] at org.openhab.binding.tcp.protocol.internal.UDPBinding.splitTransformationConfig(UDPBinding.java:236) [bundlefile:na] at org.openhab.binding.tcp.protocol.internal.UDPBinding.transformResponse(UDPBinding.java:254) [bundlefile:na] at org.openhab.binding.tcp.protocol.internal.UDPBinding.parseBuffer(UDPBinding.java:150) [bundlefile:na] at org.openhab.binding.tcp.AbstractDatagramChannelBinding.parseChanneledBuffer(AbstractDatagramChannelBinding.java:998) [bundlefile:na] at org.openhab.binding.tcp.AbstractDatagramChannelBinding.execute(AbstractDatagramChannelBinding.java:1505) [bundlefile:na] at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) [org.openhab.core_1.7.1.jar:na] at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) [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="<[192.168.20.12::'REGEX(relay6(ON|OFF))'] }

Regards
Dieter

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:192.168.20.12:5005:'MAP(Kitchen_light.map)'], >[OFF:192.168.20.12:5005:'MAP(Kitchen_light.map)']", udp="<[192.168.20.12:*:'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.

Regards
Dieter

No problem, you helped me anyway!!!

Do you know why if I have this item

Switch Light_Test "Light Test" <light> { udp=">[ON:192.168.20.12:5005:'MAP(Kitchen_light.map)'], >[OFF:192.168.20.12:5005:'MAP(Kitchen_light.map)'], <[ON:192.168.20.12:*:'REGEX(relay6ON)'], <[OFF:192.168.20.12:*:'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:192.168.20.12:5005:'MAP(Kitchen_light.map)'], >[OFF:192.168.20.12:5005:'MAP(Kitchen_light.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.

Hi,

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

Switch LichtKueche “LichtKueche” {tcp=">
[ON:192.168.165.30:7891:‘MAP(LichtKueche.map)’] >[OFF:192.168.165.30:7891:‘MAP(LichtKueche.map)’]
<[ON:192.168.165.30:7891:‘REGEX(.*value 30.*)’]
<[OFF:192.168.165.30:7891:‘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

lightMap.map

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

lightValue.js:

(function(str){
  if (str.includes("value 30")) return "ON";
  if (str.includes("value 0")) return "OFF";
  return "UNDEF";
})(input)

If that worked, your item definition would be:

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