Alternating time/date format?

I‘m looking for a way to have a readable value for the uptime of my Raspberry.

Normally I can only decide for one date, like number of minutes or date. But while minutes is fine until 59 minutes, it‘s easier to understand 3hrs 10 minutes than 190 minutes.

So, is there a way for a „smart“ date format which follow these rules?

  • minutes: until 59 minutes - afterwards: hours+minutes
  • hours+minutes: until 23:59 hours - afterwards: days+hours
  • days+hours: until 6 days 23 hours - afterwards: weeks+days

And so on…

Is there a way to accomplish this in OH?


I added a function into of the Jython helper libraries to do just that…

Don’t confuse a time duration with a date time. A DateTime Item and related DateTime Objects like now in Rules represent an instant in time. At the lowest level they are literally the number of milliseconds that have passed since 1970-01-01T00:00:00.

So if you have a duration stored in a Number Item, let’s say number of seconds, and you want to format that number of seconds for display on your sitemap, you need to apply a JavaScript transformation to split up the number of minutes to days, hours, minutes.

(function(i) {
    if(isNaN(i)) return "NAN";
    var mins = i%60
    var totalhours = Math.floor(i/60)
    var hours = totalhours%24
    var days = Math.floor(totalhours/24)
    var pad = "00";
    return days+":"+(pad+hours).slice(-pad.length)+":"+(pad+mins).slice(-pad.length)+":00";

If you are using Python for Rules and want to maintain a separate String Item with this representation see Scott’s link.

If using Rules DSL, the above should give you enough information to build it using Rules DSL. If not ask questions.

1 Like