[SOLVED] Simple mins to hours transform with label

I am trying to do a simple convert mins to hours from a number item by:

Number consumableSensorT    "Sensor [JS(mins2hours.js):%.1f] Hours"

JS file is:

(input / 60).toFixed(1);

but with the label hours on the end I get a NaN

anyone any ideas?

I seem to remember being in a similar situation before, and as I remember, the solution was to move “the last part of the string” inside the square brackets like this:

Number consumableSensorT    "Sensor [JS(mins2hours.js):%.1f Hours]"

I don’t think it makes much sense, and it is certainly not intutive, however, it was the only way I could make it work…

Yes, it does make sense, all text in [] is aligned right, ‘Hours’ is the unit for the number and belongs to it.

Hi what I thought I posted was in the square brackets unfortunately I was trying everything and the after bracket was the last thing I tried and that’s what I posted

Inside the brackets gives me a NaN. To the right gives me an Err and based on @Udo_Hartmann comment the Err makes sense as it isn’t valid syntax.

It’s almost as if the transform is trying to transform everything after it

Ah! That piece of information makes it a whole lot more sensible, indeed! I have always thought the square brackets were a special way to indicate something to be replaced when rendering the sitemap. Understanding that it simply means to right-adjust whatever text is inside clears it us nicely. Thanks!!

Regarding to http://docs.openhab.org/addons/transformations/javascript/readme.html#example your Java Script is wrong. But to be honest, I do not use JS-Transformation, so this is only a guess.

OK I tried this:

Number consumableMainT    "Main Brush [JS(mins2hours.js):%1.0f hrs]"

and get NaN still

with this as the transform:

(function(i) {  
    return (i / 60).toFixed(1);
})(input)

took a wild guess and tried to add the suffix in the transform - this works even tough its returning a string value to a Number item…

(input / 60).toFixed(1).toString() + " hrs";

Try this:

(function(minutes) {
    return Math.floor(minutes / 60);
})(input)

The floor() method rounds a number downwards to the nearest integer, and returns the result.

And if you want to show minutes also here’s a script that i use to transform daylight lenght to minutes + hrs. It’s a number item but use “%s” as shown.

Number   astroDaylight      "Daylight [JS(daylight.js):%s]" { channel="astroblahblah..." }

daylight.js

(function(minutes) {
    var hours   = Math.floor(minutes / 60);
    minutes    -= Math.floor(hours * 60);
    
    if (hours <= 0) {
    	return "00:" + minutes + " hrs";
    	}
    return hours + ":" + minutes + " hrs";
})(input)
1 Like

brillant Miika, thanks a lot!

It just took me some time to figure out that the script “daylight.js” goes into the transform section :slight_smile:

1 Like

Hey,
I too was fiddeling around with time and I came up with this solution:

Problem: System Uptime is stated in Minutes, which doesn’t look too nice.
So I needed to convert

657,6 min

Into something more readable.

Number CPU_Uptime            "Betriebsdauer [JS(mins2hours.js):%s]"   (System)    { channel="systeminfo:computer:openhab:cpu#uptime" }

The mins2hours.js in Transformation folder looks like this:

(function(minutes) {
    var hours   = Math.floor(minutes / 60);
    var min = Math.floor(minutes % 60);
    
    if (hours <= 0) {
    	return "00h " + min + " min";
    	}
    return hours + "h " + min + " min";
})(input)


Works best for me. The given solution sometimes returns endless numbers after the comma…