Easy way to average a List?


#1

I have a list of devices with temperatures, is there a short way of averaging the values without looping iterating through the list?


#2

I use this method:

Which outputs this code:


#3

That’s how I had it set previously but one of my ecobee sensor drops off the network intermittently giving me a skewed result.


#4

Hmmm… Maybe add a check to make sure all of the temperatures are within 10 degrees of each other? Or perhaps easier, create an expression and have it ignore any numbers that are not within a certain (expected) range?


#5

I did employ a check then stuff the old value back in the list, now I want to average that list (the lazy way lol) :slight_smile:


#6

Excellent! I do something similar with WUnderground’s data, because it returns bogus data every once in awhile…

I do not think it is lazy at all. You are going out of your way to make sure the data is as accurate as possible.


#7

Ah, yes, I remember that post. Ok, loop it is!


#8

First, I got to say, your last picture looks like it would work right out of the box. :sunglasses:

But I want to clarify, I was not recommending loops. (I use no loops at all for this)

The basic flow I use is very similar to what you have above:

Set variable dataNew = (current data from device or http GET)

If dataNew is inside the range of (dataOld - X) and (dataOld + X)
    Then Set variable dataDisplayed = dataNew
    Else Set variable dataDisplayed = dataOld
End If

The rest of my code goes here based on dataDisplayed

Set variable dataOld = dataDisplayed

Note the first & last lines are in their own block, so they run regardless.
(The last line is basically setting itself up for the next run)

It is a personal preference, but the reason I use 3 variables is so I can easily have a more detailed error in my log, piston state, or notification. (you can get by using only 2 variables, as you elegantly displayed above)

One thing worth mentioning is that my method catches bad data that is too low as well as data that is too high. (bad data from WUnderground happens in both directions)

An advanced version of this:
Sometimes, I embellish my code above, and make ‘X’ a variable as well. When I do that, the threshold can automatically change depending on other factors. (such as +/-20 in the winter, and +/-10 in the summer)

Just a few ideas for you to play with… I am secretly hoping that a future version of webCoRE lets us customize an acceptable threshold (per device) and reuses old data if the new one is outside the range… but until then, you are on the right track!