UoM when migrating to OH4

I have a simple rule for my Fronius PV inverter. I replaced the inverter few months ago and I would like to add the total energy generated of this old inverter to the total energy of the new inverter. I’m using the following simple DSL rule and in worked under OH3.4.4 but not any more in OH4.0.3:

val tot_energy = 20350.27 //old total energy
var PV_tot_energy = (FroniusSymoInverter_TotalEnergy.state as Number) + tot_energy
Fronius_PV_total_energy.sendCommand(PV_tot_energy) 

New Fronius binding uses UoM for the FroniusSymoInverter_TotalEnergy so its unit is now kWh. Old binding didn’t use units.

I have tried to add unit to constant value tot_energy using

val tot_energy = 20350.27 | kWh

but this doesn’t work either. Any thoughts?

Try
var QuantityType<Energy> tot_energy = 12345 | kWh

Unfortunately it doesn’t seem to work. I have now tried the following DSL script:

val tot_energy = 20350.27
var PV_tot_energy = FroniusSymoInverter_TotalEnergy.state as Number + tot_energy
logInfo("test" , "tot_energy= " + tot_energy)
logInfo("test" , "FroniusSymoInverter_TotalEnergy= " + FroniusSymoInverter_TotalEnergy.state as Number)
logInfo("test" , "PV_tot_energy= " + PV_tot_energy)

The Maths seems to go through but the outcome is completely nonsense:

2023-10-01 11:29:27.108 [INFO ] [org.openhab.core.model.script.test  ] - tot_energy= 20350.27
2023-10-01 11:29:27.109 [INFO ] [org.openhab.core.model.script.test  ] - FroniusSymoInverter_TotalEnergy= 668.415 kWh
2023-10-01 11:29:27.109 [INFO ] [org.openhab.core.model.script.test  ] - PV_tot_energy= 2406314350.27

I really don’t understand the result. PV_tot_energy should be 21018.685.

I noticed a small error when trying to use var QuantityType<Energy> tot_energy. Now I tried:

var QuantityType<Energy> tot_energy = 20350.27 | kWh
var PV_tot_energy = FroniusSymoInverter_TotalEnergy.state as Number + tot_energy
logInfo("test" , "tot_energy= " + tot_energy)
logInfo("test" , "FroniusSymoInverter_TotalEnergy= " + FroniusSymoInverter_TotalEnergy.state as Number)
logInfo("test" , "PV_tot_energy= " + PV_tot_energy)

and the outcome is:

2023-10-01 11:34:19.549 [INFO ] [org.openhab.core.model.script.test  ] - tot_energy= 20350.27 kWh
2023-10-01 11:34:19.550 [INFO ] [org.openhab.core.model.script.test  ] - FroniusSymoInverter_TotalEnergy= 668.51206 kWh
2023-10-01 11:34:19.550 [INFO ] [org.openhab.core.model.script.test  ] - PV_tot_energy= 75667615416

The result is even more weird ( PV_tot_energy= 75667615416). It seems that I don’t understand UoM at all even in this very simple case.

It’s not so simple, you need to understand properly how to do programming with UoM.
You cannot add a Number to a QuantityType.
You did not tell how the FroniusSymoInverter_TotalEnergy item is defined (so unclear if it’s a Number or Number:Energy item and if you just mistakenly casted the item state to a Number) but in many cases implicit unit and other conversions happen and that results in all sorts of unexpected results.

Also you should not be using code like var PV_tot_energy = ... because you cannot be sure what data type is used in those cases. Always explicitly define the variable data type in the first place.

I have tried to look at the documentation for UoM but I have to admit that I didn’t understand that much of it. FroniusSymoInverter_TotalEnergy is defined as Number:Energy in the Fronius 4.0.3 binding. I guess there are two options: 1) convert FroniusSymoInverter_TotalEnergy to number without unit or 2) convert tot_energy to Number:Energy item but I don’t know how to do these.

OK, now I got it working finally with the help of rossko57’s post.

My DSL script is now:

var QuantityType<Energy> tot_energy = 20350.27 | kWh
var PV_tot_energy = (FroniusSymoInverter_TotalEnergy.state as QuantityType<Energy>).toBigDecimal + tot_energy.toBigDecimal
Fronoius_PV_total_energy.sendCommand(PV_tot_energy)
var tot_energy = 20350.27 | kWh
var PV_tot_energy = (FroniusSymoInverter_TotalEnergy.state as QuantityType<Energy>).add(tot_energy)
Fronoius_PV_total_energy.sendCommand(PV_tot_energy)

If you used jruby it’s even simpler

tot_energy = 20350.27 | "kWh"
Fronoius_PV_total_energy.command(FroniusSymoInverter_TotalEnergy.state + tot_energy)

Hi Jim,

Your code looks really simple. Perhaps I should start using jruby instead of DSL.

1 Like

I think in Rules DSL + will work too. But in other languages (notably Blocky and JS Scripting) because you have to use the add function.

The JS Scripting version would be

var tot_energy = Quantity("20350.27 kWh");
var PV_tot_energy = items.FroniusSymoInverter_TotalEnergy.quantityState.add(tot_energy);
items.Fronious_PV_total_energy.postUpdate(PV_tot_energy);

Note, you probably want to postUpdate instead of sendCommand in this context.

My DSL rule is now working, thanks to your help.

I upgraded my actual OH server yesterday from 3.4.4 to 4.0.3. In my test server (another Intel NUC with Win11) the unit for FroniusSymoInverter_TotalEnergy was kWh but for some reason the unit for
FroniusSymoInverter_TotalEnergy in my actual OH server is MWh which I really don’t understand. Anyway adding of the tot_energy and FroniusSymoInverter_TotalEnergy seems to be working.

From experience: forget about those UoM. They have been a reliable source of errors for many years.
In theory they are natural - what you know from your physics class but here they just complicate things.
Worst of all is that you have to adapt all your rules.

I agree about this. I also find UoM very confusing. I have now to use UoM in Fronius inverter binding because UoM was introduced in OH4. I have several Modbus TCP devices and I never use UoM with them because you can add the unit later easily once you have done all the maths.

No you don’t.

For the record, if you don’t want to use UoM, avoid Number:Something Items. You can link a Number Item to any number Channel, even those that define using a specific unit. The unit will be thrown away and you’ll just get the raw number.

If you choose to use UoM but don’t want to in rules, in JS Scripting use

items.ItemName.numericState

In Blockly use the “get [numeric state] from item” block.