Problem with converting from m/s to km/h

Hi there,

i have a Problem with the JS Transformation. I like to show my windspeed in km/h in my basic UI. I get it to show the value, but if i add km/h in the definition, it shows “NaN”.

Thing Definition

Type number         : Windspeed                       "Windgeschwindigkeit"                 [ga="9.005:<6/1/149"]

Item Definition

Number          Windgeschwindigkeit             "Windgeschwindigkeit [%.2f m/s]"                    <wind>          (Weather)               {channel="knx:device:bridge:wetterstation:Windspeed", autoupdate="false" }

Dashboard

Default     item=Windgeschwindigkeit label="Wind [JS(windspeed.js):%.2f km/h]"
Default     item=Windgeschwindigkeit label="Wind [JS(windspeed.js):%.2f]"
Default     item=Windgeschwindigkeit

windspeed.js

(function(i){

    return i*3.6;

})(input)

What i have to do to get a nice km/h value :slight_smile:

A state transformation used in a label always returns a string.
You cannot format a string with %.2f even if it looks like "2.35’, it is not a float.
Try
[JS(windspeed.js):%s km/h]

Changed it to (tried beforce)

Default     item=Windgeschwindigkeit label="Wind [JS(windspeed.js):%s km/h]"

Also gets me “NaN”. Also when i change the “Default” Item definition to “Text”

But now the NaN comes from your script.
The Item state is passed into the javascript as a string, too, so you’d need to parse that before doing maths.

return parseFloat(i) * 3.6;

1 Like

Thank you. The NaN is gone but km/h does not show up. And the Number doesnt get rounded…

Hi there,

i got a solution. Modified my JS Transition to

(function(i){
    kmh=(i*3.6).toFixed(2);
    return kmh.toString() +" km/h";
})(input)

Item Definition

Number			Windgeschwindigkeit             "Windgeschwindigkeit [%.2f m/s]"                    <wind>			(Weather)               {channel="knx:device:bridge:wetterstation:Windspeed", autoupdate="false" }

The Sitemap Definition is like

Text		item=Windgeschwindigkeit label="Wind [JS(windspeed.js):%2f]"

No, it’s up to you have your script do that.