Issue with script transforming seconds to human-readable format

Hi all,

I’ve got an issue with a .js which should transform the value of an item which is returned in seconds from the binding to a more human readable format.

To achieve that, I created 2 items, one bound to the channel and one not bound:

Number robonect_duration "Dauer aktueller Modus [%s]"   <grass> {channel="robonect:mower:13b76ca8:mowerStatus#duration" }
Number robonect_transform "Dauer aktueller Modus [%s]"

The rule to transform currently looks like that:

rule "TransformRobonectDuration"
when
        Item robonect_duration changed
then
        logInfo("test_log","[Test Log]Log entry test...")
        var robonectduration=transform("JS", "automower_duration.js", robonect_duration.state.toString)
        robonect_transform.postUpdate(robonectduration)
        logInfo("FILE",robonectduration)
end

Looking at the logs, the rule seems to be kicking in correctly, but the actual script execution to transform is failing:

==> /var/log/openhab2/openhab.log <==
2017-07-21 08:59:03.379 [INFO ] [ipse.smarthome.model.script.test_log] - [Test Log]Log entry test...
2017-07-21 08:59:03.387 [ERROR] [ore.transform.actions.Transformation] - Error executing the transformation 'JS': An error occurred while executing script.
==> /var/log/openhab2/events.log <==
2017-07-21 08:59:03.393 [ItemStateChangedEvent     ] - robonect_transform changed from 963 to 1001
==> /var/log/openhab2/openhab.log <==
2017-07-21 08:59:03.396 [INFO ] [.eclipse.smarthome.model.script.FILE] - 1001

So whatever error occurs when executing the script.

The actual script:

automower_duration.js

(function(i) {
    var ret = "";
    var seconds = JSON.parse(input).status.duration;
    var days = Math.floor(seconds/(24*60*60));
    var restwodays = seconds % (24*60*60);
    var hours = Math.floor(restwodays/(60*60));
    var restwohours = restwodays % (60*60);
    var minutes = Math.floor(restwohours/ 60);
    var seconds = restwohours % 60;

    var minutess = "" + minutes;
    if(minutes<=9) minutess = "0"+minutes;

    var secondss = "" +  seconds;
    if(seconds<=9) secondss = "0"+seconds;

    if ( days  > 0 )   ret = ""  + days    + "T ";
    if ( hours > 0 )   ret = ret + hours   + ":";
    if ( minutes > 0 ) ret = ret + minutess + ":";
    ret = ret + secondss + "";
    return ret;
})(input)

I am on openHAB 2.1, transformation-javascript - 2.1.0 is installed according to Paper UI.

Can someone please help me to correct the code?

Thanks,

Ben

Found another one which seems to be doing fine.

(Yes, I need to steal as I don’t have the slightest clue about that)

(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)

It would be nice if y would show that your topic is solved. Use the solution button on the post that holds the solution.

Just did so, sorry that I missed that.