Daily Average of a Data Point
-
Forgive my ignorance of Javascript, but can anyone give me a script example for how to get an average of a data point for each day (with a meta data point)?
I'm looking to turn Outdoor Temperature Readings to Average Daily Outdoor Temperatures.
I appreciate it!
-
Hi Mihai,
I would use a Meta Point with cron on pattern 3 0 0 * * ? which will execute on the third second of every day, then, use Mango's built in book-ending statistics function, previous(), to compute that. So,
return p.previous(DAY).average;
-
Why are you recommending 3 0 0 * * ? and not 3 0 0 * * * as the cron pattern?
From my little research the ? will be substituted with the value of when it first started, which would mean it would only run in the year 2016. Am I wrong?
Otherwise I'll give this a test!
-
You may be correct for some implementations, but we use the CronExpression from Serotonin Timer, which from my brief reading treats ? and * more or less the same. This seems to make sense to me, since the built-in patterns will use ? in the sixth position.
I think the sixth position is "Day Of Week" and can be specified 0-6 or SUN-SAT and the seventh position, one that can be omitted, is the year.
-
You are correct about the cron pattern. I did make one change though, in that I would like to do it right before the day ends.
The reason is that if the previous day is calculated on the following day the data value is entered on the calculated day. Then the average daily temperature would always be a day off.
In any case, my main reason for wanting to do this is to also get the History. However, when I run the "Generate History" I get data points every 7 seconds, which is the rate of actual outdoor temperature sensing.
So how can I also go back in history and get the daily average temperature entered in the data point?
-
You will want to use past() then to avoid the time alignment performed by previous(). Consider,
57 59 23 * * ?
return p.past(MINUTE, 1440).average
To have the history generation only use the cron, you need to uncheck the context update box for the variable.
-
That made it work.
I double checked the values, by manually averaging some points in Excel and they are very close.
I also attached a pretty picture as more proof =D
.Thank you Phil.