Transform duration.js to Profile JS

  • Platform information:

    • Hardware: RPi4b 8GB
    • OS: openHabian
    • openHAB version: 3.0.1
  • Issue of the topic: I want to get rid off my Visual Studio Code and build in as much as possible in the new awesome GUI. I´m using duration.js to transform my “last seen” from “2021-02-03T16:54:30.106+0100” to "Wed 16:53 ". How can I handle that in the GUI with Profile / JS?

  • Please post configurations (if applicable):
    // computes nicely formatted duration from given minutes
    (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;
      	if (d == 1) {
      		result = d + " day";
      	} else {
      		result = d + " days";
      	}
      }
    
      // hours
      if (h > 0) {
      	if (result != '') {
      		result = result + ', ';
      	}
      	result = result + h;
      	if (h == 1) {
      		result = result + ' hour';
      	} else {
      		result = result + ' hours';
      	}
      }
      
      // minutes
      if (m > 0) {
      	if (result != '') {
      		result = result + ', ';
      	}
      	result = result + m;
      	if (m == 1) {
      		result = result + ' minute';
      	} else {
      		result = result + ' minutes';
      	}
      }
    
      return result;
    

    })(input)

    • Items configuration related to the issue
      Zuletzt gesehen [%1$ta %1$tR]"
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

It would be nice if somebody can help to convert this to on board tools. Would be also nice2have to transform it to “German” instead of “English”.

Thank you in advance.

You cannot define a JS transformation in the UI. You still have to create the .js file outside of the OH GUI.

But this over all seems way over complicated. If the string is coming in as an ISO8601 formatted String, just set that to a DateTime Item and then use the %1$ta %1$tR formatting in the Item’s State Description metadata and you are done.

duration.js appears to take in a number of minutes and converts it to “5 days 7 hours 15 minute” (for example). It won’t work with 2021-02-03T16:54:30.106+0100 anyway. And you don’t need it.

1 Like

Ah ok, thank you :slight_smile:
Where is the Item´s State Description in the GUI?

Also from the import it will not recognize this parameter and transform it:

Where all the Item metadata is defined. Navigate to Settings -> Items -> your specific Item and there is an “add metadata” option. Click that and State Description will be one of the well known metadata options you can add.

I stopped using the import for Items some time ago. It’s less work to recreate them using “create equipment from Thing”. But if it doesn’t like the Name field it probably means you already have an Item with that name.

Oh my goodness, there is still so much to learn :smiley: it´s working!