OH3 GUI way to set up Item as the average of a channel over past 5 minutes

  • Platform information:
    • Hardware: Raspberry Pi
    • OS: openhabian
    • Java Runtime Environment: default
    • openHAB version: 3
  • Issue of the topic: I have searched for how to use the built-in rr4dj persistence to create an Item that contains the trailing 5-minute average of a value coming from a channel.

How can I do this simply? Step by step if possible? I am a noob!


The answer by moody_blue works. Step by step:

  • Create item that records the high-frequency value (OU_Weather_Actual_Rain)
  • Create item that stores the average value (OU_Weather_Actual_Rain_avg)
  • Copy paste the solution and change cron expression, names, and numbers to match your system.

It cannot be done with just the GUI.

It cannot be done yet with Blockly but stay tuned as lots of good stuff is being added to Blockly.

So you will need to write some code in a Script Action.

Create a rule that triggers when the Item changes.

Add a Script Action and choose Rules DSL as the language.

If your raw Item is named “Foo” and the average Item is named “Bar” you need just this one line.


All of the Persistence calls are documented at Persistence | openHAB

You might need to think a bit more about when to trigger the rule to run the calculation. The Item won’t necessarily change every … well, how often do you want it calculated, exactly? Decision for you.

The number of points averaged will depend on how often your Item is persisted.

Thanks so much for the step-by-step!

The only things missing are to set up an Item to hold the average before going through your steps.

Your call is also missing a “)” at the end.

Alas, even after doing both of those, this doesn’t work for me. I also tried removing the tostring part and adding “rrd4j” as the second argument to averageSince.

This the the spec now:

  - id: "1"
      itemName: HTTPURLThing_OutdoorPM252
    type: core.ItemStateChangeTrigger
conditions: []
  - inputs: {}
    id: "2"
      type: application/vnd.openhab.dsl.rule
      script: fiveMinAveragePM2_5.postUpdate(HTTPURLThing_OutdoorPM252.averageSince(now.minusMinutes(5)))
    type: script.ScriptAction

Open your script out to do some diagnostic on it.
Don’t post your results around, find out what they are first and log them out.

5 mins isn’t very long for rrd4j data - as previously noted, a great deal depends on how often you are recording your data. Which is …?

The frequency is the system default which appears to be about once per minute. How do I check in OH3?

I don’t know what the system defaults are, they didn’t make it to the docs. Sounds about right. Default may include onChange as well, (which would be helpful in your case).

You can interrogate the persistence for raw records using REST API Explorer, for rrd4j use a period like 1 hour to get to look in the ‘recent’ archive. See what’s there.

Here is how I calculate average rain every 15 minutes in the past 2 hours;

configuration: {}
  - id: "2"
      cronExpression: 0 0/15 * * * ? *
    type: timer.GenericCronTrigger
conditions: []
  - inputs: {}
    id: "1"
      type: application/vnd.openhab.dsl.rule
      script: >-
        var work = OU_Weather_Actual_Rain

        OU_Weather_Actual_Rain_avg.postUpdate(work.averageSince(now.minusMinutes(120))as Number)
    type: script.ScriptAction

Hope this helps.

That sentence contains infinities. How specifically does it not work? Errors in the log? As @rossko57 suggested, add a logging statement to log out the result of the call to get the average.