Regex and curly bracket

Hey experts,

my router exposes some stats, i would like to use openhab (OH 3) to fetch some of them.
i would like to use the http binding tp get a raw file

content:
e.g.

node_network_transmit_packets_total{device=“wlan1”} 1266639

i tried with state transformation:
regex:node_network_transmit_bytes_total{device="br-lan"}([0-9])

i do not get a value back

ho do i need to define the thing?

UID: http:url:606efb2c2a
label: HTTP URL Thing AP5320
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://10.100.10.215:9100
  delay: 0
  stateMethod: GET
  refresh: 2
  commandMethod: GET
  contentType: text/plain
  timeout: 3000
  bufferSize: 2048
channels:
  - id: aaaa
    channelTypeUID: http:string
    label: aaaa
    description: ""
    configuration:
      mode: READONLY
      stateExtension: /metrics
      stateTransformation: regex:node_network_transmit_bytes_total\{device=\"br-lan\"\}[ ]*([0-9]*)

any hints are welcome

thanks a lot!!!

Lars

Nope.

First things first: it’s REGEX, not regex. openHAB is case sensitive in almost every scope.

Another thing: you’ll have to use .* at the beginning and the end of each regular expression.
Don’t use ^ (beginning of line) and $ (end of line) in openHAB.
The regular expression always has to match the whole string.

So maybe this is more appropriate:

stateTransformation: REGEX:.*node_network_transmit_bytes_total\{device=\"br-lan\"\} *(\d+).*

You don’t need to set square brackets around a space.
Please be aware that you better also chose the first char after the last digit (maybe another space?).

Short test:

rule "regex test"
 when
    Time cron "5/10 * * * * ?"
 then
    var theString = "this is node_network_transmit_bytes_total{device=\"br-lan\"} 123478 Bytes"
    var regex = ".*node_network_transmit_bytes_total\\{device=\\\"br-lan\\\"\\} *(\\d+).*"
    logInfo("regex","Source: {} REGEX: {} Result: {}", theString, regex, transform("REGEX",regex,theString))
end

As I use variables, I have to double the escape symbol, but you can see the resulting strings in the log:

2023-10-20 10:04:35.533 [INFO ] [org.openhab.core.model.script.regex ] - Source: this is node_network_transmit_bytes_total{device="br-lan"} 123478 Bytes REGEX: .*node_network_transmit_bytes_total\{device=\"br-lan\"\} *(\d+).* Result: 123478

thanks a lot!