openHAB 4.0.2 - weird calculations with units

Hi all,
I‘m doing some basic calculations with Azimuth angles with Qty like shown in the screenshot.

My calculation and expected results are as followed:
120° x 0,5 = 60° (correct see SunEnter)
120° + 60 = 180 (wrong see SunLeave)

The last calculation is giving a lot more decimal places:

Am I doing something wrong with the qty?

How do can I round this for less confusion ;).

  • Platform information:
    • Hardware: Raspberry PI 4 B, 4GB
    • OS: openhabian (latest)
    • Java Runtime Environment: 17
    • openHAB version: 4.0.2

First guess from my side is that you add a value without a unit to a quantity with a unit.
E.g. What is 120 Apples + 60 (whatevers)? There is no correct answer.
But 120 Apples x 0,5 makes sense, it is simply 60 Apples.

Have you tried converting the second value to a quantity with an equal UOM?

I‘m wondering, the second value (SunEnter) has a proper UOM. Pls see the console log. It shows 60°

The result of the calculation of WindowAzimut (120°) x 0,5 = 60° and is stored in SunEnter. In log SunEnter shows 60° with UOM. The error occurred when I than add SunEnter (60°) to WindowAzimut (120°). Both with proper UOM.

I wrote a small unit test to see if and why this happens in Java code as well.
It also happens in the Java code.

The reason seems to be that during the calculation degrees are converted to radians because the degree angle unit is expressed in radians in the UoM library:

1 Like