## Calculated Properties' ability to work with time and see previous values lets them do powerful conversions

Many IoT applications are about measuring and optimising energy consumption - whether it’s charging a car or heating/cooling a building. So let’s refresh our memory about the difference between power and energy (since 99% of journalists don’t seem to understand this):

Power is pushups-per-minute

Energy is Mars Bars

Power is water flowing from the tank

Energy is the water in the tank

Power is how fast we’re draining the battery

Energy is how full the battery is

Power is how fast the electricity meter spins

Energy is our electricity bill at the end of the month

Power is often measured in kilowatts (kW)

Energy is often measured in kilowatt-hours (kWh)

Calculated Properties let us convert between the power and energy. Perhaps a sensor provides periodic power measurements, and we want to turn those into energy consumed/delivered over the last week. Or a sensor might provide periodic energy updates and we want to work out what the instantaneous power was.

#### Differentiating energy into power

A typical electricity meter has an odometer-style display - a number showing kWh consumed that ticks upwards. So to find the power (the rate at which we’re using energy right now) we need to differentiate - a word that might sound scary but is really very simple: we just need to find the difference between one reading and the next, and divide by the elapsed time.

Imagine that we sit in front of an electricity meter for 3 hours and on every hour we note the odometer reading. Here’s a plot:

Plotting constantly-rising large numbers is rarely useful, so we’d like to turn this into a plot of power. Intuitively, we can see that the power (i.e. the rate of energy use) was lower between Hours 1 & 2 than elsewhere. The maths by which we calculate the average power over that interval is:

And the equivalent Calculated Property expression is relatively simple to construct:

**kW_calculated:**

(kWh - $previous/kWh) / (($ts/kWh - $previous/$ts/kWh) / (1000 * 60 * 60))

The reason for the (1000 * 60 * 60) term is to turn DevicePilot timestamps measured in milliseconds into hours. In this example the readings were taken at regular intervals, but often that’s not the case, for example a reading might get lost in transmission. Even in this case, because we’re keeping track of time, we will still get the calculation right.

#### Integrating power into energy

Conversely, sometimes our sensor might just report power (e.g. a current clamp, or some proxy for power such as light level or heat difference), and we want to turn that into a measure of the energy actually transferred. The simplest way to do that is:

**kWh_calculated:**

kW * (($ts/kW - $previous/$ts/kW) / (1000 * 60 * 60))

That works if the kW property is reporting the *average *power since the previous report. More likely it’s reporting the *instantaneous* power at the moment of this report, in which case we probably want to average it with the previous reading:

**kWh_calculated:**

((kW + $previous/kW)/2 * (($ts/kW - $previous/$ts/kW) / (1000 * 60 * 60))

What’s going on here is that we’re measuring energy as the "area under the curve". Your maths teacher would be proud: