Calculate Motor Runtime Duration
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.
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.
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:
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/motorInactiveDurationin the tworeturnstatements with the keys your dashboards and downstream fields expect. - To read from differently named attributes — change the
Attribute key on the
motorOnTs/motorOffTsarguments. - To output seconds instead of minutes — change the
/ 60000divisor 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.