Delete ending space with regex at udp binding

Sorry for asking, but I fight with the regex now for some hours.

I use the udp binding to get a string from my fingerprint reader.

String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:default]" }

The String I got is with a lot of ending spaces. I tried

String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:'REGEX((.*))']" }

but this gives me the same. I searched in the internet for a way to cut the ending spaces with regex. Found some hints but all are not working inside item definition. What do I wrong

Some hints for regex:

^[ \t]+
String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:'REGEX((^[ \t]+))']" }

^[ \t]+|[ \t]+$
String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:'REGEX((^[ \t]+|[ \t]+$))']" }

^[\s]+
String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:'REGEX((^[\s]+))']" }

Any idea what I’m doing wrong?

Can you send one example of the string you want to process, please?

[ \t]+$
Seems to work on https://regex101.com/

@vzorglub:
Thanks for the reply. All three examples are working on regex101.com. But I get not to work in OH.

My syntax of using it inside of the item definition seams to be wrong.

Here my string

1_0002_6_80183822150798_1_1                                                                                                 

Al lot of spaces at the end.

Try:

String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:'REGEX(([ \t]+$))']" }

without the ^

It is not working:
String changed to null

2018-04-22 10:08:45.501 [vent.ItemStateChangedEvent] - ekeyHomeTest changed from 1_0002_6_80183822150798_1_1                                          to null

Now it is working, don’t know why.

I’ve tried this before with no success, but no it if working fine

String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4::*:'REGEX((.*))']" }
2018-04-22 10:15:46.221 [vent.ItemStateChangedEvent] - ekeyHomeTest changed from NULL to 1_0002_6_80187242150798_1_2                                                                                                                    
2018-04-22 10:15:46.221 [vent.ItemStateChangedEvent] - ekeyHomeTest changed from 1_0002_6_80187242150798_1_2                                                                                                                                                to 1_0002_6_80187242150798_1_2

Try changing to a javascript transform

In your transform folder create a file called ekey.js
With he content:

(function(i) {
    if (i == 'NULL') {return i; }
    if (i == '-') {return 'Undefined'; }
    return i.trim(); // Removes all spaces and beginning and end
})(input)

And change you item to:

String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:'JS(ekey.js)']" }

Thanks, this not working converts from ending spaces to ending spaces. Bit is is solved with REGEX((.*)).

But I run into another problem too. The udp-binding or the transform (even default, regex or js) updates the item 15 times.

If your udp server publishes 15 lines you’ll get 15 updates. Is that the case?

I checked it with wireshark. The udp packet is send only once from the remote device. But something inside of OH udp binding updates the item a lot of times.

OK, can you show your udp.cfg file and your log when an update happens, please

There is another thing I see.

If I use

String ekeyHomeTest     "ekeyHomeTest [%s]"     { udp="<[192.168.1.4:*:default]" }

the item is updated only from null to the string with the spaces. If I use the regex. The item is updated to the string with the spaces and then, maybe after the regex is running, to the string without the spaces. I see this double update only in the udp binding.

In the http binding the transform in the item config does not update the item twice. I get only one update not two.

Yes I noticed that earlier. But it doesn’t explain the 15 times…

Here is my config, now with different IP-addresses because I use my test system now

Item:
String ekeyHomeTest         "ekeyHomeTest [%s]"     { udp="<[10.4.1.4:*:default]" }

Rule:
rule "ekey message"
    when
        Item ekeyHomeTest received update
    then
        logInfo("Tester", "ekeyHomeTest:   " + ekeyHomeTest.state.toString)
end

udp.cfg:
port=56000
addressmask=true
updatewithresponse=true
charset=ASCII

Log:
2018-04-22 22:10:39.939 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
2018-04-22 22:10:39.939 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
2018-04-22 22:10:39.939 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
2018-04-22 22:10:39.939 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Sometimes one line in log, sometimes more, depends on?

If I change to

String ekeyHomeTest         "ekeyHomeTest [%s]"     { udp="<[10.4.1.4:*:'REGEX((.*))']" }

Event Log: (two updates, one before REGEX and one after):
2018-04-22 22:14:12.189 [vent.ItemStateChangedEvent] - ekeyHomeTest changed from NULL to 1_0002_6_80187242150798_1_3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
2018-04-22 22:14:12.221 [vent.ItemStateChangedEvent] - ekeyHomeTest changed from 1_0002_6_80187242150798_1_3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      to 1_0002_6_80187242150798_1_3

Openhab log:

2018-04-22 22:14:12.455 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3
2018-04-22 22:14:12.455 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3
2018-04-22 22:14:12.455 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3
2018-04-22 22:14:12.455 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3
2018-04-22 22:14:12.455 [INFO ] [clipse.smarthome.model.script.Tester] - ekeyHomeTest:   1_0002_6_80187242150798_1_3

one or more times triggering the rule.

But only one time send the udp packet over the network.

I tried to fix it with reentrancelock. But the item update sometimes lasts 2 sec. I should have a 2 sec wait until unlock. Very long for a sleep. I fixed it with trigger on changed instead of update and change string to something else after command is processed. Not good but it is working.

rule "ekey message"
    when
        Item ekeyHomeTest changed
    then
        if(ekeyHomeTest.state.toString == "keineAenderung")
            return;
        
        logInfo("Tester", "ekeyHomeTest:   " + ekeyHomeTest.state.toString)
        ekeyHomeTest.postUpdate("keineAenderung")
end

I assume that your finger print reader send data after scanning a print and not after an OH request. In that case you don’t need updatewithresponse=true. You can comment it out or set it to false. Does that change anything?

I tried this too, with no success. I us a udp test tool to simulate udp packets. This is the same behaviour. I think something inside of the binding is wrong with the handling of received packets.

You can file an issue on Github