Does JS Transformation use as profile only work with String items?

I am using OpenHab 2.5.5
Do transform:JS profiles on items only work if the receiving item is a string and the incoming Channel delivers a string? I have been fiddling for the past hour and this seems to be the only combination that works.
The examples are all with String items, but I never saw it documented that it would not work with Number items. It would be quite convenient

I have as item:

String office_getter {channel="mqtt:topic:ec41f7fc:office_blinds_get" [profile="transform:JS", function="shellyin.js"]}

shellyin.js is:

(function (i) {
   return parseInt(100-i);
})(input)
  • I tried to omit the parseInt() or replace it with String(), no effect
  • When I change the item from “String” to “Number”, I get “NULL” as state for the string, no matter what I do
  • When I changed the mqtt channel from String to Number (which I had first), I get

Could not transform state ‘100’ with function ‘shellyin.js’ and format ‘%s’

  • adding 'sourceFormat=“%d” did not help either, in that case. It just changed the error from format ‘%s’ to format ‘%d’

Transformation services of any type only handle strings in and strings out.

When you call a transform, the service automatically converts the input parameter to a string.
Maybe it’s a channel state update, maybe it’s raw device data, maybe it’s a display state, depending on the context you are using the transform in.
Doesn’t matter, it gets delivered to the transform as a string.
(So you might have to parse it to a number inside the transform etc.)

When the transform returns a result, it is always converted to a string.
What happens next again depends on context. When used in a binding for example, the binding may want to use the return to feed a number channel update. The binding has to take care of parsing the returned string into the desired new number form.

The transform profile does not currently do anything with the return string. So it is only suitable as a state update for a String type Item.

ok, thanks for the quick response. So it’s clear then :wink: