Javascript transform example with system uptime

I thought I would share a bit.
I want to point out that I was not aware of the work already done by @rtvb regarding the same problem I encountered.

The System Binding gives access to the CPU uptime in minutes. I like this value because it gives me a measure of how stable the system is.

I wanted to have a more readable format for the value. For instance 2365 minutes doesn’t mean much but 1 day 19 hours 25 minutes is more ‘human’.

So I made a Javascript transform file named CPUTime.js that need to be placed in the transform folder.

Note that you will need the javascript transform installed!

/*
Javascript transform function to change the number
of minutes of CPU time from the System Info Binding
into a more readable format
eg: 2365 into '1 day 15 hours 25 minutes

The item in the items file is defined as follow:
Number LocalComputer_Cpu_SystemUptime "[JS(CPUTime.js):%s]"
and linked via PaperUI to the System uptime channel
of the System Info Thing
*/

(function(i) {
    var val = parseInt(i); // The value sent by OH is a string so we parse into an integer
    var days = 0; // Initialise variables
    var hours = 0;
    var minutes = 0;
    if (val >= 1440) { // 1440 minutes in a days
        days = Math.floor(val / 1440); // Number of days
        val = val - (days * 1440); // Remove days from val
    }
    if (val >= 60) { // 60 minutes in an hour
       hours = Math.floor(val /60); // Number of hours
        val = val - (hours * 60); // Remove hours from val
    }
    minutes = Math.floor(val); // Number of minutes

    var stringDays = ''; // Initialse string variables
    var stringHours = '';
    var stringMinutes = '';
    if (days === 1) {
        stringDays = '1 day '; // Only 1 day so no 's'
    } else if (days > 1) {
        stringDays = days + ' days '; // More than 1 day so 's'
    } // If days = 0 then stringDays remains ''

    if (hours === 1) {
        stringHours = '1 hour '; // Only 1 hour so no 's'
    } else if (hours > 1) {
        stringHours = hours + ' hours '; // More than 1 hour so 's'
    } // If hours = 0 then stringHours remains ''

    if (minutes === 1) {
        stringMinutes = '1 minute'; // Only 1 minute so no 's'
    } else if (minutes > 1) {
        stringMinutes = minutes + ' minutes'; // More than 1 minute so 's'
    } // If minutes = 0 then stringMinutes remains ''

    var returnString =  stringDays + stringHours + stringMinutes
    return returnString.trim(); // Removes the extraneous space at the end

})(input)

The item in defined as follow:

Number LocalComputer_Cpu_SystemUptime “[JS(CPUTime.js):%s]”

And I linked it with PaperUI to the CPU uptime channel of the System Binding Thing

Finally in Habpanel I have a dummy wigdet linked to the item and with the ‘Use server-provided format if available (openHAB 2)’ option ticked

The result is:

I hope this can help

Vincent

5 Likes

hello, thanks for help, but doesn’t work for me.
have i to install also some java plugin?
i ha copied the js file in transform and added the line in items file. linked also in habpanel but shows me in minutes :frowning:

You need to install the javascript transform

yep perfect! you’d better add this information at the top of this topic :wink:

Thanks done

On my home system doesn’t work…

Same files copied from raspberry pi desktop,but doesn’t work.

Installed java transformation,but nothing change…where can i get more information?

Ruben Fuser

Does anyone have this working in OH3?

Same question from my side.
Same picture. And, yes the javascript is being executed, but the result is not displayed.
No idea why…