# Kostal: Calculation kWh to MWh

I’m using now the new Kostal-Binding (2.4 M7) with the QuantityTypes.
Before I grabbed the values directly from the converter and had such a simple conversion rule.

``````Number	num_PVWR_totalEnergy		"Ertrag (gesamt) [%.0f kWh]"		{ channel="kostalinverter:kostalinverter:Piko:totalEnergy" }
Number	num_PVWR_totalEnergyMWh		"Ertrag (gesamt) [%.1f MWh]"
``````
``````rule "PVWR Gesamtwert konvertieren"
when
Item	num_PVWR_totalEnergy changed
then

if (num_PVWR_totalEnergyMWh.state != NULL)
num_PVWR_totalEnergyMWh.postUpdate(num_PVWR_totalEnergy.state as Number / 1000.0)
end
``````

Since I upgraded to use the QuantityType, I get a value, which is multiplied by 3600 instead of divided by 1000. Whats wrong with my rule?

``````Number:Energy     num_PVWR_totalEnergy    "Ertrag (gesamt) [%.0f kWh]"	{ channel="kostalinverter:kostalinverter:Piko:totalEnergy" }
Number            num_PVWR_totalEnergyMWh "Ertrag (gesamt) [%.1f MWh]"
``````
``````rule "PVWR Gesamtwert konvertieren"
when
Item	num_PVWR_totalEnergy changed
or
Time cron "10 0/2 * * * ?"
then

if (num_PVWR_totalEnergy.state != NULL)
{
val Number total = num_PVWR_totalEnergy.state as Number
val Number totalM = total / 1000.0

val Number totalkWh = num_PVWR_totalEnergy.state as QuantityType<Number>
val Number totalMWh = totalkWh / 1000.0

num_PVWR_totalEnergyMWh.postUpdate(totalMWh)
logInfo("PV", "Total {}, {} MWh, {}, {} MWh", total, totalM, totalkWh, totalMWh)
}
end
``````

As result I get:

``````09-Dez-2018 13:30:10.028 [INFO ] [rg.eclipse.smarthome.model.script.PV] - Total 25631 kWh, 92271600.00000000 MWh, 25631 kWh, 92271600.00000000 MWh
``````

Try changing the line above to this:

``````((num_PVWR_totalEnergy.state as Number).intValue)/1000.0)

``````

Thanks, that did it.

But to be honest, I haven’t understood why it hasn’t worked before. Which information is additionally included in Number.state?

Good deal.

If you would please mark the post as solved by clicking the square box (lower right corner) on the post that provided the solution. Also edit the title to start with [Solved] this will help other with a similar issue find a quick solution.

Thanks

Your specifying in the rule to use the items state as a number. I’m not sure what additional info your referring to. If you mean .intValue, that’s just specifying to use integer number not float.

Here’s a good site that I keep in my book marks for help with rules. https://www.w3schools.com/js/js_numbers.asp You can see examples and use “try it yourself” to help with learning/understanding. Don’t forget to checkout some of the other topics on the left side of the page like JS Operators, JS Conditions, etc…

One more thing I forgot to mention above, when naming items, its recommended to use a capitol letter as the first letter.

``````num_PVWR_totalEnergy
``````

recommended to be

``````Num_PVWR_totalEnergy
``````

As you’ve noticed either way works, but using lower case items with VSCode results in the item not being highlighted. If your not using VSCode with the OH extension, to create and edit files, I highly recommend it. You can thank me later.

The `Number.state` returns a QuantityType, which is a child of NumberType.
It includes the Unit
To go back to a plain number you can use:

``````rule "PVWR Gesamtwert konvertieren"
when
Item	num_PVWR_totalEnergy changed or
Time cron "10 0/2 * * * ?"
then
if (num_PVWR_totalEnergy.state != NULL) {
val Number total = num_PVWR_totalEnergy.getStateAs(QuantityType).doubleValue
val Number totalM = total / 1000.0
val Number totalkWh = num_PVWR_totalEnergy.getStateAs(QuantityType).doubleValue
val Number totalMWh = totalkWh / 1000.0
num_PVWR_totalEnergyMWh.postUpdate(totalMWh)
logInfo("PV", "Total {}, {} MWh, {}, {} MWh", total, totalM, totalkWh, totalMWh)
}
end
``````

BUT you could make use of the UoM:

``````Number:Energy     num_PVWR_totalEnergy    "Ertrag (gesamt) [%.0f kWh]"	{ channel="kostalinverter:kostalinverter:Piko:totalEnergy" }
Number:Energy            num_PVWR_totalEnergyMWh "Ertrag (gesamt) [%.1f MWh]"
``````

``````rule "PVWR Gesamtwert konvertieren"
when
Item	num_PVWR_totalEnergy changed or
Time cron "10 0/2 * * * ?"
then
if (num_PVWR_totalEnergy.state != NULL) {
num_PVWR_totalEnergyMWh.postUpdate(num_PVWR_totalEnergy.state as Number)
}
end
``````

This should convert the KWh into MWh automagically.

It seems that the handling of the QuantityType is not fully reliable yet.

``````  if (num_PVWR_totalEnergy.state != NULL)
num_PVWR_totalEnergyMWh.postUpdate(num_PVWR_totalEnergy.state as Number)
logInfo("PV", "Total {}, {}", num_PVWR_totalEnergy.state, num_PVWR_totalEnergyMWh.state)
``````

and got this result:

``````10-Dez-2018 20:42:12.764 [INFO ] [rg.eclipse.smarthome.model.script.PV] - Total 25633 kWh, 25.633
10-Dez-2018 20:44:10.019 [INFO ] [rg.eclipse.smarthome.model.script.PV] - Total 25633 kWh, 25633 kWh
10-Dez-2018 20:46:10.042 [INFO ] [rg.eclipse.smarthome.model.script.PV] - Total 25633 kWh, 25633 kWh
``````

The first conversion looked quite good, but the MWh is missing.
All following cycles the conversion was completely ignored.

I still need to give the variant with getStateAs a try…

Regarding:

I expected that

``````val Number total = num_PVWR_totalEnergy.state as Number
``````

would do a conversion and returns the value of the quantity and not a value which includes somehow additional information. Otherwise the both values would have been identical.