# Electronic – converting watt values over time to kWh

mathpowerwatts

This is probably an elementary question, but I wanted to be sure.

I have a system that reads instantaneous watts values, and stores them as "events", with a power value and a time value.

According to the answer here (Should I multiply by time to determine Watt-hours?) it's simply an "take an average of the power readings, then multiply by time" to get it. That's the crude version, then calculus is mentioned.

Unfortunately, most of my calc has left me behind, and I'm not sure what type of integration to do over a set of points that doesn't really have any rhyme or reason (ie, f(x) is not just x^2, it's kind of all over the place).

Is the integration version that much more accurate? If I were trying to calculate the kWh over time for my house, and match it as close as possible to what the power company bills me for at the end of the month, what would be best? Is the average * time good enough?

FWIW, I'm in python, and looking at http://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html . I'm assuming (again, calc was quite a while ago) that I'd use the trapz version, since the others appear to need a function, and trapz (and simps?) appear to work on sample-style data.

Thanks very much for any help!

If you can only take measurement at discrete times, then summing up and dividing by the time between measurements is the only way possible – the integral

$$E_\text{total}=\int\limits_{T_\text{start}}^{T_\text{end}} P(t) dt$$

really collapses to a sum, it \$P(t)\$ is only known for set of points. For example, assume the power value is constant for amount of time that you spend between your \$N\$ individual measurements, let's call that \$T_\text{sample}\$, then

\begin{align} \tilde E &= \sum\limits_{n=0}^{N-1} T_\text{sample}\cdot P(nT)\\ &= T_\text{sample} \sum\limits_{n=0}^{N-1} P(nT)\\ \end{align}

Now, you say

a set of points that doesn't really have any rhyme or reason

Well, that's a problem. What if the power goes up between two measurement points, and just happens to be low every time you're actually taking note?

The answer to this problem is the Nyquist-Shannon Sampling Theorem, which is quite handy in a lot of signal processing applications, but in this case it means:

If you have a real signal (here: your power measurements) whose highest frequency is \$f_\text{max}\$, then you will need to look with twice that frequency at it to be sure not to miss anything, i.e. \$f_\text{sample}\ge 2f_\text{max}\$.

Frequency here means the amount of time between two consecutive events. That means that if you can say "the shortest power fluctuation I need to consider is \$T\$ long (e.g. 5 second)", then your signals highest frequency \$f_\text{max}= \frac1T\$ (i.e. 0.2 Hz in the 5s case), and you'll need to sample twice that often, so \$f_\text{sample} \ge 2f_\text{max}=\frac2T\$, or considering the sampling interval \$T_\text{sample}=\frac1{f_\text{sample}}\le \frac12 T\$.

If you sample slower than that, your measurement is not representative for your observed (unless you have another, restricting model for how the power consumption fluctuates, which you don't seem to have), and no statement can be drawn from your set of measurement points.

If you then have the measurements in a sensible, constant time interval, just adding them up and multiplying the result with that interval will give you your total energy reading. You don't need any special python modules for that, i.e. simply

 ### assuming "powers" is a list / iterable of your power measurements in Watt,
### and "T" contains the sample interval in seconds

total_power = sum(powers) / 1000 * T / 3600


will give you your kWh.

Now, you might say "how should I know how fast my appliances turn on and off?"

In many scenarios, you can put a sensible limit to power fluctuation. For example, sure, lights might switch on and of within fractions of a second, but the amount of power consumed by quickly switched off lights (e.g. toilet usage, turn on, 60s, turn off) is probably negligible, whereas things that really matter (fridge, water heater, washing machine, oven) tend to change relatively slow in a typical home usage scenario.