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?
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
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.
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. JavaScript Numbers 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
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.
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.