Skip to content
Try for free

ThingsBoard Cloud

Choose your data region

Your data stays in the region you choose, for residency and compliance. No credit card required.

Rather run it yourself? Install on your own servers

Add & Remove Devices from Group Dynamically

Automatically add or remove a device from an entity group based on incoming telemetry, using the Rule Engine. The group is created on the fly when the first matching device is added.

Example: Place devices that report temperature > 30 into a High temperature group and remove them when temperature drops back to ≤ 30.

  1. Go to Data processing ⇾ Rule chains and click + Add rule chain ⇾ Create new rule chain.
    • Name it Add Device to Group
    • Click Add and open it.
  2. Add a script filter node:
    • Name it Has temperature
    • Set the script to:

      return msg.containsKey('temperature') && msg.temperature != null;
    • Click Add.

    This expression returns true if msg contains the temperature field and its value is not null; otherwise, it returns false.

  3. Add a second script filter node:
    • Name it High temperature
    • Set the script to:

      return msg.temperature > 30;
    • Click Add.

    This expression returns true if the temperature value is greater than 30; otherwise, returns false.

  4. Add an add to group action node:
    • Name it Add to High Temperature group
    • Specify group name pattern:

      High Temperature (${deviceType})
    • Enable Create new group if not exists
    • Click Add.
  5. Add a remove from group action node:
    • Name it Remove from High Temperature group
    • Specify group name pattern:

      High Temperature (${deviceType})
    • Click Add.
  6. Connect the nodes:
    • Has temperature ⇾ True ⇾ High temperature
    • High temperature ⇾ True ⇾ Add to High temperature group
    • High temperature ⇾ False ⇾ Remove from High Temperature group
  7. Save the rule chain.

Alternatively, you can skip the manual setup and import the ready-made configuration: add_device_to_group_rule_chain.json

Step 2. Route Telemetry to the New Rule Chain

Section titled “Step 2. Route Telemetry to the New Rule Chain”
  1. Open the Root Rule Chain.
  2. Add a Rule Chain node:
    • Name it Add Device to Group
    • Select Add Device to Group rule chain as the target.
  3. Connect the existing telemetry path (e.g. the Save Timeseries node output, or a Message Type Switch Post telemetry branch) to this rule chain node.
  4. Apply changes.

When a device publishes telemetry:

Condition Outcome
temperature > 30 Device added to High temperature {deviceType} group (created automatically if needed)
temperature ≤ 30 Device removed from that group

Open the Devices & assets ⇾ Devices page and verify that the device is not assigned to any group (the Groups column is empty).

  • When a device publishes telemetry temperature > 30 ⇾ the device is automatically added to the High Temperature (thermostat) group.
  • When a device publishes telemetry temperature ≤ 30 ⇾ the device is automatically removed from the High Temperature group.