Convert Number to Number:<Unit>

  • Platform information:
    • Hardware: Docker
    • OS: Debian Image
    • openHAB version: 3.0.0

I’m migrating my system atm to OH3.0 and so finally decided to also give UoM a try during this migration. I was always a little worried about the unit handling when it comes to rules.

But now I’m struggling at a different step with UoM:
When there is a Binding/Thing providing just a normal Number channel (in my case it’s the SNMP binding which offers just String/Number/Switch channels) is there some possibility to “add” a unit to this number and save it as a Number: item (here again I try to use Number:DataAmount) to make use of unit conversions later on?

My idea was to define the item as Number:DataAmount and use a JS transformation on thing-level to add the unit.
But I couldn’t get it working as I have no idea what’s the syntax of a QuantityType in Javascript. Just tried the syntax like in Rules DSL w/o any success. Would this be possible at all? Do I need an import? Any other solution to add the unit?

ram.js

(function(raw){
    output = raw|"B";
    return output;
})(input)

Would something like this work? ModBus Binding: convert raw 'Number' register to a 'Number:Energy' Item? - #10 by AndrewFG

Although there might be problems on general with bytes as unit, see Problems with UnitOfMeasurement DataAmount Byte - #16 by schmidmuc.
and I can't get prefixes to work with DataAmount (UoM) - #10 by mva_one

Out of curiosity - why not making a channel configurable with unit? Obviously it would permit only reading of numeric values, but it would solve some of troubles related to lack of unit information coming from modbus poll. (this is more on development side of things)

1 Like

Doh!
Sometimes things are too easy …

Thanks :ok_hand:

@splatch Thanks for the comment.

In fact that is what I have come up with, but using profiles

The profile also supports offset and gain correction, often at least gain is used with modbus.

This makes the channels fully UoM aware, even accepting quantity commands, with units converted and descaled appropriately for writing the data back to integer.