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
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.
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)
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.
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.
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.