Just to make sure I understand, the sensor counts up over days until the max is exceeded and then it starts over. It doesn’t reset under any other circumstances (time, power loss, etc.), correct?
I once wrote a rule template to handle something similar to this but unfortunately it’s not one that I rewroite for OH 4. It’s been on my list but no one really used it so it’s never been a priority, and there is another rule template that does mostly the same thing only better.
Both are focused on energy reading but the same approach applies here I think. You’ll need a proxy Item for tracking the daily rainfall.
- At a high level trigger the rule on changes to the sensor Item.
- If the current state is > the previous state
a. Add the difference between the previous state and the current state tracking Item - else
b. Add the new value to the current state tracking Item
At midnight trigger a rule to reset the tracking Item to 0, or just let it continue to increase forever. Unless your name is Noah you are unlikely to see enough rain over a lifetime to exceed the maximum value allowed. I’m not sure which will work best for your evolution calculation. I think resetting would still work except between midnight and 1 am perhaps so maybe that needs a work around if you reset it.
This will essentially eliminate that rollover (the following assumes a managed rule and JS Scripting and UoM).
var currReading = Quantity(event.itemState);
var prevReading = Quantity(event.oldItemState);
var total = items.ProxyItem.quantityState;
if(currReading.lessThan(prevReading)) {
total = total.add(currReading);
} else {
const delta = currReading.minus(prevReading);
total = total.add(delta);
}
items.ProxyItem.postUpdate(total);
You will use ProxyItem
in your current rule.
Other notes, time.toZDT()
can make your time code a lot simpler:
const minus_1h = time.toZDT('PT-1H'); // ISO8601 duration string
const start_of_day = time.toZDT('00:00'); // supports AM/PM as well as 24 hour time strings
If you keep the now.withHour(....
approach make sure to add withMillis(0)
or else you could be off of exactly midnight by almost a second.