Hi,
I’m trying to implement this rule in OH3 and am it is not showing the previous month as it should (and yes, I do have history for the last month, although not for the full month).
What am I missing here?
rule "Update kWh for Heat Pump + AC"
when
Item HeatPumpEnergy changed
then
val currentPower = (HeatPumpEnergy.state as DecimalType).doubleValue // Power in Watts
val lastUpdateDate = HeatPumpEnergy.lastUpdate.toInstant.atZone(ZoneId.systemDefault()) // Last update time as ZonedDateTime
val deltaTime = Duration.between(lastUpdateDate, ZonedDateTime.now()).toMillis / 1000.0 / 3600.0 // Time in hours since last update
val energy_kWh = (currentPower / 1000) * deltaTime // Energy consumed since last update in kWh
val currentMonth_kWh = if(HeatPumpCurrentMonthkWh.state instanceof DecimalType) (HeatPumpCurrentMonthkWh.state as DecimalType).doubleValue else 0.0
HeatPumpCurrentMonthkWh.postUpdate(currentMonth_kWh + energy_kWh) // Update the energy consumed so far this month
val currentDay = ZonedDateTime.now().getDayOfMonth()
if(currentDay == 1 && lastUpdateDate.getDayOfMonth() != 1) {
HeatPumpLastMonthkWh.postUpdate(currentMonth_kWh)
HeatPumpCurrentMonthkWh.postUpdate(0)
val currentYear_kWh = if(HeatPumpCurrentYearkWh.state instanceof DecimalType)
(HeatPumpCurrentYearkWh.state as DecimalType).doubleValue // Energy consumed so far this year in kWh
else
0.0
HeatPumpCurrentYearkWh.postUpdate(currentYear_kWh + currentMonth_kWh) // Update the energy consumed so far this year
val currentYear = ZonedDateTime.now().getYear()
if(currentYear != lastUpdateDate.getYear()) {
HeatPumpLastYearkWh.postUpdate(currentYear_kWh)
HeatPumpCurrentYearkWh.postUpdate(0)
}
}
end
Thanks!