Calculating the average of a variable over a period of time using RTES

Variables

Analog Input : LEVEL

Registers : AVERAGE, TOTAL, RUN, PERIOD, SAMPLES

Rules

RULE AVERAGE=TOTAL/SAMPLES

Task

001 LET TOTALS=0
002 LET SAMPLES-0
003 LET TOTAL=TOTAL+LEVEL
004 LET SAMPLES=SAMPLES+1
005 WAIT INTERVAL
006 IF [SAMPLES<RUN] 003
007 LET LASTAV=AVERAGE
008 GOTO 1

Notes

Say INTERVAL is set at 10 (seconds) and RUN is set at 6. AVERAGE is the average of LEVEL since the start of the 60 (10*6) second period. LASTAV will contain the average for the previous 60 seconds.