OH3: transformation of numbers with units

  • Platform information:
    • Hardware: Raspberry Pi 4 Model B Rev 1.1 ; 4GB memory
    • Host: Linux openhabian 5.15.76-v7l+ #1597 SMP Fri Nov 4 12:14:58 GMT 2022 armv7l GNU/Linux
    • Distro: Raspbian GNU/Linux 11 (bullseye)
    • openjdk version “11.0.18” 2023-01-17
    • OpenJDK Runtime Environment (build 11.0.18+10-post-Raspbian-1deb11u1)
    • OpenJDK Server VM (build 11.0.18+10-post-Raspbian-1deb11u1, mixed mode)
  • OH Version: 3.4.3 (Build)
    • Installation method: openhabian

I installed the NetWorkTools binding for the UPS connected to the openHABian Pi.
The binding has built-in channels; one of which is ‘Battery Runtime’. It comes across as Number:Time: 4000 s.
image

I have unit-less JS transfomrations that don’t seem to work on numbers with units.

So I created a new channel, as number… applied the transformation and show it in a string item.

This doesn’t seem right to me; or units make life more difficult to work with.

Is there a better way?

There’s not enough details here to answer your question about what a better way could be because it’s not clear what way you tried in the first place.

Over all the answer is yes, you don’t need to create a separate Channel to deal with the units. But without more details it’s kind of hard to say what that would be.

JS transform scripts can handle units and once you know how, it’s really not hard to work with Quantity types in transform scripts or rules (it’s the same in both places).

The way I set up the UPS runtime Item in my setup is as follows:

  1. Number:Time
  2. State Description Pattern: %1$tH:%1$tM:%1$tS

That keeps the value as a number so it can be charted or do math with it in rules. Number:Time Items support DateTime formatting up to 30 days. To get the output you are showing use pattern %1$tH hour, %1$tM minutes.

If you want the transform to be smarter and use hours when there is more than one (for example) use. a JS transform on the State Description Pattern. Keep the Item a Number:Time Item so you can do math and chart and such and just change how it’s displayed.

var seconds = Quantity(input);
var duration = time.Duration.ofSeconds(seconds.int);
var hours = duration.toHours();
var minutes = duration.minusHours(hours).toMinutes();
var seconds = duration.minusHours(hours).minusMinutes(minutes).toSeconds();

var parts = [];
if(hours >= 1) parts.push(hours + (hours > 1) ? " hour" : " hours");
if(minutes >= 1) parts.push(minutes + (minutes > 1) ? " minute" : " minutes");
if(seconds >= 1) parts.push(seconds + (seconds > 1) ? " second" : " seconds");

return parts.join(', ');

I use the time.Duration to break the seconds apart and then test to see how many hours, minutes, seconds there are to choose whether the name ends in “s” or not and put them into and array and use join to create the comma separated list. If the value is zero it’s omitted from the output.

Apply this in the State Description Pattern and keep the Item a Number:Time.

Note, the JS above is JS Scripting and uses features in the latest openhab-js library. You’ll need to use the SCRIPT transform (not the JS transform) in OH 3 and you might need to install the latest openhab-js to use it as written. I don’t know what/if anything relevant has changed between 3.4 and 4.2 M1 in opennhab-js.

1 Like

As always; thank you very much. :+1:

I pondered about what was missing; other then the JS to do the conversion.
However, after all these years, the GUI config and menu parameters still seem unfamiliar to me.
All that was required, is what you suggested…
I deleted the extra channel; changed the string time to a Number:Time item and selected the output format. Done, works.

Also thanks for the JS conversion code.

This function was oblivious to me :frowning:

But now I know why :slight_smile:

It is probably this time again…
… where I need to upgrade to v4. :slight_smile:

All I can recommend is to keep the reference docs for JS Scripting open when writing rules and always refer to them the moment you don’t know how to do something that is dealing with interacting with OH. They are really quite thorough and include a number of examples. Be sure to reference the linked to docs as well, particularly when needing to do something with ZonedDateTime for example.

Quantity is covered at JavaScript Scripting - Automation | openHAB.

Note, be sure to open the version of the docs that matches the version of OH you are running.

The upgrade from 3 to 4 is super easy compared to the upgrade between 1 and 2 and between 2 and 3. OH 4 mostly just adds new stuff with far fewer breaking changes. But be sure to review the announcement for OH 4 because there are a few breaking changes you might have to account for.

The good news is your current JS Transforms should transfer to OH 4 with the JS Scripting add-on without any needed changes on your part. But you’ll have to update the mime type of your rules if the upgradeTool fails to do it for you.

1 Like