Aggregate daily, weekly and monthly consumption

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"
    Time cron "0 0 1 * * ? *"
  logInfo('Water Meter', 'Rule aggregate water usage with current reading = {}', current_value.state as Number)
  if (now.getDayOfWeek == 1){
  if (now.getDayOfMonth == 1){

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.