Getting last n values of a sensor to create a weighted mean sensor

I’m thinking that if weights followed a geometric series (with ratio -a- less than 1), it’s enough to store the cummulative weighted sum and when a new term arrives, sum it to the previous cummulative sum multiplied by the ratio.

In that case, the sum of the weights is the sum of a geometric series (1/(1-a)) if it has infinite length or (1-a^(N+1))/(1-a) if it has finite length.

With this approach I don’t have to store all the previous values within a period, and with the ratio parameter, a, I can adjust the inertia of the weigthed thermostat.