converting watt values over time to kWh

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.


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.


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 :)

Tags:

Math

Power

Watts