Calculating numbers with different units

I am trying to do some math again. Looks like I am a bit lost again:

I want to calculate the Coefficient of Performance for my Heatpump:

rule "ESPAltherma.ESPAlthermaCOP"
//ESPAltherma COP Calcualtion
    Item ESPAltherma_INVprimarycurrentA changed
    if ( ESPAltherma_OperationMode.state == "Heating" ) {
        espaltherma_cop.postUpdate(((ESPAltherma_Flowsensorlmin.state as QuantityType<Number>) * 0.06 * 1.16 * (ESPAltherma_LeavingWatertempbeforeBUHR1T.state as QuantityType<Number>) - (ESPAltherma_InletwatertempR4T.state as QuantityType<Number>)) / (ESPAltherma_INVprimarycurrentA.state as QuantityType<Number>) * (ESPAltherma_VoltageNphaseV.state as QuantityType<Number>) / 1000)

The CoP is a number without any unit. How I am suppose to take only number from individual .items to make the formula work?


Work with the units, not against them. Do the maths using the Quantity types without casting to Numbers.

For example, it looks like you have a Volts x Amps calculation. Perform that using Quantities and the result will be in Watt units.

Do it in steps so you can see what is happening, consolidate into mega code after you’ve fixed it.

Hi Ross,
Thanks, yes, I have done that already with VA / W and it is working.

rule "solaredge.ESPAltherma_consumption"
//ESPAltherma COP Calcualtion in kW
    Item ESPAltherma_INVprimarycurrentA changed
    espaltherma_consumption.postUpdate((ESPAltherma_VoltageNphaseV.state as QuantityType<Number>) * (ESPAltherma_INVprimarycurrentA.state as QuantityType<Number>))

EDIT: I am trying to do it the “right way”

espaltherma_consumption.postUpdate((ESPAltherma_VoltageNphaseV.state as QuantityType<ElectricPotential>) * (ESPAltherma_INVprimarycurrentA.state as QuantityType<ElectricCurrent>))

The issue with the CoP formula is for example with volumetric flow rate. Mine is in l/min. Is this then correct?
ESPAltherma_Flowsensorlmin.state as QuantityType<VolumetricFlowRate>
How the OH will recognize it is in l/min instead of m3/s?

It doesn’t ‘recognize’ any particular units desired, that’s the whole point of quantities really.
It will do V x A or kV x mA and the result might come in mW or kW, who knows.
But who cares either; 1500mW or 1.5W are the same quantity.

Now that’s a bit inconvenient for humans thinking in number terms, we need a way to tell it when we want a quantity represented in some particular unit -

(ESPAltherma_Flowsensorlmin.state as QuantityType<VolumetricFlowRate>).toUnit("°l/min")

it needs to be a unit string known to openHAB, of course