Have you considered using the flip-flop node?
You may find it helpful to take a look at this discussion on calculating a moving average: