How to use arrays in rules under DSL?

This is a genuinely embarrassing rookie question but:

I’m trying to use some form of arrays of numbers i DSL but I can not figure out how to do it. Whatever I try, the interpreter keeps barking at me… I could switch to JSR223 if need be but now that I started using DSL I figured I would solve it. Have searched the community and documentation without success.

The background is that I’m reading wind data and wanted to collect a number of samples over, say, a minute in order to estimate the variance in the heading of the wind. Something like:

// Estimate the variance of wind heading
 var jsonDataHeading[10]  <- this is obviously wrong...
 var n = 0
 while((n=n+1) > 10) {
    jsonDataHeading[n] = sendHttpGetRequest("https://"+ serverIP + ":" + ....
    Thread::sleep(5000)
}
Calculate variance here...

So, how do I use arrays in openHAB? :blush:

The Rules DSL doesn’t support arrays. You can create an ArrayList but not arrays.

But typically when one is trying to use arrays like this, there to a better way to do it in Rules DSL. In this case, I think you can get the values you need using persistence and called like sumSince.

If not, then you need to create an ArrayList instead of an array.

Thank you Rich!
At least, I can stop thinking arrays. I will look into the alternatives you suggest later today. I need to figure out how to use persistence in a good way; there is no need to save the raw data for longer time than it takes to calculate the SD (or variance).

If you would use rrd4j as a persistence service AND configure it to your specific needs you could have it persist a fixed amount of values. Something like a value for each of the last 1440 minutes. No need for the “feared” data-consolidation in such case and the database does never grow.
If you need help with rrd4j for such a case, just ask.

2 Likes

Apart from the suggestions already made, you really shouldn’t do this:

As explained here:

1 Like

Very interesting insights in the workings of openHAB and god advices. Thank you Rolf and Jürgen.