InfluxDB2 Persistence: Wrong calculation for Rain using Blockly/Persistence

Hi everyone,

I’m trying to calculate the amount of rain over a specific period (e.g., the last week) using Blockly and InfluxDB2. However, the result I’m getting is incorrect (a negative value), and I suspect a misunderstanding of how the persistence service fetches the data.

My Setup:

  • Item: WeatherStation_RainforYear (stores cumulative rain)

  • Persistence: InfluxDB2

  • Goal: Get the difference between now and 7 days ago.

The Problem:
When I run my Blockly script to get the “historic state delta” , I get a result like -408.3 mm, which is impossible.

Runtime Info:

runtimeInfo:
  version: 5.1.3
  buildString: Release Build
locale: de-DE
systemInfo:
  configFolder: /etc/openhab
  userdataFolder: /var/lib/openhab
  logFolder: /var/log/openhab
  javaVersion: 21.0.10
  javaVendor: Eclipse Adoptium
  javaVendorVersion: Temurin-21.0.10+7
  osName: Linux
  osVersion: 6.12.75+rpt-rpi-v8
  osArchitecture: aarch64
  availableProcessors: 4
  freeMemory: 123541896
  totalMemory: 328204288
  uptime: 498875
  startLevel: 100

Debug Logs:
I enabled DEBUG for the persistence service and found these entries:

org.openhab.persistence.influxdb.InfluxDBPersistenceService
Query-Filter: FilterCriteria [itemName=EasyMeterVerbrauchSumme, beginDate=null, endDate=2026-04-14T00:00+02:00, pageNumber=0, pageSize=1, operator=EQ, ordering=DESCENDING, state=null]

org.openhab.persistence.influxdb.internal.influx2.InfluxDB2RepositoryImpl
Query from(bucket:"openhabDB") |> range(start:-100y, stop:2026-04-07T14:42:59.497000000Z) |> filter(fn: (r) => r["_measurement"] == "WeatherStation_RainforYear") |> keep(columns:["_measurement", "_time", "_value"]) |> last()

Item ‘WeatherStation_Rainfor24h’ updated to -408.3 mm

Observation:
If I run the Flux query from the logs directly in the InfluxDB Data Explorer, I get:
0 WeatherStation_RainforYear 621.2 2022-10-21T16:40:00.276Z

This is the very first value ever recorded in the DB. My current value is 212.9028. The difference between them is exactly the -408.3 I see in openHAB.

It seems the deltaSince or persistedState call is not picking the value from “7 days ago”, but instead falls back to the oldest value in the database because of the range(start:-100y...) filter.

Questions:

  1. Is the start:-100y in the InfluxDB query intended behavior for a “since” request?

  2. Why does it return the oldest value instead of the value closest to the requested timestamp (7 days ago)?

  3. Has anyone else experienced this with cumulative values and InfluxDB2?

Any help or guidance on how to debug this further would be much appreciated!