Store your average and history length (though you won't be storing a history). Initialise it something like average = 30, historyLength = 5.
When you get a new value you add it to your average which is fairly easy to do:
average = (average*historyLength+newValue)/(historyLength+1);
historyLength++;
But now your history length has increase too so you need to drop an old value which can be done like
average = average*(historyLength-1)/(historyLength - 1)
historyLength--;
But because of how averages work, the average won't change from dropping an 'average value' so you just need to decrement historyLength but since you just incremented it you can simply not increment it to begin with leaving you with just:
average = (average*historyLength+newValue)/(historyLength+1);
The larger the value you choose for history length the less susceptible it is to sudden changes, lower values will be more responsive. It will probably take a very long time to converge if at all.
Here's an example of how it works (image). The blue is the raw values, the red is a long history (20), the yellow is a shorter history (2).