As developers, we spend our days thinking about systems, rates, and consumption (like API calls or memory usage). Your electricity bill is just another system with its own set of variables and logic. Let's break it down.
The Variables: Watts, Joules, and BTUs
-
Joule
: Think of this as a single unit of work, the base integer. It's too small to be practical for billing. -
Watt
: This is a rate. It's not a quantity; it'sJoules/second
. In programming terms, it’s not the total data, but the throughput (e.g., MB/s). An appliance's Wattage is its fixed consumption rate. -
BTU
: A domain-specific unit for thermal energy. It's like a library you'd only use for a specific task (heating/cooling).1 Watt ≈ 3.41 BTU/hr
.
The Core Logic: Calculating Consumption (kWh)
You're not billed for the rate (Watts), you're billed for the total consumption over time. This is the kilowatt-hour (kWh).
The function is simple:
function calculateKwh(wattage, hours) {
// A kilowatt is 1000 watts
const kilowatt = wattage / 1000;
const kwh = kilowatt * hours;
return kwh;
}```
If your electric rate is $0.15/kWh, the cost function is just calculateKwh(wattage, hours) * 0.15.
Why This Matters
Understanding this logic allows you to identify the most "expensive" functions in your home's "runtime." It's not always the device with the highest wattage, but the one with the highest wattage that runs for the longest time.
I wrote a more detailed, plain-language guide on this that breaks down the concepts for everyone. You can check it out here, and the site itself has some useful unit conversion tools.
Full Article -> https://www.unitly.info/en/energy
Top comments (0)