smarthomej / addons

SmartHome/J addons for openHAB
Eclipse Public License 2.0
59 stars 24 forks source link

[Math transform] Unit is ignored if result is 0 #506

Closed clinique closed 1 year ago

clinique commented 1 year ago

At night I observe this warning :

2023-07-21 22:24:54.597 [WARN ] [penhab.core.library.items.NumberItem] - Failed to update item 'Total_Radiation_COR' because '0' could not be converted to the item unit 'W/m²'

The item definition is :

Number:Intensity Total_Radiation_COR "Radiation pondérée" <radiation> (gAstroSun1,gSynopanalyzer1) ["Point"] {unit="W/m²", channel="astro:sun:1:radiation#total" [profile="transform:MULTIPLY", multiplicand=0, itemName="Synop_Attenuation"]}

It takes current solar radiation :

Number:Intensity Total_Radiation "Radiation Totale" <radiation> (gAstroSun1) ["Point"] {unit="W/m²", channel="astro:sun:1:radiation#total"}

and multiplies it by the cloudiness factor (that has no unit) :

Number Synop_Attenuation "Coefficient Atténuation" <sun_clouds> (gSynopanalyzer1) ["Measurement"] {channel="synopanalyzer:synopanalyzer:1:attenuation-factor"}

During the day, it works fine but at night Total_Radiation = 0 W/m² and the warning is emitted. Taking a look at AbstractMathTransformationService, I found the origin of the problem :

QuantityType<?> result = performCalculation(source, value);
return BigDecimal.ZERO.compareTo(result.toBigDecimal()) == 0 ? "0" : result.toString();

So, if the result is 0, the code drops the unit, leading to the warning.

I think this could be changed to :

return performCalculation(source, value).toString();

@J-N-K : what do you think ?