UoM problem

I can‘t see the forest for the trees with this UoM problem.
Until recently the following js-rule worked perfectly:

var result = actions.Exec.executeCommandLine(…); // this returns a two digit number without unit, e.g.: 50
items.itemX.postUpdate(result + " °C");

All of a sudden this results in a parsing error so that I need to convert the argument to a quantity first, according to the docs:

items.itemX.postUpdate(Quantity(result.toString() + " °C"));

My itemX is of type number:temperature
However, this results in this error:

Failed to create QuantityType from 50 °C: java.lang.IllegalArgumentException: Invalid Quantity value: 50 °C

whereas this works:

items.itemX.postUpdate(Quantity("50 °C"));

Where is my error?

If I had to guess it is because you are trying to use a string value of 50 concatenate the degree sign and C then convert the string. A quantity value needs to be only an integer normally.

According to the docs the argument value can be a string, a Quantity instance or an openHAB Java QuantityType.
Furthermore this line works

yes I see that line works however in your example that “works” you are not concentrating the integer of 50 with the quantity type. and trying to convert it as a entire string value. Order of operation
The working example is calling quantity with 2 arguments a integer of 50 and a type of Degree C

Even if I did an „overkill“ like this to make sure the argument is a string

var qResult = Quantity((result.toString() + " °C".toString()).toString());

the error message remains the same.
I have no clue what I need to do…

does this work?
var qResult = Quantity((result.toString()) + (" °C"));

No, it doesn‘t. Error message is the same.

Are you sure the space and the degree are really the same in the result + " °C" and the "50 °C" strings? There are some unicode characters that look nearly identical but may cause issues (this is also the case with µ: U+00B5 and U+03BC look nearly the same, but they might parse differently.

I double checked your suggestion.
I copy pasted the working code Quantity("30 °C") and changed it to Quantity(result.toString() + " °C") but the problem remains

Did you check the result of the action is really what you expect? Add some logging for that. Maybe it‘s not a two-digit number (e.g. quoted).

I am absolute positive. The correct string already appears in the error message. Have a look here:

This is the item (I just replaced the long item name above)

I am on 4.1.0.M3

I loaded up a 4.1.0.M3 and built out a script this works fine for me .
var somenumber = '95.34';

   ``` var qResult = Quantity(somenumber +\"°F\");```

  ```  console.info(qResult);```

console.info('try this '+ qResult);
console.info('custom Item '+ items.getItem('NewItemtest').quantityState);
console.info('custom Item '+ items.getItem('NewItemtest').state);

2023-11-11 01:37:48.674 [INFO ] [nhab.automation.script.ui.19c2ac3a5f] - try this 95.34 °F
2023-11-11 01:37:48.675 [INFO ] [nhab.automation.script.ui.19c2ac3a5f] - custom Item 95.34 °F
2023-11-11 01:37:48.675 [INFO ] [nhab.automation.script.ui.19c2ac3a5f] - custom Item 95.34 °F

only thing different is I am on imperial.
And I did not include the space.

Thanks for helping me in finding the problem.
Finally I was able to find the problem. I had to multiple my variable „result“ by 1

result = result * 1
Quantity(result.toString() + " °C")

Allthough result just contained numbers, multiplying by 1 maybe got rid of some „hidden“ characters?

Anyway, now it is working again. Thanks for your help!

1 Like