Calculating mean, median and standard deviation

You may want to smooth out incoming data from sensors, looking for mean and median values of a field, and for the standard deviation.


Let's say your incoming data has a field called pressure. Then let's assume you want to create some fields for the mean, median, and SD. 

First go to the schema, and then into its metrics (scheduled calculations which are performed every few minutes). Create fields pressureMean, pressureMedian and pressureStdev like this:

Google ChromeScreenSnapz2316.jpg

Then in the calculation logic, enter code like this:

# Query the historic data from midnight to midnight of each day (this is for a Daily-reporting calculation
# that runs with a frequency of Daily or more frequently.
# nodeId passes the current data pool ID or asset ID to the query.
cursor = query(fromTimestamp, toTimestamp, nodeId)

pressures = []  # Declare an array
for (row in cursor) {  # Iterate through the values
    if ( is not null) {
        pressures[] =

setValue('pressureMean', mean(pressures))
setValue('pressureMedian', median(pressures))
setValue('pressureStdev', standardDev(pressures))

It's a good idea to perform a dry run before saving, to ensure you've got data coming through and it's going to be calculated correctly. (Remember that the dry run happens instantly, but is not saved, whereas metrics will be run on the next scheduled run, which could be some minutes later).