[SOLVED] Easiest way to convert seconds to minutes

Hi,

I’ve an item linked to the Network UPS Tools binding which returns the number of seconds of UPS battery power remaining. The Item definition looks like this:

Number UPS_house_runtime "UPS battery runtime [%.0fs]" {networkupstools="house:battery.runtime"}

It would be much more user-friendly to display the number of minutes remaining. I could do this with a rule that captures every update of this Item, divides it by 60 and then updates another Item with the result. That feels messy and inefficient - is there a neater way I am missing?

many thanks,

Dan

Try

Number:Time UPS_house_runtime "UPS battery runtime [%.0f min]" {networkupstools="house:battery.runtime"}
1 Like

Ah, yes - I tried that but it just displays the figure in seconds. Perhaps the binding has to be updated to indicate that the raw unit is in seconds? Or is there a well I can tell the item definition to convert from seconds?

Check with [%.0f %unit%]

2 Likes

it doesn’t like that - displays ‘Err’

I should have said: really appreciate your help

Dan

Your item:

Number:Time UPS_house_runtime "UPS battery runtime [%.0f s]" {networkupstools="house:battery.runtime"}

Your sitemap:

Text item=UPS_house_runtime label="UPS battery runtime [%.0f min]"
5 Likes

nice, it works. THX

Where can I find a documentation to this function?
Is there any similar function for MB to GB?

No this is not implemented in the Uom Framework yet.
I may have a go at it, it looks simple enough

2 Likes

Maybe the exec transformation can help you with that.

https://docs.openhab.org/addons/transformations/exec/readme.html

1 Like

Yes or Javascript transform…

1 Like

THX

I had found the link:
Units Of Measurement | openHAB

But I could not found the information about Time etc.
Now with your link to the Topic everything is clear.

1 Like

funny, doesn’t work for me. Still displays the number of seconds.

did you set:

1 Like

Yes - copied straight from your post, the item and the sitemap entry

What Version of openhab do you run?

I am not sure, but I think it is a very new feature:

1 Like

the 2.3 release

OK. Turns out I just wasn’t patient enough. Took a few minutes to update (or next binding refresh?) and now works fine

Thank you!

I also use a JS transformation to make it “human”

CPUTime.js

/*
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) {
    if (i == 'NULL') { return i; }
    if (i == '-') { return 'Undefined'; }
    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)

It seems to make the same but more easier

(function(i){ 

	var d = Math.floor(i / (24 * 60));
	var h = Math.floor((i / 60) - (24 * d));
	var m = Math.round(i - 60 * (24 * d + h));
	var result = '';

	// days
	if (d > 0) { 
		result = result + d + ' day(s) ';
	}
	
	result = result + ("00" + h).substr(-2,2) + ":" + ("00" + m).substr(-2,2);

	return result;
})(input)

can you explain how whe can inside rule converting secondes to minutes, my goal is to send 90secondes to 1minutes30 fo exemple to somes voice assistant, i don’t want to have a finished reply but a wire to make myself , i thing is very important to doing by meself , thanks in advance