Skip to content
Stand with Ukraine flag

Calculate Motor Runtime Duration

  • Calculated Field
  • 0 installs
  • v1.0.0
  • Aug 12, 2026
  • MIT license
  • v4.2+

Computes how long a motor was on or off for each completed cycle — motorActiveDuration when it just switched off, motorInactiveDuration when it just switched back on.

Type
  • Script
Category
  • Custom Formula
Use Cases
  • Industrial Automation
  • Predictive Maintenance
  • Smart Building
  • Smart Home

Who it’s for

Maintenance engineers and IoT builders asking things like “how long did my motor run before it shut off?” or “how long was my generator sitting idle before it started again?” — or anyone turning a pair of on/off timestamps into the actual time spent in each state.

What it does

Reads two server-scope attributes, motorOnTs and motorOffTs — the timestamps of the device’s last on- and off-transitions. Whenever motorOffTs is the newer of the two, it emits motorActiveDuration, the length of the cycle that just ended (on-time). Whenever motorOnTs is the newer one, it emits motorInactiveDuration instead — the length of the off-time that just ended.

calculated-field-calculate-motor-runtime-duration

Each branch only fires once the other timestamp holds a genuine value, not just its 0 default — so the very first transition on a brand-new device never produces a nonsense duration measured back to epoch zero.

How to set up

This field reads motorOnTs and motorOffTs. Pair it with the upstream field that captures those timestamps:

Capture Motor On/Off Times Mykhailo Kornieiev

Install this field on the same device that field is installed on, or on its device profile so every device of that profile is covered, including ones added later. Both arguments have a default value, so no per-entity setup is required before the field starts evaluating.

How to customize

  • To rename the outputs — open the script and replace motorActiveDuration / motorInactiveDuration in the two return statements with the keys your dashboards and downstream fields expect.
  • To read from differently named attributes — change the Attribute key on the motorOnTs / motorOffTs arguments.
  • To output seconds instead of minutes — change the / 60000 divisor in both branches of the script to / 1000.

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.