I can't get prefixes to work with DataAmount (UoM)

Tags: #<Tag:0x00007f616d8343e0> #<Tag:0x00007f616d8342c8> #<Tag:0x00007f616d8341b0>

Hello,
for many hours I tried to get the UoM (Units of Measurement) system to work properly. Now I’m almost there but one problem remains: If I use the Number:DataAmount as item with B (Byte) as the unit, it seems not possible to convert that to any other unit with Decimal or Binary Prefixes listed here: Units of Measurement
If I add any prefixes to the Unit in my sitemap (e.g. MB, GiB, …) it doesn’t change the number as expected, it just shows the same number with the new unit, what is obviously wrong.
Though I got it to work just like this with other Units (DataTransferRate, Length), with DataAmount it just does not work.
I could not find any solution to this very specific problem by googling or searching in this forums.
I’ll include the relevant parts of my items, and sitemap files below along with a screenshot.

This is my first forum post (after reading many other), so please dont’t be mad if i did something wrong…

Thanks in advance for your help!
Marcell

Platform information:
Hardware: Raspberry Pi 3B / ARM
Kernel/OS: Linux openhab2 4.19.118-v7+
Java Runtime Environment: OpenJDK RE Zulu 8.46.0.225-CA-linux_aarch32hf
openHAB version: 2.5.6 (Build)

My items:

Group g_snmp_nas "NAS2104 (SNMP)"
String             nas_sysname  "Systemname [%s]"     (g_snmp_nas) { channel="snmp:target:nas2104:sysName" }
Number:Temperature nas_cputemp  "CPU-Temp. [%d °C]"   (g_snmp_nas) { channel="snmp:target:nas2104:cpuTemp" }
Number:DataAmount  nas_freeram  "RAM frei [%.0f B]"   (g_snmp_nas) { channel="snmp:target:nas2104:freeMem" }
Number:DataAmount  nas_totalram "RAM gesamt [%.0f B]" (g_snmp_nas) { channel="snmp:target:nas2104:totalMem" }

My sitemap (snippet):

Text item=nas_sysname
Text item=nas_cputemp  label="CPU-Temp. [%.0f °F]"
Text item=nas_freeram  label="RAM frei [%.0f MB]"
Text item=nas_totalram label="RAM gesamt [%.0f GiB]"

Please note that the conversion from °C to °F (for testing purposes) works just fine!

Here’s a screenshot how this sitemap looks like in the OpenHAB Android App:


Obviously the values aren’t correctly converted from Bytes to MegaBytes or GibiBytes… The values shown in the sitemap are the raw values reported by the Binding (SNMP) in Bytes, no matter how I try to configure .items or .sitemap

What happens if you use more conventional English abbreviations, like Mb , Gb ?

I tried it right now. It’s the same problem - just shows Mb instead of MB then. The number still does not get converted.

It wouldn’t change until an update takes place. What does your events.log tell you is going on, with the actual Item states?

Here’s a small snippet from the events.log.
I just posted lines which actually refer to the nas_ items.
There aren’t any other errors or warnings…

2020-07-05 11:36:58.301 [vent.ItemStateChangedEvent] - nas_freeram changed from 8838252.0 o to 8839444.0 o
2020-07-05 11:36:58.305 [vent.ItemStateChangedEvent] - nas_cputemp changed from 71.0 °C to 70.0 °C
2020-07-05 11:37:58.301 [vent.ItemStateChangedEvent] - nas_freeram changed from 8839444.0 o to 8836652.0 o
2020-07-05 11:37:58.305 [vent.ItemStateChangedEvent] - nas_cputemp changed from 70.0 °C to 67.0 °C
2020-07-05 11:38:58.293 [vent.ItemStateChangedEvent] - nas_freeram changed from 8836652.0 o to 8838520.0 o
2020-07-05 11:39:58.275 [vent.ItemStateChangedEvent] - nas_freeram changed from 8838520.0 o to 8840012.0 o
2020-07-05 11:39:58.287 [vent.ItemStateChangedEvent] - nas_cputemp changed from 67.0 °C to 69.0 °C

Okay, whatever unit an “o” is, it doesn’t seem to be one of the data measures you expect.

Does the SNMP binding support UoM? I didn’t think it did. The channels seem to be plain Number types.

It’s really strange, because the temperature reading (also from SNMP) works just fine with the UoM system. It is received as a plain number (value in °C) and I can assign the unit °C to it in the item definition with [%d °C] or [%.0f °C]. If I want to see the Temperature in °F, it works just as expected by converting the value in the sitemap with [%.0f °F] (as shown above).

