Change seconds to elapsed time

Hi,

I receive a uptime in seconds from a device and would like to convert it to a easier format to read (Years, monts, days, minutes, seconds).

How would I got about doing that ?

I’m pretty sure I need a DateTime() object but after that, i’m stuck

Any pointers ?

Thanks :slight_smile:

if you define a DateTime item for this information, you can have the output formatted like in the WeMo example :

DateTime Insight_LastChange “Last change [%1$tH:%1$tM]”

In this example, seconds are transformed to hours and minutes.

1 Like

And this done without a rule to convert the values ?

Ok, tried it and it doesn’t work.

The seconds aren’t formatted into a datetime

Sorry for misleading you, this can’t work without translation in a rule. The WeMo binding sends the information in DateTime form.
Let me think about it a bit more.

I wonder if that might be a Homie device, :wink:

I have done something similar for my Homie-based sensor device in the following way:

.items
String S02D001_nUptime { mqtt="<[mosquitto:devices/12138ee0/system/uptime:state:JS(uptime.js)]" }

uptime.js
(function(seconds) {
var retval = “”;

  var days = Math.floor(seconds / (24 * 60 * 60));
  seconds = seconds % (24 * 60 * 60);
  var hours = Math.floor(seconds / (60 * 60));
  seconds = seconds % (60 * 60);
  var minutes = Math.floor(seconds / (60));
  seconds = seconds % (60);

  if (days > 0) {
    if (days > 1) {
      retval = retval + days + " " + "days ";
    }
    else {
      retval = retval + days + " " + "day ";
    }
  }

  retval = retval + hours + ":";

  if (minutes < 10) {
    retval = retval + "0" + minutes;
  }
  else {
    retval = retval + minutes;
  }

  return retval;
})(input)

.sitemap
Text item=S02D001_nUptime label="Uptime [%s]"

With this in place I get the uptime reported as for example “9 days 13:43”.

I choose the format to be similar to that being reported by the System Info binding. Obviously, you can tweak the JS code to format it any way you like.

If you want/need your uptime item in openHAB to be of type DateTime, then I guess this should be possible too - although I have not tried it.

2 Likes

Yes it is LOL !

Thanks for the script, it worked like a charm ! :slight_smile:

Hi guys, just trying to borrow this code to show the uptime of my Homie devices…

I dont’t really know what I’m doing but created a “uptime.js” file in the “transform” folder of openhab and then setup my item the same as yours…

No joy though… I get massive errors in the openhab log with stuff like…
26:2 Invalid return statement

Any pointers?

UPDATE : Hmmm looks like I had copied the JS script and missed the first line :slight_smile: Works perfectly now…

How would I adapt this for a Wemo Insight device? The device reports uptime in seconds only.

I have no experience with Wemo devices, but from the documentation (the Wiki) it looks like the Wemo binding does not support transformations, so I don’t think you can use the script in the way shown for the MQTT binding.

You best or only (?) option may be to implement a rule to convert from your Wemo item (in seconds) to a Text item (the time string) and try to re-write the script in the rule language in some way.

I’m new to OH but I’ll try and work on that.

Slightly modified above presented uptime.js

function(s){ 
  var d=s/86400|0;
  var h=(s%=86400)/3600|0;
  var m=(s%=3600)/60|0; 
  return d+'d. '+('0'+h).substr(-2)+':'+('0'+m).substr(-2);
})(input)

Maybe someone will be interesting…

4 Likes

This script throws me a bunch of erros into the log…

I´m on openhab 2.3 snapshot.

items:

Number  Squeezeplayer_EG_WC_duration	"EG WC Dauer"               (gSqueezebox)   { channel="squeezebox:squeezeboxplayer:B2C02C4C-9107-4CC8-B2F3-D16387AE9904:Player_Raspi_EG_WC:duration" }
String  Squeezeplayer_EG_WC_duration_converted  "EG WC Dauer mm:ss [JS(mmss.js):%s]"    (gSqueezebox)

rule:

rule "Squeezebox WC - convert seconds to mm:ss"
when
    Item Squeezeplayer_EG_WC_duration received update
then 
    Squeezeplayer_EG_WC_duration_converted.postUpdate(Squeezeplayer_EG_WC_duration)
end

Any idea? I only get small second-amounts to my number item, only few minutes or maybe sometimes 1 hour, but never as long as a day. But i think the script has to work with mabe only 200 seconds as well?

Your script is named as mmss.js and it is inside transform-folder of openhab.

Javascript Transformation is installed. Other js-scripts work without any problem.

EDIT:
Got it:

I have to use the command:

Squeezeplayer_EG_WC_duration_converted.postUpdate(Squeezeplayer_EG_WC_duration.state.toString)

EDIT2:

But script doesn´t work…

I still get only the seconds without any conversion to mm:ss.

Thank you for this really short JS Code. I think there is a missing open bracket “(” before the word “function”. :wink:

Yes! Sorry, did not look :slight_smile:

Hi

Whats the correct code with the missing ( ?

Found it :slight_smile:
/Jerry