Skip to content
Stand with Ukraine flag

Calculate Energy Cost from Metered kWh

  • Calculated Field
  • 0 installs
  • v1.0.0
  • Aug 13, 2026
  • MIT license
  • v4.3+

Turns metered kWh into money: applies day/night or flat electricity tariffs to per-interval energy, so dashboards show spend rather than consumption. For tenant billing, energy budgets, submetering, and cost reporting.

Type
  • Script
Category
  • Energy
  • Custom Formula
Use Cases
  • Smart Energy
  • Smart Metering
  • Smart Building

Who it’s for

Energy managers, facility and building operators, and anyone doing tenant sub-metering or cost allocation asking “how much of our spend is on the cheaper night rate?”, “what did this 15-minute interval actually cost?” - when a kWh figure alone does not tell you the money, and a day-and-night tariff makes the answer change with the clock.

What it does

Reads the energyDelta telemetry (kWh used in the interval), picks the day or the night rate based on the local hour, and returns energyCost for that interval. It reads the tariff from server-side attributes on the entity: tariffDay, tariffNight, nightStartHour, nightEndHour, tzOffsetHours, and a currency label that is passed straight through to the output.

Each interval, from the metered energy through the day/night rate to the priced cost:

Calculate Energy Cost from Metered kWh diagram

Downstream, energyCost is the per-interval money you total with a Sum aggregation (see See also), and currency rides alongside as the unit label.

The night window may cross midnight: the default 23 -> 7 means 23:00 through 06:59 bills at the night rate and 07:00 through 22:59 at the day rate. Set tzOffsetHours to your site’s UTC offset so the day/night split lines up with local time (0 keeps everything in UTC).

The field emits a cost only when a fresh energyDelta drove the evaluation, so an attribute change alone never re-bills an interval that was already priced. Cost records are stamped at evaluation (server) time rather than at the exact reading time.

How to set up

Provide energyDelta. This field expects energy used per interval, not a lifetime meter total. If your meter reports a cumulative counter, produce the per-interval delta first with this calculated field, then point this one at its output key:

Calculate Delta from Pulse Counter Dmytro Shvaika

Set the tariff attributes. The defaults (day 0.15, night 0.10, night 23:00 to 07:00, UTC) let it run immediately, but real prices live in server-side attributes so you can change them without editing the field:

  1. Open the device or asset details page (Entities -> Devices -> [device name]).
  2. Go to the Attributes tab and select the Server-side scope.
  3. Click + to open the Add attribute dialog.
  4. Add tariffDay and tariffNight (data type Double, your price per kWh), nightStartHour and nightEndHour (Long, 0 to 23), tzOffsetHours (Long, e.g. -5), and currency (String, e.g. USD).
  5. Click Add.

For bulk setup across many devices, use the REST attributes API.

How to customize

  • For a single flat rate - set tariffNight equal to tariffDay. The hour and timezone no longer matter and every interval bills at the one price.
  • To change the tariff prices or the night window - edit the tariffDay, tariffNight, nightStartHour, and nightEndHour attributes on the entity. The attribute value overrides the argument Default value.
  • To read a different energy key - change the Time series key on the energyDelta argument (e.g. energyDelta -> kwhConsumed, activeEnergyDelta).
  • To rename the outputs - open the script and replace energyCost or currency in the return object with the keys your dashboards and alarms expect.
  • The money is rounded to two decimals in the script (Math.round(energyDelta * rate * 100) / 100.0, confirmed in the calculated-field test panel). Precision is fixed there, not on the output - the Decimals by default control is not rendered for a Script field.
  • To bill against the customer or tenant tariff instead of the device - change Entity type on the tariff arguments to Current owner or Current tenant and set the attributes there.

See also

energyCost is per-interval by design. To show a daily total, add a chart or value widget bound to energyCost and set its data aggregation to Sum over the Day interval - ThingsBoard computes that total at render time from the stored per-interval costs, so the daily figure is a view, never a stored key. Use the same Sum aggregation over a longer interval for per-week, per-month, or per-billing-cycle totals.

Share Your Calculated Field with the Community

Built a useful data transformation? Export it as a JSON from ThingsBoard and publish it to the IoT Hub. Share it with thousands of ThingsBoard developers worldwide.