"safe" AVG function

Ill describe the issue and welcome feedback.

Lets say I have:

Three stats, I pull the temps in as TS1, TS2, TS3. They all read 70F for this example.

(result AVGTEMP = 70)

This works great. No problems.

But what if TS1 decides to falsely report 0 or it is unplugged… mouse chews a wire… worker smashes the stat, etc.

TS1 = 0 or null
TS2 = 70
TS3 = 70

(result AVGTEMP = 46.6)

The average is going to be skewed all the way down to 46.6F

If im using that AVGTEMP to drive other functions all kinds of bad things will start happening. Sure I can bounds check the result and have code in place to make things work somewhat… but this isnt ideal.

Im thinking we need an average function that allows bounds setting or it will disregard the data. Something like this in use:


Where the last two values are minimum and maximum acceptable values. The function should silently drop any passed value below 55 and higher than 85.

SO in my example if TS1 = 0, TS2 = 70, TS3 = 70… SAFEAVG used as above would return 70. Dropping TS1 and not using it in the average calculation. Hopefully I described this correctly.

Im aware this can be done in code. I could write a series of conditional statments to check the data before passing it to the AVG function. Honestly though, doing it in 2000 bytes is going to be difficult at best depending on how many stats I need to poll.

By all means if someone has a better way to do this “safe” average Im all ears.

Statisticians are used to dealing with this and refer to it as ‘trimming the outliers’. In order to avoid tossing out good data you need to do some math to check for values that are outside a certain standard deviation. Rather than get too complicated it will be simplest for now is to keep an eye on the alarms, the system will automatically generate an alarm when a thermistor shows open or short circuit. Any inputs which are configured as thermistors will periodically generate alarms when the controller detects an open or short circuit event as shown below.

You can also fine tune the alarms with your own custom triggers, this statement will generate an alarm when the room temp 101 is lower than 10 Deg C/F for thirty seconds.

Going a step further as you suggest, we could do the SAVG function which will toss out sensors which are open/short but I wonder if there’s something simpler… open to ideas here.

1 Like