Problem with toItemScript

Hello.

In Openhab 4, I have the following item configuration:

Number		Windgeschwindigkeit		"Windgeschwindigkeit [%1.f km/h]"   { channel="knx:device:bridge:sensors:Windgeschwindigkeit"[profile="transform:JS", toItemScript="ms_to_kmhval.js", toHandlerScript="kmh_to_msval.js"] }

The intent is to use the script ms_to_kmhval.js to convert the meter/second value that comes from the KNX bus to km/h.

The respective transform script is this:

(function(i) {
return i*3.6;
})(input)

The problem is now that if I configure things that way then the wind speed no longer updated, updates from the bus do not reach the item. If I remove the scripts, instead using the following, then updates work as expected (after a restart of OH):

Number		Windgeschwindigkeit		"Windgeschwindigkeit [%1.f m/s]"   { channel="knx:device:bridge:sensors:Windgeschwindigkeit" }

Do you have any idea what could go wrong there?

knx Binding is unit aware since OH4.0 (maybe since OH3.4), so it’s way easier to do it the UoM way.

Type number : windspeed "Windgeschwindigkeit" [ ga="9.005:1/2/3"  ] // which DPT is used?
Type number : windspeed "Windgeschwindigkeit" [ ga="14.065:1/2/3" ] // which DPT is used?

The Item:

Number:Speed windSpeed "Windgeschwindigkeit" { channel="knx:device:bridge:sensors:windspeed", unit="m/s", stateDescription=""[pattern="%.1f km/h"] }

Of course you can set set unit to km/h as well (but you can also use knots, without any calculation)

I’m not sure if that answers his question about why toItemScript doesn’t seem to work.

Setting aside for the moment whether to use units vs transforms. I have a regular Number type (no units) from a binding holding a temperature value that I want to convert from C to F.

Number OutTempX “Temperature [%.1f °F]” { channel=“fineoffsetweatherstation:gateway:4368657686:temperature-outdoor” [profile=“transform:JS”, toItemScript=“C2F.js”]}

Where C2F.js is

(function(x) {
return 5;
})(input)

So I’de expect the Item to be “5”, but it just ignores the transformation.
If instead I do something like

Number OutTempX “Temperature [JS(C2F.js):%.1f]” { channel=“fineoffsetweatherstation:gateway:4368657686:temperature-outdoor” }

Then the item displays correctly, so I know the script works.

Given the official documentation

the channels do use QuantityType, so please don’t mess with JavaScript but use UoM

Number:Temperature OutTempX "Temperature" { channel="fineoffsetweatherstation:gateway:4368657686:temperature-outdoor", unit="°F", stateDescription=""[pattern="%.1f °F"] }

Thanks a lot Udo, this looks great! I now updated this to:

Type number     : Windgeschwindigkeit   "Windgeschwindigkeit"    [ ga="9.005:0/0/20" ]

and:

Number:Speed		Windgeschwindigkeit		"Windgeschwindigkeit"   { channel="knx:device:bridge:sensors:Windgeschwindigkeit", unit="m/s" , stateDescription=""[pattern="%.1f km/h"] }

But in the UI, I now see a value UNDEF for the item. So something still seems to be at odds…

By the way: what is this weird syntax with “” in the stateDescription? Why two quotation marks?

That’s an error and should be corrected.

The forum likes to make our posts “pretty” and it replaces normal double quotes with these fancy reversy quotes.

Because @billfor is using quotes instead of code fences, the original characters are not preserved and the quotes are being replaced. Always use normal double quotes in your configs and pay close attention to the difference between block quotes:

This is a block quote.
A block quote looks like this

This is a code fence
Code fences look like this 

How to create:

> block quote


```
code goes here
```

In fact, you could setup a state Description, too (but it’s not used…) You can see it through the API Explorer:

{
  "link": "<some link to an item>",
  "state": "22 °C",
  "stateDescription": {
    "pattern": "%.1f °C",
    "readOnly": false,
    "options": []
  },
  "unitSymbol": "°C",
  "metadata": {
    "unit": {
      "value": "°C"
    },
    "semantics": {
      "value": "Point_Setpoint",
      "config": {
        "relatesTo": "Property_Temperature",
        "hasLocation": "<some location>"
      }
    },
    "stateDescription": {
      "value": "",               <==== this is the double quote
      "config": {
        "pattern": "%.1f °C"
      }
    }
  },
  "editable": false,
  "type": "Number:Temperature",
  "name": "<Itemname>",
  "label": "Item Label",
  "category": "temperature",
  "tags": [
    "Temperature",
    "Setpoint"
  ],
  "groupNames": [
    "<some group",
    "another group>"
  ]
}

If not setting the double quotes, you can’t set the child parameters (at least this was true for OH3.x) via text file. And of course you can see it in the metadata listing (here: Not Set)

I am even more confused now. So the double quotes are correct after all?

Well, at least for now you have to set the empty string when configuring the stateDescription pattern (openHAB states an error when not using the quotes).

Number:Temperature OutTempX "Temperature" { channel="...", unit="°C", stateDescription=[pattern="%.1f °C"] }
// openHAB will mark that as an configuration error                              here ^^
Number:Temperature OutTempX "Temperature" { channel="...", unit="°C", stateDescription=""[pattern="%.1f °C"] }
// openHAB will use this configuration without complaining

Okay the syntax looks alright because in the UI it now looks like this:

Yet, the item still shows UNDEF:

If I remove the stateDescription part then it correctly shows the m/s value.

That’s strange behavior, works for me (at least when sending some values directly to the Item. I do not have a knx wind sensor.

It’s really weird. I also tried setting this through the UI, creating a new Item of type Number:Speed, but as soon as I connect it with the proper channel, the value changes to UNDEF, no matter whether a state description is even present. Maybe something is wrong with my particular Version of OH. I am using version 4.0.1.