Format time to total number of minutes

Hello,

I have an total elapsed time that comes in from my robovac of total time it has cleaned for. It is in the below format:

historyTime changed from 1970-01-01T00:02:33.000+0000 to 1970-01-01T00:02:41.000+0000

I would like to display this as total elapsed minutes. I tried configuring my item like this:

String historyTime	"Total Clean Time [%1$tM]"

but for 162 total minutes is only displays as “02”.

googleing around I quickly found this:

and that points to this:

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Where $tM is defined as minutes within an hour. I’m guessing this will not work for me, but I do not see any options for elapsed minutes. Anyone have any ideas?

Dear Tom,

In Java 11 you can use the class java.time.Duration. With the between(Temporal startInclusive, Temporal endExclusive method you can calculate the Duration between to temporals and then the toMinutes() method will give you what you are asking for.

Because ZonedDateTime, which is the internal representation in OH3, implements Temporal you can directly use it with Duration

See the javadoc for a deeper dive: Class Duration

Hope this helps.

Thanks Frank.

I’m actually not trying to determine duration between two times, that duration is being supplied.

1970-01-01T00:02:41.000+0000

This is 2 hours and 41 minutes duration from the unix epoc (1970-01-01 0:00)

I’m looking for a way to either format this as 2:41 hours or 161 minutes

From this log I got the impression that this is from a rule triggered by and item change event. That is why I suggested some programming hints using Duration. If this is the case, then you have the new state and the old state as DatenTimeType. You can simple convert them to a ZonedDateTime and give that to the duration.

If you want to convert a string directly to a duration, there is no way I can think of. The reason for this is that there is no concept of durations in OH and therefore no way of formatting them.

The only way would be to calculate a duration by using your string to parse a ZonedDateTime from it and use the epoc start as the other ZonedDateTime, calculate the duration like I suggested and format it with the formatting methods of Duration. But this can only be done in a rule or script not with item format descriptions.

?? isn’t it 0 hours 2 minutes and 41 seconds instead of 2 hours and 41 minutes ?

1 Like

You can’t do that with the java datetime formatter, which offers no duration options.
You could display hh:mm using $H:$M but it will go wrong when duration exceeds 24 hours.
But I don’t think any of that works on your String type Item, because the state is not held as a datetime for the formatter to work with…

You’ll want a transformation to do this properly, probably a javascript that calculates epoch seconds and then does maths division to get the time values you want.

1 Like

Thanks for the pointer @rossko57. I developed a small js transform for this. In case someone looks for something similar in the future, here is what worked for me in openHAB 2.5.11

//Example of raw input string: "1969-12-31T19:04:52-05:00"
//This would be converted to 292 mins
//Example item:
//String historyTime		"Total Clean Time [JS(elapsedtime.js):%s]"

(function(i) {
	if (i == 'NULL') { return i; }
	if (i == '-') { return 'Undefined'; }

	//split the string at on colons.  The second item is then hours.  Parse this string into an Int
	var hour = parseInt((i.split(":"))[1]);
	//split the string at on colons.  The third item is then minutes and the time zone.  Split the 
	//String again at the - and the first item is the minutes.  Parse this string into an Int
    //Note, if you are in a timezone + from GMT, you'll need to have the second split be on "+" rather than "-"
	var min = parseInt(((i.split(":"))[2].split("-"))[0]);

	//Convert HH to mins and add together
	var total_mins = (hour*60)+min;

	//Return as a whole number only (no decimal)
	var returnString = total_mins.toFixed(0) + " mins";
	return returnString;

})(input)