And I think the o means octet, which should be the same as B (Byte). It is also a valid Unit for DataAmount according to the List of Units
But setting o as the unit in the item and trying to convert to Mo oder Go in the sitemap doesn’t work either.

Here’s my things file with the channels of the SNMP Binding:

Thing snmp:target:nas2104 "NAS2104" [ hostname="192.168.3.3", protocol="v2c", community="public" ] {
    Channels:
        Type string : sysName           [ oid="1.3.6.1.2.1.1.5.0", mode="READ" ]
        Type number : cpuTemp           [ oid="1.3.6.1.4.1.24681.1.3.5.0", mode="READ" ]
        Type number : sysTemp           [ oid="1.3.6.1.4.1.24681.1.3.6.0", mode="READ" ]
        Type string : modelName         [ oid="1.3.6.1.4.1.24681.1.3.12.0", mode="READ" ]
        Type number : freeMem           [ oid="1.3.6.1.4.1.24681.1.3.3.0", mode="READ" ]
        Type number : totalMem          [ oid="1.3.6.1.4.1.24681.1.3.2.0", mode="READ" ]
        Type number : runningProcesses  [ oid="1.3.6.1.2.1.25.1.6.0", mode="READ" ]
}

With a “real” quantity type UoM channel, a value received in F or K would be converted to the desired C.
This is just a number type channel bolted to a Number:Temperature Item. It’s not real UoM. It’s really a number plus a string.

I think this what you are trying to achieve with your Mb etc.? It ain’t ever going to work.

With a Number type Item, again these are just strings added for display purposes.

The UoM framework can also use those as defaults for selecting units. You’re making a hybrid with Number:QuantityType Items and number-only channels. I’m surprised it works as well as it appears with the temperature, but unsurprised it makes a mess of the data type. I suspect you are getting system default units - C and mystery “o”

It would be great if there were a channel profile to link plain channel with quantity type by properly adding a unit - but there isn’t, yet.

You could ask for a binding enhancement to support UoM types. So far as I can see, that would not be a good idea - the raw data arrives with no units? So it would all be device dependent and you’d be asking the binding to add umm, “fictional” units, which I doubt would attract support.

This binding does not support transformations, so I think you are stuck with proxy Item and rules. Link your raw snmp data to a plain Number Item, trigger a rule on change that scales and adds units as you wish and updates a Quantity Type Item.

Ah - I’ve found that “o”, for octet !

The Data arrives with no units, just as a plain number.

It is indeed a very complicated system, at least for me…

I tried one more thing: in the items I set the unit to “o”, like this:

Number:DataAmount nas_freeram  "RAM frei [%.0f o]"   (g_snmp_nas) { channel="snmp:target:nas2104:freeMem" }
Number:DataAmount nas_totalram "RAM gesamt [%.0f o]" (g_snmp_nas) { channel="snmp:target:nas2104:totalMem" }

In the sitemap, I now tried to use the conversion to Mio or Gio!

Text item=nas_freeram  label="RAM frei [%.2f Mio]"
Text item=nas_totalram label="RAM gesamt [%.2f Gio]"

…and guess what: it works! :partying_face: Now the values get converted with the binary prefixes.
Although I initially wanted to show the values in MB or GB, I now gave up on that an will go with the (for me) unusual format. At least it’s not shown in single Bytes any more.

Anyway, thank you @rossko57 for your support!

Here I’d like to present another workaround for others who maybe have the same problem and are reading this.
The principle is to store the raw value as a plain Number item and convert it via the JS transformation to the desired “shape & format”.

In my very specific usecase it looks like this:
Items:

Number nas_freeram  "RAM frei [%.0f]"   (g_snmp_nas) { channel="snmp:target:nas2104:freeMem" }
Number nas_totalram "RAM gesamt [%.0f]" (g_snmp_nas) { channel="snmp:target:nas2104:totalMem" }

Sitemap:

Text item=nas_freeram  label="RAM frei [JS(BtoGB.js):%s]"
Text item=nas_totalram label="RAM gesamt [JS(BtoGB.js):%s]"

The JS transformation is called BtoGB.js (Bytes to Gigabytes) and is stored in the transform folder:

(function(i) {
    var result = ((parseInt(i)  / (Math.pow(1000,3))).toFixed(1) + " GB")
    return result;
})(input)

Now the sitemap for example converts the “raw” item state 8785828471 to 8.8 GB which is then shown in the sitemap.

Hope this maybe helps somebody! :slight_smile:

1 Like