I am maintaining an item with rain readings (total mm, incrementing at random times) - and would like to be able to calculate a “rate” as mm/hour.
I guess that I let an update of the “total mm” reading trigger a rule, which should then calculate the difference in time between now and the last update - and then difference in reading (mm) between them.
Dividing the mm amount towards the period (in hours) should give me the “rate”.
Can anyone suggest how this looks in a rule ? - it is primarily the “difference in time between now and the last update” I am struggling with?
This is what I use for calculating water consumption:
rule "Meter_update"
when
Item meterupd received update
then
val Number newMeter = meterupd.state as DecimalType
switch(updmeter.state as DecimalType) {
case 1: {
val oldMeter = wtrMeter.state as DecimalType
val oldDate = new DateTime((wtrMeterDate.state as DateTimeType).calendar.timeInMillis)
val newDate = new DateTime(now())
val double daymsec= 86400000
var double daydiff = (newDate.millis - oldDate.millis)/daymsec
// if(daydiff==0) {daydiff=1}
wtrUsedI.postUpdate((newMeter-oldMeter)/daydiff)
wtrUsedD.postUpdate((newMeter-oldMeter)/daydiff)
wtrMeter.postUpdate(newMeter)
wtrMeterD.postUpdate(newMeter)
wtrMeterDate.postUpdate(new DateTimeType(newDate.toCalendar(null)))
logInfo("Meters","Updated water meter")
}
}
end
Works like a charm and is sort of similar to what you want to do.