[SOLVED] averageSince returns the last value, not the average

I am having some issues with accessing persistence data.

Rules:

val luminanceAvgMinutes = 10
val luminanceThreshold = 500

rule "Check if its dark"
when Item Sensor01_Sensor01Light changed
then
    var luminance_avg = Sensor01_Sensor01Light.averageSince(now.minusMinutes(luminanceAvgMinutes)) as Number 
    logInfo("Status", "Luminance current is " + Sensor01_Sensor01Light.state + ", average is " + luminance_avg + ", threshold is " + luminanceThreshold)
    if(luminance_avg >= luminanceThreshold) { // its dark
        if(Dark.state != ON) {
            Dark.sendCommand(ON)
        }
    } else { // its light
        if(Dark.state != OFF) {
            Dark.sendCommand(OFF)
        }
    }
end 

the log output:

020-02-09 16:55:53.105 [INFO ] [clipse.smarthome.model.script.Status] - Luminance current is 435, average is 435, threshold is 500
2020-02-09 16:57:31.667 [INFO ] [clipse.smarthome.model.script.Status] - Luminance current is 461, average is 461, threshold is 500

As you can see the current value and the ‘average’ are always the same. That is not what i expected …

My persistance data:

http://openhabianpi:8080/rest/persistence/items/Sensor01_Sensor01Light?serviceId=influxdb

{"name":"Sensor01_Sensor01Light","datapoints":"1438","data":[{"time":1581177660162,"state":"200.0"},
{"time":1581177720165,"state":"200.0"},
... 
{"time":1581263520063,"state":"382.0"},
{"time":1581263580065,"state":"409.0"},
{"time":1581263640066,"state":"409.0"},
{"time":1581263700068,"state":"409.0"},
{"time":1581263760070,"state":"435.0"},
{"time":1581263820072,"state":"435.0"},
{"time":1581263880074,"state":"461.0"},
{"time":1581263940075,"state":"461.0"},
{"time":1581264000078,"state":"461.0"}]}

and

http://openhabianpi:8080/rest/persistence/items/Sensor01_Sensor01Light?serviceId=mapdb

{"name":"Sensor01_Sensor01Light","datapoints":"1","data":[{"time":1581263851637,"state":"461"}]}

Could it be that my rules are trying to get the average from the mapdb persistence service instead of the finluxdb ?

It could be. mapdb only stores ONE value. You can specify which DB to use.

2 Likes

Thank you Vincent

Sensor01_Sensor01Light.averageSince(now.minusMinutes(luminanceAvgMinutes,"influxdb"))

The documentation is not so clear. But turns out that also for methods which already have an argument, you can add an optional argument with the persistance serviceId (influxdb here) to use.

Or you can change the defoult persistence service in the paperUI and the rules will use that one by default without the argument

True.
But i have different persistence providers for different groups of items. Don’t want to mess up what is already working. (Until i add the ‘hardcoded’ persistenceId’s in place everywhere).

Created a pull request to add an additional example to the documentation.

1 Like

You might want to make sure you made it to the right branch. Until just a few minutes ago (as of this writing) the URL at the bottom of the page was pointing to the wrong branch in github. I’m sure the maintainers will help with that if that was the case.

1 Like