SNMP and regex transformation from string to integer


Really enjoying OH3’s new interface and having quite a bit of fun integrating stuff and various devices.

My setup is as follows:
OH3 running on a virtual machine, version openHAB 3.0.2, Ubuntu 18.04.5

I have a QNAP nas in another VLAN and am using the SNMP binding to fetch the system temperature and other things. Works fine to get the values from the OIDs, but it is defined as a string in the MIB so the output I get is like this:

So it gives me for example “35 C\94 F”. But in order to graph this, I would need to extract the value in degrees celcius.

I’m a bit confused on the best way to achieve this. I thought simply changing the profile transformation of the item to REGEX and adding the expression ^(\d*) would be enough to extract the “35”. But when I do this and save the item, it is then empty. Using a regex tester, the regex is valid for that string:

If I do this (so I add a state transformation):

The item looks like this, so as if the regex did nothing:

What am I not understanding?

The binding channel has a fixed type - number, string, switch.
That naturally mates with the equivalent Item type.
Channel based transformations won’t change this type.

A trick you can use to accommodate the mis-match is to apply a transform profile to the link between the channel and the Item.
You should be able to use a Number:Temperature type Item if you see that your transform returns nicely formatted strings “38 °C” or “38.2 °C” or whatever.

Hey thanks for the tip.

I tried adding an item of type number:temperature (also tried with just number) to the channel, but I’m getting NULL:

I logged into the karaf console and 2 things are happening when I click on the linked item.
First, I get this error:

13:44:42.397 [ERROR] [.internal.JSONResponseExceptionMapper] - Unexpected exception occurred while processing REST request.
java.lang.NumberFormatException: Character   is neither a decimal digit number, decimal point, nor "e" notation exponential mark.

If I add a state transformation like %.1f additionally I get this:

13:44:56.341 [WARN ] [l.profiles.RegexTransformationProfile] - Could not transform state '38 C/100 F' with function '^(\d*)' and format '%.1f'

Ok from what I read this might be a binding limitation.

I’m going to go another route: a little php script that queries the qnap through snmp and builds a json output of whatever I need that I can then just read using JSONPATH which is much more convenient (at least for me). This way I don’t need to use regex expressions within an item.

This is what I did with my solar inverter.

Here’s what I came up with for anyone interested (you’ll need to install php-snmp):



I’ll just publish this to /var/www/html as something like qnapjson.json

And use an HTML thing to extract the data with JSONPATH.


38 C does not satisfy this.