Values with regex

Hi, i read out my power meter with an ir-usb-reader. I get the values printed in Openhab already, i can see it in the live-log.

But i have to extract the right values from the text i get submittet.
Here is my items-file:

Number Exectest "Exx [%d]" <power> {exec="<[ssh -i /home/openhab/.ssh/key_openhab user@192.168.1.1 /usr/src/libsml/examples/sml_server /dev/lesekopf0:10000:REGEX((.*?))]" }

And this is what i can see in the live-log:


2017-02-02 15:06:59.958 [INFO ] [runtime.busevents             ] - Exectest state updated to 1-0:1.8.0*255#9620.988#
1-0:2.8.0*255#42317.589#
1-0:1.8.1*255#9620.988#
1-0:2.8.1*255#42317.589#
1-0:16.7.0*255#-1.377#

So the submitted information is this:

1-0:1.8.0*255#9620.988#
1-0:2.8.0*255#42317.589#
1-0:1.8.1*255#9620.988#
1-0:2.8.1*255#42317.589#
1-0:16.7.0*255#-1.377#

How can i extract the Values between the # with regex command? Can i extract all values in one command or do i have to make one item in item-file for each value?

I haven’t tried this here, but you could try the following replacement for REGEX((.*?)):

REGEX([^#]*#([^#]*)#)

By way of explaining the meaning of that regular expression, it reads: Match a string of any characters except a ‘#’ followed by a ‘#’ , then capture a string of any characters except a ‘#’, end the capture and match a final ‘#’.

In the submitted text there are 5 values, how can i read out them? I need one item for every value.

If i enter your example, then which one will be read out and how will this be stored in only one item?

My items file had an error. So my new item looks like that and now i get all the information shown as text (string):

String Exectest "Exx [%s]" <energy> {exec="<[ssh -i /home/openhab/.ssh/key_openhab debian@192.168.102.73 /usr/src/libsml/examples/sml_server /dev/lesekopf0:10000:REGEX((.*?))]" }

The execution of the script to get the information lasts some seconds, so i think executing the script several times to get each value to a separat item is no good idea.

I think i can do this with a rule too? Execute the script one time and store all the values as one long string. Then execute a rule and separate the values?

I have found another thread with the same problem and an example how to do it.

Here’s another try. You will still have to create a rule to extract the number from each line of the resulting string value of Exectest.

String Exectest "Exx [%s]" <energy> {exec="<[ssh -i /home/openhab/.ssh/key_openhab debian@192.168.102.73 /usr/src/libsml/examples/sml_server /dev/lesekopf0:10000:REGEX(s/(?m)^[^#]+#([^#]+)#$/$1/g)]" }

The result should be a string consisting of the five numbers extracted from each line of your example (one number per line):

9620.988
42317.589
9620.988
42317.589
-1.377

One assumption I made is that there is no white space at the end of each line that was retrieved from /dev/lesekopf0.

Doesn´t work, i still get the whole string.

Aha! I just realized that you are using openhab-binding-exec1 under OH-1.x.x, not openhab-binding-exec1 under OH2.

I set up the following test under OH2 with the binding openhab-binding-exec and the OH2 (smarthome) regex transform.

/etc/openhab2/scripts/simulate-ehz-out:

#!/bin/bash

echo "1-0:1.8.0*255#9620.988#
1-0:2.8.0*255#42317.589#
1-0:1.8.1*255#9620.988#
1-0:2.8.1*255#42317.589#
1-0:16.7.0*255#-1.377#"

/etc/openhab2/things/testre.things:

Thing exec:command:testre [command="/etc/openhab2/scripts/simulate-ehz-out", transform="REGEX(s/(?m)^[^#]+#([^#]+)#$/$1/g)"]

/etc/openhab2/items/testre.items:

String TestRE_Output "[%s]" {channel="exec:command:testre:output"}

I then loaded openhab-binding-exec from the KARAF console:

feature:install openhab-binding-exec

I restarted openhab2 and looked in /var/log/openhab2/events.log, where I found the following log entries:

2017-02-02 13:21:10.252 [hingStatusInfoChangedEvent] - 'exec:command:testre' changed from UNINITIALIZED to INITIALIZING
2017-02-02 13:21:10.269 [hingStatusInfoChangedEvent] - 'exec:command:testre' changed from INITIALIZING to ONLINE
2017-02-02 13:21:10.314 [ItemStateChangedEvent     ] - TestRE_Output changed from NULL to 9620.988
42317.589
9620.988
42317.589
-1.377

I’m glad you asked for some help, @halloween. I had no idea that the smarthome REGEX transform supported the s/something/something else/ construct.