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!

  • \$\begingroup\$ Without knowing a lot more about the measurements, its hard to give any really useful feedbacck on this question. We could assume you're not attempting to verify your power company's meter - so what have you determined already about the accuracy of your scheme? \$\endgroup\$ May 29, 2016 at 11:56

4 Answers 4


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.

  • \$\begingroup\$ Thanks very much! As you mentioned in another comment, this is home power usage, and my readings are about every second, so that simple equation should be enough for my needs. You are right that in between readings, someone could be flipping some super-lightbulb on and off when I'm not reading it, but I'm gonna bet against that, and assume it'll all come out in the wash (or at least, close enough to be in the same neighborhood as the power bill at the end of the month). \$\endgroup\$
    – Hoopes
    May 29, 2016 at 12:43
  • 1
    \$\begingroup\$ Your math formulas make it look so complicated when in reality it’s just this line of code every time you read a sample: energy = energy + sampleValue * timeSinceLastSample; (with the samples in Watts and the time in seconds the energy will be in Ws, which you can easily convert to kWh) \$\endgroup\$
    – Michael
    May 29, 2016 at 18:04
  • \$\begingroup\$ @Hoopes: If you sample this at about 1 value per second, and we both don't expect things to fluctuate very much in between, what's up with your measurement? \$\endgroup\$ May 29, 2016 at 18:37
  • \$\begingroup\$ Nothing, AFAIK! I was confusing myself, and reading plenty of things that caused me to question the calculations I was doing, so I just wanted some help that would solidify the concepts for me, which this question and its answers did in spades. I can't thank you enough for your attention and help. \$\endgroup\$
    – Hoopes
    May 29, 2016 at 23:00
  • \$\begingroup\$ Follow up question: I don't have any guarantees about length of interval, such that I can make it a constant. If I receive (time/Watt pairs): (0, 1000), (1, 2000), (3, 3000), I actually want the average of my Watt readings - total_power = avg(powers) / 1000 * (end_time - start_time) / 3600 or (2000/1000) * (3/3600) = (2 * .000833) = .001667 kWh . Is that still as accurate as I can manage, given those readings at non-constant intervals? \$\endgroup\$
    – Hoopes
    May 29, 2016 at 23:39

You don't need a python package containing some libraries for integration to do that. As you say, you don't have a function to integrate anyway.

If you have a set of points, just multiply each power value (in Watt) by the time period, and add them all together.

For example, if you get 1kW for 2hours, then 5kW for 30 minutes, it makes 4.5kWh. If you have time values for each measurement point, instead of periods, just substract the previous point time value from the current point time value, you'll have the period.

  • \$\begingroup\$ This will not work if you don't know what the power did between your measurements. Yes, it will give you a number, but that number will just be meaningless. \$\endgroup\$ May 29, 2016 at 12:26
  • \$\begingroup\$ @MarcusMüller Wow. Ok. I assumed the sampling time was appropriately set, wich may not be the case, that's right. Now, you can use all the theorems you want, but if indeed the power goes way up beetween the samples, you won't be able to know anyway... \$\endgroup\$
    – dim
    May 29, 2016 at 12:33
  • \$\begingroup\$ applying the sampling theorem, your statement is disproven: If you know how rapidly the power can maximally change, you can prove that a fixed sampling rate (as explained in my answer) is sufficient. \$\endgroup\$ May 29, 2016 at 12:34
  • \$\begingroup\$ Yes. I just think that OP does not know how rapidly the power can maximally change, that usually power usage for a house or an appliance does not go up and down every second, and that if it does, the power measurement tool smoothes it anyway. Wich are all assumptions, I agree. But reasonable assumptions. \$\endgroup\$
    – dim
    May 29, 2016 at 12:39
  • \$\begingroup\$ 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 \$\endgroup\$ May 29, 2016 at 12:41

First - figure out what the sampling device is actually measuring; in most power measurement devices, the sample is the accumulated energy over the devices sampling period. That means the device is doing the integration for you and all you need do is transform the measurement into kWh. Assuming the sample is measured in seconds and provides a value in Watts:

$$kWh = {sample\ value} \cdot \left(\frac{\frac{T}{1000}}{(60 \cdot 60)}\right)$$

Many devices actually give you accumulated power so you may need to subtract the prior value to get the power over that sample:

$$kWh = (sample(n) - sample(n-1)) \cdot \left(\frac{\frac{T}{1000}}{(60 \cdot 60)}\right)$$

The device may be measuring real power (Watts) or apparent power (VoltAmps or VA). If the device is measuring apparent power, it will likely provide you with the power factor in its output, bear in mind that the sampled powerFactor is also integrated by the device. In that case:

$$kWh = sample \cdot {power\ factor} \cdot \left(\frac{\frac{T}{1000}}{(60 \cdot 60)}\right)$$

All of the above give you the average power flow and not the instantaneous power flow. There are some devices, phasor measurement units for example, that provide accurate apparent power measurements that reveal how dynamic the loads we typically use are. Capacitive (eg dc power supplies) and inductive loads (any rotating machine) cause harmonics and phase shifts as do loads with duty cycles where the load switches on and off within the supply cycle e.g. mobile device chargers. These things are common in households, so beware of statements like the power flow is stable - it probably isnt :)


Interpolate the measurement points as trapezoids and integrate them. (the interpolation kind is what you asked for integration version I think) Integrating them easy, because calculating the area of trapezoids easy.


Then compare your results with an energy meter's results. If there is too much difference, it is out of requirements of Nyquist-Shannon Sampling Theorem which Marcus mentioned before. If they are closely related, you may to try to fine tuning it by improving the interpolation technique.

In the case of Nyquist-Shannon Sampling Theorem (I always copy-paste that :)) fail; you may try to evaluate the data points as long as they are closely related in time, interpolate and integrate them after exceeding a time interval of getting new data (which breaks the theorem and you will avoid that), fill that gap by averaging the previous and next closely related data point packages. And compare with an energy meter as I told before. Good luck.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.