[SOLVED] Simple mins to hours transform with label

Tags: #<Tag:0x00007fd30f82c438>

(Andrew Pawelski) #1

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?

(Kjetil Asdal) #2

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…

(Udo Hartmann) #3

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

(Andrew Pawelski) #4

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

(Kjetil Asdal) #5

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!!

(Udo Hartmann) #6

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.

(Andrew Pawelski) #7

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);

(Andrew Pawelski) #8

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";

(Miika Jukka) #9

Try this:

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

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..." }


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

(StegemannK) #10

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:

(Sascha_S) #11

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";

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