RDS
Posts: 820
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Python 3 - How do I create an Average High Value

Mon Apr 16, 2018 11:14 am

I have a program running that monitors the output from my Solar Panels.
In that program, I send the output to a tkinter window and use the high kwhr value to draw one of the needles on a meter.
However, occasionally there must be a spike or something in the system because it records an incorrect (high) value.
In order to overcome this, I would like to take average of (say) the last 5 values and use that calculation to do the comparison.
The code I use currently is shown below, so if the last value is less than 4 and it is higher than the previous high, the last value becomes the new high but I cannot work out the programming change to cover the proposed change to find an average.

Code: Select all

if float(kwhr) < 4:
        if float (kwhrlast) > float (kwhrhigh):
            kwhrhigh = kwhrlast
Any help would be appreciated,

scotty101
Posts: 4491
Joined: Fri Jun 08, 2012 6:03 pm

Re: Python 3 - How do I create an Average High Value

Mon Apr 16, 2018 11:20 am

Have you tried adding the items to a list an calculating the average.

This is untested but may give you an idea.

Code: Select all

kwhrhighList = []
if float(kwhr) < 4:
        if float (kwhrlast) > float (kwhrhigh):
            #Add new high value to a list
            kwhrhighList.append(kwhrlast)
            #Calculate average of the list
            kwhrhigh = sum(kwhrhighList)/len(kwhrhighList)
            
        #Somewhere in the code, limit the number of values in the list, clear each hour for example but keep the last 
        #average as the first element in the newly cleared list.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

hippy
Posts: 14411
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python 3 - How do I create an Average High Value

Mon Apr 16, 2018 11:37 am

You really need to define what it is you are wanting, what an "average high" means in practice, how that is defined.

Not sure what data types you actually have or need because it seems odd to float() what appears to be a non-float and then compare that with an integer.

This, untested, is one way of determining the average of the last 5 values greater than 4 -

Code: Select all

listOfReadings = []

def UpdateAverage(reading):
  global listOfReadings
  if reading > 4:
    listOfReadings.append(reading)
  if len(listOfReadings) <= 0:
    average = 0
  else:
    if len(listOfReadings) >= 5:
      listOfReadings = listOfReadings[-5:]
    average = sum(listOfReadings) / len(listOfReadings)
  return average

RDS
Posts: 820
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Python 3 - How do I create an Average High Value

Mon Apr 16, 2018 9:46 pm

Thank you both for your very prompt replies.
There are some interesting ideas there to try.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand

Re: Python 3 - How do I create an Average High Value

Tue Apr 17, 2018 2:34 am

try to filter the signal first:
A = 0.1 # A = 0.001 .. 1.0 ( strong .. to .. no filter )
B = 1.0 - A

F = Fin * A + F * B

for the HIGH average ( is that a VU PEAK meter ?)
make a second filter FH what also uses the Fin but only if (Fin > F )
FH = Fin * FH_A + FH * FH_B
if (Fin < F) drag that value down to F by a ramp
if ( FH > F ) : FH = FH - FH_downramp

blog
code
picture
this filter i use in Indicator or PID Control loops on the sensor input
but also a high low limit for sensor input
to limit ( or rejection ( like not use in PID )) can be used additionally ( like at broken wire for 4...20mA loops )

Return to “Beginners”