Trying to create essentially a "circular buffer"

I am writing an app that will average 4 of the last readings and I'm looking for the best method to handle the data.

I thought perhaps a MAP with indices: 1 2 3 4 where I would move the "index pointer" from 1 to 2 to 3 .....etc to write the new reading where to oldest is located. I don't want to do a running average.

As I read more about groovy is see the term "groovy way" to introduce novel approaches to different procedures. Does anyone have a suggestion that would be more efficient than the above?

Thanks
John

There are several Averaging apps available, do you know of them and found they don't work? Bruce created one, and it's out in the Hubitat Repo:

Cobra has an Average All:

I created one using a weighted average:

@csteele Thanks for the info. There is more to my goal making just averaging insufficient.

My final goal is to calculate the % ON time for either my A/C or Boiler. And provide the Total on time for the day.

I think a list is better than a MAP but I'm still trying to get the flow straight in my head :slight_smile:

John