[SOLVED] Item calculation m/s to km/h

Quick Shot!

I got a weather station which reports in meters per second. But I want kilometers per hours.
I know the calculation is value * 3.6
Do I need to create a rule for this?
Or is it possible to do that inside of an item?

I summon @Dim :smiley:

1 Like

You can use Java Script transformation in item definition if you want to keep it simple (without rules):

Number WindSpeed "Wind speed [JS(windpseed.js):%d km/h]" ...

and the corresponding windspeed.js:

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

Nice. Be aware that this will transform the presentation, not the stored value.

Thant is exactly what I want. Easy on the eye but fully documented.

I will try that

@Mihai_Badea
Sadly my openHAB throws an error

16:00:11.997 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - transformation throws exception [transformation=org.eclipse.smarthome.transform.javascript.internal.JavaScriptTransformationService@6e5cb2b6, value=1.89999997615814208984375]
org.eclipse.smarthome.core.transform.TransformationException: An error occurred while loading script.

EDIT

:crazy_face:
My mistake I named the file windspeed.js and used the file windpseed.js
Thank you very much @Mihai_Badea

(function(i){
        return (i*3.6).toFixed(2);
})(input)

Added the .toFixed(2); because I noticed that it does not trim the digits anymore when using

Number WindSpeed "Wind speed [JS(windpseed.js):%.2f km/h]"
2 Likes

EDIT: Never mind. SCALE doesn’t work like I thought.

For the record, I think the SCALE transform will work too since it is just a simple multiplication.

1 Like

How would you use the SCALE transformation with my problem? Could give an example ?

I’ve not used it much myself but I think is would be something like … well crap. It doesn’t work the way I thought it did at all.

Sorry, my bad. It won’t work.

I did notice that the docs for the transform are pretty inadequate. I had to go to the Migration Tutorial and wiki to figure out how it works.

2 Likes

Next time you have that thought, why not create an issue. Wait, didn’t I… :smiley:

@BrutalBirdie did you check the JS transformation README? Do you think it could also benefit from some improvements? http://docs.openhab.org/addons/transformations/javascript/readme.html

The README?
Yea it could benefit from some improvments. Many people use openHAB for the first time and never dealt with java script or anny other programming language bevor. So some good examples and more explanation would help users I think.

To be honest the question was platonic and I wanted to tickle your willingness to help with that improvement :laughing: Do you think that’s something you want to spend some time on? Check out these recent developments triggered by this very thread of yours:

I knew that very well @ThomDietrich . I am allready working on the text with my friend who is more familiar with javascript.

2 Likes