OH3 "On the Fly" transform from SNMP Binding

  • Platform information:
    • OS: Windows 10
    • Java Runtime Environment: zulu 11.43.5
    • Openhab 3.0.0

Hi All

Looking for some advice as I have now upgraded to OH 3.0.

I use the SNMP Binding to read values from my UPS using SNMP OID, and example of a channel is as follows:

  - id: UPS_Batt_Voltage
    channelTypeUID: snmp:number
    label: UPS Batt Voltage
    description: null
    configuration:
      mode: READ
      datatype: INT32
      oid: .1.3.6.1.4.1.21111.1.1.3.5.0

The result from this is in this example 816, however the UPS Battery Voltage is actually 81.6V (not 816V). The same situation exist for most of the information retrieved via SNMP, needing either a /10 or /100 to correct. So I use a rule to divide the value by 10 as follows:

rule "UPS_Batt_Voltage"
when
        Item UPS_SNMP_UPSBattVoltage received update
then
{
        val trans = (UPS_SNMP_UPSBattVoltage.state as Number)/10
        UPS_SNMP_UPSBattVoltage_Fix.postUpdate(trans)
}
end

The result of this is that UPS_SNMP_UPSBattVoltage_Fix has a value of 81.60000000

There are two downsides to this that I would like to resolve, but have not been able to find a solution that I can implement as a non coder.

Firstly this requires a second Item so carry the “Fixed” value - I seem to recall reading somewhere that the value could be corrected on the fly, but I have not been able to find how to do this. I have searched TRANSFORM etc. with no luck.

Second is that the resultant value is something like 81.60000000, where it would be more meaningful to have 81.60 (limit to 2 decimal places).

Any advise on what to look at would be appreciated?
Mark

I’m also trying to avoid as much dummy items and rules as possible.

You were so close :wink: It’s possible where you link channel to item.
See my sample here: I’m transforming a number channel into a Contact. In your case JavaScript-Transform or something similar would be more fitting.

In an item-file it would look like the following:

Contact vGF_Hallway_SmartLock_LockState “Schloß ist” (gGF_Hallway_SmartLock) {widgetOrder=“10”, channel=“nuki:smartlock:bridge1:lock1:lockState” [profile=“transform:SCALE”, function=“nuki_lock.scale”,sourceFormat="%s"]}

see more here:

You second issues you can solve by %.2f (for 2 digits). Just go into the item and add Metadata “stateDescription”

// if you use item-files this was working and works in OH3, too
Number no_oh2 “Number OH2 [%.2f]” {channel="…"}

// If you want to import your item content via the developer function you must use this one here
Number no_oh3 “Number OH3” {channel="…", stateDescription=" “[ pattern=”%.2f" ] }

Hope that helps,
thefechner

@thefechner

Thank you so much for the pointer. So I will require a some JS to do my calculation. Will have to do some research on that. Thanks so much for the pointer.

Also added the Meta Data as you outlined and works great.

Sure I tried the JS option in 2.5 and couldn’t get it to work. But working 100% now using the script:

(function(i) {
    return parseFloat(i) / 10;
})(input)

Thanks for the assistance.