[SOLVED] Basic JS script error

I have a very basic jscript to convert minutes to days. But I can’t get it to work.
Number EmonPi_Cpu_Uptime “EmonPi Uptime [JS(mintodays.js):%.1f Dagen]” icon=“clock-on”

This JS works OK:
(function(i) {
var d = i;
return d;

But this doesn’t, it returns NAN in the sitemap.
(function(i) {
var d = i / 1440;
return d;

I tried as well the following:
return Number(d);
return d.valueOf(d);

What is wrong, I really have no clue. And I can’t make the script smaller then it already is.

I can’t help too much but I can say that NAN stands for “Not A Number”. So what is happening is that the JS doesn’t know how to convert the date of the Number Item to something it can do math with.

Having never used JS transforms on an Item’s label I can’t offer any further help.

What if you do: var d = parseInt(i) / 1440 ?

No kidding, parseInt() did the trick. But why ???
Working fine:
return straight from the input i is OK
when I assign i to d, return is OK as well.
even i = 123.456789 / 123 and return i: OK !!!
It’s starts to go wrong only when i is used in the division.

For JS, the type of variable should still be the same as i is float. It should make no difference at all to do the calculation. Or are there too many digits? This should make no difference, and I have no clue how to reduce them without going to a string. I guess there’s a special way OpenHab sends the value to JS.
And what to do when you want the value in as a float and not an integer?

Anyway, a lot of thanks for your help. This is the solution.

Probably because the state of the Item is passed as a String to the JavaScript.

But the state of an Item isn’t a float. It’s a State. In the case of Number Items the State is a DecimalType which can easily be converted to a float, but it must decidedly is not a float.

I suspect that in order to make the JS transform work for all Item types, it is passed to the JS as a String so if you want to do math with it you need to parse the String to a numerical type in the transform.


JS returns a string, not a value !
Therefor I had to do the formatting and addition of text in JS, not in the definition of the sitemap. I didn’t expect that, but it’s working for 100% now.

Together with the post before, it seems clear that OpenHab and JS share strings. Knowing that will make future development a lot easier

Thanks a lot.