[SOLVED] Simple mins to hours transform with label

transformations
Tags: #<Tag:0x00007f6cef009318>

(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);
})(input)

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

(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: