Aggregate daily, weekly and monthly consumption

Hi,
I have a water meter that report water usage, and I’d like to create a rule that calculates daily, weekly and monthly water usage and store them into different items:

Number current_value "Current Value" (gWaterMeter) {channel="mqtt:topic:water_meter:current_value"}
Number daily_usage "Daily Usage" (gWaterMeter) 
Number weekly_usage "Weekly Value" (gWaterMeter) 
Number monthly_usage "Monthly Value" (gWaterMeter) 

I have created the following rule:

rule "Aggregate Water Usage"
when
    Time cron "0 0 1 * * ? *"
then  
  logInfo('Water Meter', 'Rule aggregate water usage with current reading = {}', current_value.state as Number)
  daily_usage.postUpdate(current_value.deltaSince(now.minusDays(1)))
  if (now.getDayOfWeek == 1){
    weekly_usage.postUpdate(current_value.deltaSince(now.minusWeeks(1)))
  }
  if (now.getDayOfMonth == 1){
    monthly_usage.postUpdate(current_value.deltaSince(now.minusMonths(1)))
  } 
end

It doesn’t seem to work and I receive the following error:

2022-04-01 19:53:21.932 [INFO ] [penhab.core.model.script.Water Meter] - Rule aggregate water usage with current reading = 1275925
2022-04-01 19:53:21.933 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'WaterMeter-1' failed: The argument 'state' must not be null. in WaterMeter

The error doesn’t indicate what’s wrong, any suggestion where to look? Thanks!

Split it up and log the parts. Instead of trying to pull the delta and postUpdate on the same line, pull the delta and log what comes back. The error is complaining about the value that’s returned by the deltaSince.

The first thing that came to mind: is current_value persisted onto your default persistence service? Which persistence service is it and what is the frequency / strategy?

I guess you don’t have persisted values a month back? Hence, the delta fails.