Arduino energy monitor - Consumption per hour

Hi all,

I have built an arduino energy monitor, the same as described here,  and it works just fine. I have also developed a simple database where I store the measurements and see graphs in a simple php page.
About every 2 seconds I get measurements from the arduino to my local server.
As I am thinking of it, to calculate the total consumption in one hour I should take the mean from the current incoming consumption plus the mean saved consumption and store it back.
To be clear enough, in my database I have a table called current which has 6 fields, among the others there is the currentConsumption field and the meanConsumption field.
So, in every sample I get the meanConsumption is this one:
meanConsumption = ( currentConsumption + meanConsumption ) / 2

where in initialization both current and mean consumption are null and as we move on, the meanConsumption stores the mean of every current consumption sampling taken since that time.
So, in a way like this, I get the total consumption per hour which for example if it is 200 watt I get 0.2KwH, right ?
My problem is that if I turn off the electrical devices for the 30 minutes left ( suppose they were working for the first half hour ) I end up taking 0 consumption because the mean will slowly decrease and decrease and finally reach zero value ( as I store it as integer )
This happens because of the equation:
meanConsumption = ( currentConsumption + meanConsumption ) / 2
which will be
meanConsumption = ( 0 + meanConsumption ) / 2
because of the null consumption for the rest 30 minutes.

Obviously, I am missing something in my thoughts but what ?
Thank you in advance and sorry for my english.
 

Mattia Rossi's picture

Re: Arduino energy monitor - Consumption per hour

Hi Ricardo,

You're indeed missing some basic statistical notions (sorry if I'm too blunt)

Your method for calculating the average is wrong.

You should use this formula: http://upload.wikimedia.org/math/f/d/2/fd2d0cf834c997607df2713883a80d0d.png to calculate a rolling average but I'm afraid it still won't be what you'r after since that will give you the average consumption for all your samples. If you want the average consumption per hour then you'll have to calculate that average only on the samples in the hour you're interested in so you'll have either to reset the counter at the beginning of each hour or compute it from the last 60 minutes worth of samples

If you want to have a look at how that is done just have a look at the sources in emoncms, process model 

Regards

 

 

 

Ricardo's picture

Re: Arduino energy monitor - Consumption per hour

Hi madmatt and thanks for the reply,

I forgot to mention that in every sample I get I track the hour (and compare it with the hour I have stored in the db). When these two hours are different I reset the meanConsumption to zero and continue from the start.
Also, I update the db with the new hour and continue to check in every sample I get.
This happens for day, month and year too. So as you see, when one hour passes, I collect the consumption I have and update another table and with this way I do have a history in my consumption which I can see in graphs and other..
I may not be an expert in statistics but I think and as I clarified the situation, that to get the mean consumption in one hour sampling I will have  to sum, and divide with 2, every sample with the previous one (all taken in that specific hour) or in another way, I will have to sum up all the values of currentConsumption and divide with the number of samples to find the mean consumption per hour and therefore the KwH.
That's my point of view, But my question still remains.
Say I monitor the consumption for one hour, eg 10, and until 10:30 I have the electrical devices in full load, then I unplug them and for the rest half hour ( 10:30 - 11:00) I continue to receive samples which obviously will be null or something bigger due to any possible noise. Then, at 11 o'clock I will have a mean consumption near to zero due to this fact. I think I made clear which my problem is. Do you guys have any idea or method for avoiding this state as it's obviously very inaccurate ?
Thanks in advance.

Mattia Rossi's picture

Re: Arduino energy monitor - Consumption per hour

When you add your last sample to the previous value and divide by 2 what you have isn't an average over your interval, but an average between two samples.

If you want to calculate an average over a time interval you have to count the number of samples you are actually averaging over and use the formula I linked. Using that formula you won't have to read all the previous samples in order to do the average, but simply carry over the number of samples and the current average.

Let's say at 11.00 you turned on a 1 kw  load and sample it every 5 seconds

At 11.30 you turn it off and your average will be 1kw and your counter will be 30*12=360

At 11:30:05 the measured load will be 0 and with your calculation the average would be 0.5kw, while with the formula I posted it will be (0+1*360)/361=0.997kw and at 11:59:55 it will be something like (0+0.501*719)/720=0.5003 kw that will be the actual average consumption over an hour

Ricardo's picture

Re: Arduino energy monitor - Consumption per hour

Aha, I see now the point clear enough.
So in my example the formula should be this one:

$meanConsumption = ( $currentConsumption + $counter * $meanConsumption ) / ( $counter + 1 );

where initial I have $meanConsumption = $currentConsumption = $counter = 0.
Plus those values should me zero in every new hour.
That's the whole story, isn't it ?

Mattia Rossi's picture

Re: Arduino energy monitor - Consumption per hour

Yes , you zero the values and will have to keep separate counters for every interval you want an average for but ... There is a catch ... This simple average is accurate if the time intervals you take your measure on are spaced equally. If for any reason you think you'll miss a lot of measurements then the average should be weighted by the interval between one sample and the previous one in order to keep that into account otherwise your measurements could be skewed by high measurements followed by intervals were the consumption was lower but the measurement wasn't taken (or the other way around)

Ricardo's picture

Re: Arduino energy monitor - Consumption per hour

Yes that's a very good notice.
In my point of view, arduino will always send measurements every 2 seconds so I can't find a way to determine whether I might face a situation like this. Only if arduino fails, or the computer which powers it crash.
In any of these cases I will not have samples at all.
Both are possible, that's for sure, but I currently don't have the time to watch such a detail.
Anyway Mattia, thank you very much for your interest and the time spend to help me out.
Cheers buddy!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.