The fundamental kilowatt-hour calculation is E(kWh) = (P(W) × t(h)) / 1000. This is the exact math your utility meter uses to bill you, and the same logic an embedded system runs when logging energy to a dashboard. Whether you are sizing a solar battery bank or building an ESP32-based smart plug, getting the units and integration assumptions right is the difference between a working prototype and a system that drifts by 40% over a month.
The Core kWh Formula and Symbol Definitions
At its core, energy is the integral of power over time. For direct current (DC) or purely resistive alternating current (AC) loads where power remains constant, the base formula is:
E = (P × t) / 1000
Below is the complete symbol definition table. Every variable referenced in this guide maps directly to these symbols.
| Symbol | Name | Standard Unit | Context & Notes |
|---|---|---|---|
| E | Energy | kilowatt-hours (kWh) | The total work done or heat generated over a period. 1 kWh = 3.6 Megajoules. |
| P | Real Power | Watts (W) | The actual power consumed. In AC circuits, P = V × I × PF. |
| t | Time | hours (h) | Must be in hours to yield kWh directly. Convert minutes/seconds first. |
| V | Voltage | Volts (V) | RMS voltage for AC systems (e.g., 120V or 240V nominal). |
| I | Current | Amperes (A) | RMS current for AC. Measured via shunt or Current Transformer (CT). |
| PF | Power Factor | Dimensionless (0 to 1) | The ratio of Real Power (W) to Apparent Power (VA). Critical for inductive loads. |
| C | Cost | Currency ($) | Total financial cost based on utility rate (R). |
Rearranged Forms for Missing Variables
On the bench, you rarely have every variable handed to you. Here are the algebraically rearranged forms solving for each primary variable, assuming constant power draw:
- Solve for Real Power (P):
P(W) = (E(kWh) × 1000) / t(h) - Solve for Time (t):
t(h) = (E(kWh) × 1000) / P(W) - Solve for Current (I) in Single-Phase AC:
I(A) = (E(kWh) × 1000) / (V(V) × t(h) × PF) - Solve for Cost (C):
C($) = E(kWh) × R($/kWh)(where R is the utility rate) - Solve for Power Factor (PF):
PF = (E(kWh) × 1000) / (V(V) × I(A) × t(h))
Assumptions, Unit Traps, and Realistic Magnitudes
When the Formula Applies (and When It Breaks)
The base formula E = (P × t) / 1000 assumes P is constant. This works perfectly for a resistive space heater or an incandescent bulb. However, if P varies—such as an inverter-driven HVAC compressor ramping up and down, or a solar array tracking the sun—you cannot use a single static P value. Instead, you must integrate power over time. In discrete digital sampling (like an ESP32 reading a sensor every second), the applied formula becomes a summation:
E = Σ (P_i × Δt) / 1000
Where P_i is the instantaneous power at sample i, and Δt is the time step in hours (e.g., 1 second = 0.000277 hours).
Unit Mistakes That Break the Math
- Time in Minutes: Plugging 45 minutes directly into
twithout dividing by 60 will inflate your calculated energy by 60x. - Ignoring Power Factor: Using
P = V × Ifor an AC motor ignores PF. You will calculate Apparent Power (VA), not Real Power (W), resulting in a falsely high kWh estimate. Utility meters only bill for Real Power (W) on residential single-phase systems. - Forgetting the /1000: Multiplying Watts by Hours yields Watt-hours (Wh). Forgetting to divide by 1000 breaks the conversion to kilowatt-hours.
Realistic Answer Magnitudes
If your calculation yields 50 kWh for a single appliance in a day, you have a math error. Use this reference table to sanity-check your results against typical residential loads.
| Device | Typical P (W) | Daily t (h) | Realistic Daily E (kWh) |
|---|---|---|---|
| Refrigerator (Inverter) | 150 (avg) | 24 (duty cycled) | 1.0 - 1.5 |
| Electric Space Heater | 1500 | 4 | 6.0 |
| Level 2 EV Charger | 7200 | 8 (session) | 57.6 (per session) |
| LED Lighting (Whole Home) | 80 | 5 | 0.4 |
Worked Examples with Explicit Unit Tracking
Let us walk through two scenarios, explicitly tracking units through every step to prevent scaling errors.
Problem 1: Constant Resistive Load (DC / Single-Phase AC)
Scenario: You run a 1500W portable space heater for 45 minutes. Calculate the energy consumed (E) and the cost if your utility rate (R) is $0.16/kWh (the approximate U.S. national average per the U.S. EIA).
- Identify Knowns: P = 1500 W, t = 45 min, R = $0.16/kWh.
- Convert Time to Hours: t(h) = 45 min × (1 h / 60 min) = 0.75 h.
- Apply Base Formula: E = (P × t) / 1000
- Substitute and Track Units: E = (1500 W × 0.75 h) / 1000 = 1125 Wh / 1000 = 1.125 kWh.
- Calculate Cost: C = E × R = 1.125 kWh × $0.16/kWh = $0.18.
Problem 2: Inductive AC Load with Power Factor
Scenario: A 240V single-phase well pump draws 9.5A with a measured Power Factor (PF) of 0.85. It runs for 2 hours to fill a cistern. Calculate E. (For a deep dive on why PF matters here, see All About Circuits' guide on AC Power).
- Identify Knowns: V = 240 V, I = 9.5 A, PF = 0.85, t = 2 h.
- Calculate Real Power (P): P = V × I × PF
- Substitute: P = 240 V × 9.5 A × 0.85 = 1938 W. (Note: Apparent power would be 2280 VA, but we only bill Real Power).
- Apply Energy Formula: E = (P × t) / 1000
- Substitute: E = (1938 W × 2 h) / 1000 = 3876 Wh / 1000 = 3.876 kWh.
Decision Path: Selecting a Hardware kWh Calculator
If you are building a system to calculate kWh automatically, you need hardware that samples V and I simultaneously to capture PF. Use this decision tree to select the right sensor module for your microcontroller.
| If Your Application Is... | And Your Constraint Is... | Then Select This Sensor IC / Module |
|---|---|---|
| DIY ESP32 Smart Plug (120V/240V AC) | Low cost, isolated measurement, UART/Modbus | PZEM-004T v3.0 (with 100A split-core CT) |
| Whole-Home Panel Monitor | High channel count, consumer app integration | Emporia Vue 2 (or Emporia Gen 3) |
| DC Battery Bank / Solar Logging | High-side DC current, I2C bus, low voltage | INA226 (shunt-based) or Victron SmartShunt |
| 3-Phase Industrial Motor Logging | 3-phase support, RS-485, DIN rail mount | Carlo Gavazzi WM30-96 or similar Modbus meter |
The Default Hardware Pick for DIY AC Energy Logging
If you are wiring up an ESP32-WROOM-32 to log AC mains energy to Home Assistant or an MQTT broker, do not waste time trying to build a zero-crossing detector and analog sampling circuit from scratch. The math required to calculate true RMS and PF in software at 4kHz sampling rates will consume your ESP32's processing overhead and introduce phase-shift errors if your ADC timing drifts.
The definitive pick is the Peacefair PZEM-004T v3.0 module.
The PZEM-004T v3.0 uses a dedicated metrology IC (the ATM9022) to handle the high-speed V and I sampling, RMS calculation, and PF derivation internally. It outputs the final Real Power (P) and cumulative Energy (E) directly via TTL UART (Modbus-RTU protocol).
Wiring & Safety Caveat: The PZEM-004T v3.0 requires a 5V supply for its logic, but its internal optocouplers isolate the mains side from the UART TX/RX pins. Always use the included split-core Current Transformer (CT) for the I measurement. The CT clamps around the hot wire without requiring you to cut the conductor or expose bare copper. Warning: Never open the CT clamp while it is wrapped around a live, current-carrying wire; the secondary winding can generate lethal high-voltage spikes if opened under load. De-energize the breaker, verify dead with a multimeter, clamp the CT, and then re-energize.
By offloading the discrete summation (E = Σ (P_i × Δt)) to the PZEM's internal registers, your ESP32 only needs to poll the module every 5 seconds, read the 32-bit energy register, and push the exact kWh value to your dashboard. This guarantees your software calculations perfectly match the physical reality of the load, eliminating unit-tracking bugs and integration drift.






