averageSince from persistence latching to discrete values

  • Platform information:
    • Hardware: virtualized in Proxmox LXC
    • OS: Debian 11
    • Java Runtime Environment: 17.0.6
    • openHAB version: 4.1.1
  • Issue of the topic: I receive heavily fluctuating values from a wind speed sensor via mqtt. The raw data from the sensor are always the discrete values 3.89, 6.61, 9.48, 12.40. Never anything inbetween. At low windspeeds it jumps from 0 to 3.89 and back to 0 after a second or so, longer if there is more wind. To mitigate this issue, I wanted to create a 1min time-weighted average to filter out bursts and get wind speed values inbetween those discrete ones sent by the sensor.
    So I persisted the raw data item on every change and wrote this rule:
rule "Average Windspeed"
    when
        Item windspeed_kmh changed or
        Time cron "0 0/1 * * * ?" // every minute
    then
        logInfo("avg","Average Windspeed: " + windspeed_avg_kmh.state.toString)
        windspeed_avg_kmh.sendCommand(windspeed_kmh.averageSince(now.minusMinutes(1), "jdbc") as Number)
    end

With this windspeed_avg_kmh unfortunately just mirrors the values of windspeed_kmh, latching to the same discrete values and not calculating an average. I tried it with longer durations of now.minusMinutes(5) to no avail.

  • Items configuration related to the issue
Number windspeed_kmh "windspeed[%.1f km/h]" (gPersistEveryChange) {channel="mqtt:topic:mosquitto:Wintergarten:LE_Windgeschwindigkeit_Wintergarten"}
Number windspeed_avg_kmh "windspeed average [%.1f km/h]" (gPersistEveryChange)

What am I doing wrong here?
Best regards,
RockNLol

How often are the raw values saved? If it’s less than every minute or every five minutes then the average is going to be that single value in the time range or the most recently saved value before the time range.

I think for this to work you’ll want to use an everyMinute (or what ever makes sense) persistence strategy and use an average significantly larger than one minute.

Thats also what I thought, which is why I tested with now.minusMinutes(5) as the duration while keeping the persistence to “every change”, which is every couple of seconds. I’ll give you an excerpt of the persistence database of the raw data item:
grafik

as you can see, there are multiple values lasting for a couple of seconds, while the averages-item just mirrors the same value without calculating an average:
grafik

Ok, I tinkered around a bit and swapped the windspeed sensor, which now gives me reasonable values for windspeed in km/h every 15s.

averageSince does still not work as intended though. It seems I have an issue with persistence. I tested the following:

        logDebug("avg", windspeed_kmh.toString)
        logDebug("avg", windspeed_kmh.historicState(now.minusMinutes(30)).toString)

…which logs the following:

12:08:02.149 [DEBUG] [org.openhab.core.model.script.avg    ] - windspeed_kmh (Type=NumberItem, State=14.96, Label=Windgeschwindigkeit, Category=null, Groups=[gPersistEveryChange])
12:08:02.149 [DEBUG] [org.openhab.core.model.script.avg    ] - JdbcItem [name=windspeed_kmh, state=14.96, timestamp=2024-04-01T10:08:02.148+02:00[Europe/Vienna]]

as you can see, even though I requested a historic state 30min earlier, the state that gets returned is the most current one.
The database itself is full of values every 15s.

What can I check to find my error?

It’s hard to tell because you’ve truncated the timestamps of the log statements but the log is occurring at 12:08 but the timestamp on the historic state is 10:08. When persistence looks for an entry, if there is none in the timeframe you’ve specified, it assumes the Item hasn’t changed since the most recent update which, according to this timestamp, was two hours ago.

All I can recommend is to check your timezone settings on OH, the OS and the database.

1 Like

Yes, that was the problem! The DB is running on a different VM which was set to UTC, while openhab was set to Europe/Vienna (GMT+2 atm) in openhab and OS.
Now it is averaging as expected.