Skip to content
Stand with Ukraine flag

Active Device Count Per Site

  • Calculated Field
  • 106 installs
  • v1.0.0
  • Jun 15, 2026
  • MIT license
  • v4.3+

Count total, online, and low-battery devices across a site’s child entities in one CF for site-level health dashboards.

Type
  • Related Entities Aggregation
Category
  • Aggregation
  • Statistical
Use Cases
  • Asset Tracking
  • Industrial Automation
  • Smart City

Who it’s for

Fleet operators, maintenance teams, and on-call engineers asking “how many devices do I have on this site?”, “how many are online right now?”, “how many are running low on battery?” — when the maintenance dashboard needs the headline counts at a glance, not a deep drill-down.

What it does

Lives on the parent site asset. Reads each child’s active server-scope attribute (maintained by ThingsBoard’s Device State service based on the device’s connectivity) and battery telemetry via the Contains relation, then emits three device counts atomically — total, online, and low-battery — from one consistent snapshot.

Site maintenance KPIs diagram

How to set up

This CF lives on the parent site asset. Before it can count, each child device must be linked to the parent via a Contains relation.

Via UI:

  1. Open the parent asset’s details page (Entities → Assets → [asset name]).
  2. Navigate to the Relations tab and select the From scope.
  3. Click + to open the Add relation dialog.
  4. Set type Contains, target type Device, and pick the device to include.
  5. Click Add.
  6. Repeat for each device that should be counted.

When devices are added or removed from the site, create or delete the corresponding Contains relation. The CF automatically adjusts the counts.

For bulk setup across many sites and devices, use the Relations REST API.

How to customize

  • To use a different battery key — change the Time series key on the battery argument to match what your devices publish (e.g. batterybatteryLevel, soc). The active argument reads the TB-managed connectivity attribute and needs no swap.
  • To use a different fallback when no data has arrived — change Default value on the battery argument.
  • To rename the counts for a different domain — change each Metric name (e.g. totalDevicesCounttotalMachinesCount, activeDeviceCountrunningMachinesCount) so downstream dashboards bind to meaningful keys.
  • To count additional conditions — click Add metric and pick a function (COUNT, COUNT UNIQUE, …) plus an optional TBEL filter (e.g. count devices in fault state, count devices below a signal-strength threshold). Each metric defines one output key.
  • To change the low-battery threshold — edit the TBEL filter on the lowBatteryDevicesCount metric (default battery < 20).
  • To follow a different relation type — change the Relation type from Contains to whatever your hierarchy uses (Manages, MonitoredBy).
  • To match your fleet’s reporting cadence — tweak the Deduplication interval (minimum time between aggregations).
  • To timestamp the aggregated counts with the latest source reading’s time instead of aggregation time — toggle Use latest timestamp. Default is on for this template. Turn it off if you’d rather the output ts mark when the aggregation ran on the server.
  • To turn off the database write, WebSocket push, or downstream CF re-evaluation — under Process right away, toggle Save to time series, Save to latest values, Send to WebSockets, or Send to Calculated fields.
  • To filter, transform, or forward the counts to external systems — switch Strategy to Process via Rule Chains. Rule nodes then receive the output and can drop it, change it, save it conditionally, push it to an external system, or anything else the chain defines.

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.