Help needed : using REGEX in rules

Hi,

I’ve got from a daemon this output (var StatusOutput):

********************************
*                              *
* S.USV pi solutions           *
* www.s-usv.de                 *
*                              *
* Model: Basic                 *
* Firmware Version: 2.32       *
* Software Version: 2.20       *
*                              *
* Mail notification: Disabled  *
*                              *
* Timed Boot: Disabled         *
* Boot time: 00:00:00          *
*                              *
* Timed Shutdown: Disabled     *
* Shutdown time: 00:00:00      *
*                              *
* Mon Jul 31 14:39:06 2017     *
*                              *
********************************
*                              *
* Powering Source: Primary     *
* Charging circuit: ONLINE     *
* Charging current: 300 mA     *
*                              *
* Voltage in: 5.00 V           *
* Battery capacity: 71.39%     *
* Battery voltage: 4.03V       *
* Power Battery: 000.00 mA     *
* Power Extern: n/a            *
*                              *
* Shutdown timer: -1           *
* Autostart:  enabled          *
* Sleep timer: 1               *
*                              *
********************************

Now In wan’t to build a rule. The rule should post some values to openhab2, e.g. PoweringSource, MainVoltage etc.
Which REGEX I should use to get these values? I got much errors ;(

var float MainVoltage = transform("REGEX", "* Voltage in:((?:\d*\.)?\d+)V", StatusOutput)

Or exists a binding for SUSV for PI?

thx
Heiko

One thing to keep in mind with regular expressions is that * is a special character and must be escaped.

So I think to extract the voltage you would need something like:

.*\* Voltage in: ([\d]+\.[\d]+) V.*

The errors and potential errors I see include:

  • a .* to consume everything up to the line you want
  • escaping the * that starts the line
  • missing the space between “in:” and the number
  • I’m not sure the double set of parens does what you need it to and I don’t understand the ?: part at all
  • missing the space between the end of the number and the “V”
  • missing a .* to consume the rest of the line.

There are dozens of online regex checkers that let you paste in your text and build up and test your regular expression in the web page. I highly recommend using one.

([\d]+.[\d]+)

this causes an error ;(

Try this one:

.* Voltage in: (\d+(?:.\d+)?) V.*.*

I’m using Kodos, it’s of age, but still useful.

http://kodos.sourceforge.net/

I’ve found my solution

.* Voltage in: ([0-9.]+) V.*

With the \d oh2 have a problem. This part will cause an exception.

thx @all

I did some more tests with that regex by sending your message via MQTT and processing it with REGEX:

Number TestRegex "Wert [%.1f]" <chart> (Test) {mqtt="<[mosquitto:testitem:state:REGEX(.*Voltage in.*(\\d+\\.\\d+) V.*)]"}

It works by using double backslash.

PS: I get errors when using ‘:’ as part of the expression. Might be a parsing problem with openhab.

Thank you.
My solution work’s stable since few days.