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

6 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…

It works in OH 3.3.

As the function returns a string, you should define the item as a String item, not a Number:

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

I also added a semicolon to the line

var returnString =  stringDays + stringHours + stringMinutes;

Excellent

I think there’s a misunderstanding about the workflow here.

In this usage -
anyItemType LocalComputer_Cpu_SystemUptime "[JS(CPUTime.js):%s]"
the JS transformation is being used for display presentation purposes only.

It is true that the JS input and output are in string form - but that does NOT mean you can only work with String type Items.

Used with any Item type at all, the OH framework takes care of converting the Item state to a string before feeding it to the transform.
So Number Item types are okay.
As are DateTime, Dimmer etc.
(A side effect of that is that if you intend to do maths inside your transform, you may need to parse the received string into a decimal etc.)

The (string) output of the transform is used for display.
Again, if the transform returns a number, the OH framework will stringify it before use.
That means that you need a string formatter in your [presentation] parameter - the %s - something like %0.f would fail on the string transform result.

The transform here never affects the Item state in any way, only presentation. It’s a different usage to e.g.in a profile or channel.

The trick with MainUI is getting it to use the transformed state in your widget, because both transformed and raw Item states are available to you.

1 Like

In fact I was using the main UI with the JS profile. I was not aware that there were differences between the two methods. Thank you for clarifying this.

But my remark about using a String item is definitely the solution for the problem of @smarthomepch. He also uses the JS profile in the link.