Oliver2
(Oliver)
November 10, 2023, 2:46pm
1
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?
justaoldman
(Justan Oldman)
November 10, 2023, 5:55pm
2
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.
Oliver2
(Oliver)
November 10, 2023, 6:02pm
3
According to the docs the argument value
can be a string, a Quantity
instance or an openHAB Java QuantityType.
Furthermore this line works
justaoldman
(Justan Oldman)
November 10, 2023, 6:07pm
4
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
Oliver2
(Oliver)
November 10, 2023, 6:25pm
5
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…
justaoldman
(Justan Oldman)
November 10, 2023, 6:29pm
6
does this work?
var qResult = Quantity((result.toString()) + (" °C"));
Oliver2
(Oliver)
November 10, 2023, 6:34pm
7
No, it doesn‘t. Error message is the same.
J-N-K
(Jan N. Klug)
November 10, 2023, 7:14pm
8
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.
Oliver2
(Oliver)
November 10, 2023, 7:27pm
9
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
J-N-K
(Jan N. Klug)
November 10, 2023, 7:30pm
10
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).
Oliver2
(Oliver)
November 10, 2023, 7:32pm
11
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
justaoldman
(Justan Oldman)
November 11, 2023, 6:39am
12
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);```
items.getItem('NewItemtest').postUpdate(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.
Oliver2
(Oliver)
November 11, 2023, 7:29pm
13
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