Item value JS transformation error


I’m trying to divide KNX item value by 10:


Number L1_v "L1_V [JS(divide10.js):%.1f] " <energy>  { channel="knx:device:bridge:ZennioKes:L1_v" }

but I’m getting the following error from log:

2019-02-09 12:17:27.902 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '2219.52' on item 'L1_v' with pattern 'JS(divide10.js):%.1f': Cannot format state '2219.52' to format '%.1f'

any ideas what could be wrong?

The JS transform will return a string. I wonder if the formatter is clever enough to parse that into a number before trying to format.

Should be able to do the format in your JS

not the best news for me as I’m not very strong on coding.
I’m currently using the following JS transformation to convert some of my modbus values:

// Wrap everything in a function (no global variable pollution)
// variable "input" contains data passed by openhab
(function(inputData) {
    // on read: the polled number as string
    // on write: openHAB command as string
    var DIVIDE_BY = 10;
    return parseFloat(inputData) / DIVIDE_BY;
(function(inputData) {
    // on read: the polled number as string
    // on write: openHAB command as string
    var DIVIDE_BY = 10;
    var result = parseFloat(inputData) / DIVIDE_BY;
    return result.toString();

I replaced the code, but I’m getting the same error.

2019-02-09 13:12:16.751 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '2219.52' on item 'L1_v' with pattern 'JS(divide10.js):%.s': Cannot format state '2219.52' to format '%.s'

I tried with “:%.s” as well as it is now string but still.

%s no dot required

I just noticed and removed that. I don’t get any error now, but it doesn’t convert the value either.

Ok let’s try something completely different.
Let’s use a profile:

Number L1_v "L1_V [%.1f] " <energy>  { channel="knx:device:bridge:ZennioKes:L1_v"[profile="transform:JS", function="divide10.js"] }

it broke the item altogether

What OH version are you running?

latest stable 2.4.0

without the part [profile=“transform:JS”, function=“divide10.js”] Item reads its old value.
With it, it doesn’t even show up on the bus (log). No errors either.

Let’s go back a step.

Can you be clear about whether you’d like to store the divided-by-10 value in the Item ? (rather than just for display in the UI)

As your other (modbus) items are stored that way, it would make sense if this KNX Item was too.
It’s a shame KNX2 doesn’t support transforms as well, but a profile should do a similar job for reading a sensor.

Number L1_v "L1_V [%s] " <energy> { channel="knx:device:bridge:ZennioKes:L1_v"[profile="transform:JS", function="divide10.js"] }

really should work. Be sure you’ve not got any funny quote marks.
Is the KNX channel a number type?

Yes, I would like to store the divided-by-10 value, not just to use it the UI.
I don’t see any typos nor funny quote marks.

And the new code still didn’t work.


Thing device ZennioKes [
    ] {
        Type number        : L1_mA        "L1_mA"       [ ga="3/2/1" ]
        Type number        : L2_mA        "L2_mA"       [ ga="3/2/2" ]
        Type number        : L3_mA        "L3_mA"       [ ga="3/2/3" ]
        Type number        : L1_v         "L1_V"        [ ga="3/1/1" ]
        Type number        : L2_v         "L2_V"        [ ga="3/1/2" ]
        Type number        : L3_v         "L3_V"        [ ga="3/1/3" ]
        Type number        : L1_w         "L1_W"        [ ga="3/3/1" ]
        Type number        : L2_w         "L2_W"        [ ga="3/3/2" ]
        Type number        : L3_w         "L3_W"        [ ga="3/3/3" ]

        Type number        : Hz           "Hz"          [ ga="3/0/1" ]


//ZennioKes 1.1.8
Number Hz "Hz [%.1f Hz]" <energy> { channel="knx:device:bridge:ZennioKes:Hz" }

Number L1_mA "L1_mA [%.1f mA]" <energy>  { channel="knx:device:bridge:ZennioKes:L1_mA" }
//Number L1_v "L1_V [JS(divide10.js):%s]" <energy>  { channel="knx:device:bridge:ZennioKes:L1_v" }
//Number L1_v "L1_V [%.1f] " <energy>  { channel="knx:device:bridge:ZennioKes:L1_v" }
//Number L1_v "L1_V [%s] " <energy> { channel="knx:device:bridge:ZennioKes:L1_v" [profile="transform:JS", function="divide10.js"] }
Number L1_v "L1_V [%s]" <energy> { channel="knx:device:bridge:ZennioKes:L1_v"[profile="transform:JS", function="divide10.js"] }
Number L1_w "L1_W [%.1f W]" <gauge>  { channel="knx:device:bridge:ZennioKes:L1_w" }

Number L2_mA "L2_mA [%.1f mA]" <energy> { channel="knx:device:bridge:ZennioKes:L2_mA" }
Number L2_v "L2_V [%.1f mV]" <energy> { channel="knx:device:bridge:ZennioKes:L2_v" }
Number L2_w "Töökoja elekter [%.1f W]" <gauge> { channel="knx:device:bridge:ZennioKes:L2_w" }

Number L3_mA "L3_mA [%.1f mA]" <energy> { channel="knx:device:bridge:ZennioKes:L3_mA" }
Number L3_v "L3_V [%.1f mV]" <energy> { channel="knx:device:bridge:ZennioKes:L3_v" }
Number L3_w "Serveri elekter [%.1f W]" <gauge> { channel="knx:device:bridge:ZennioKes:L3_w" }

so it is a number type…


I think you have run into the same problem as here