Skip to content
Stand with Ukraine flag

Data Filtering and Traffic Reduction

This recipe demonstrates an in-vehicle IoT monitoring system connected to ThingsBoard Edge. The device sends readings from 10 sensors — distance, gas consumption, vehicle speed, engine temperature, ambient temperature, tire temperature, and tire pressure (four tires) — to the Edge. The Edge root rule chain filters that data and forwards only the distance readings to the cloud, reducing bandwidth while keeping full sensor history available locally.

Create a device named “In-vehicle monitoring system” on the Edge instance.

  1. Log in to your ThingsBoard Edge instance and go to the Devices page.
  2. Click ”+” in the top-right corner, then click Add new device.
  3. Enter the name “In-vehicle monitoring system” and click Add.
  4. The device now appears in the devices table.

Once the device is created on Edge, verify it has been synchronized to the ThingsBoard server.

Update the Edge Root Rule Chain on the ThingsBoard server to add a transformation script that filters incoming messages and forwards only the distance reading to the cloud.

Here is the final rule chain configuration you are aiming for:

The transformation script creates a new message containing only the distance field:

var newMsg = {};
newMsg.distance = msg.distance;
return { msg: newMsg, metadata: metadata, msgType: msgType };
  1. Log in to your ThingsBoard server. Go to Edge management → Rule chain templates and open Edge Root Rule Chain.
  2. Search for nodes containing “script”. Drag a Script (Transformation) node onto the canvas.
  3. Enter the node name “Transform Incoming Message” and paste the JavaScript snippet above.
  4. Delete the connection between “save timeseries” and “push to cloud”: select the connection, then click X.
  5. Drag a new connection from “save timeseries” to “Transform Incoming Message”. Choose Success and click Add.
  6. Drag a connection from “Transform Incoming Message” to “push to cloud”. Choose Success and click Add.
  7. Click Apply Changes to save.

Now verify that the updated rule chain has been synchronized to the Edge instance.

Get the device access token from Edge, then run the telemetry generator script.

  1. In the ThingsBoard Edge UI, go to Entities → Devices.
  2. Click on “In-vehicle monitoring system” to open device details.
  3. Click Copy access token — the token is copied to your clipboard.

Install the MQTT Python library:

Terminal window
pip install paho-mqtt

Download the telemetry generator script, update the three placeholders at the top, then run it:

import paho.mqtt.client as mqtt
import json
import random
import time
ACCESS_TOKEN = YOUR_ACCESS_TOKEN # REPLACE with your device access token, e.g. 'edge_vehicle'
THINGSBOARD_EDGE_HOST = YOUR_TB_EDGE_HOST # REPLACE with your ThingsBoard Edge host, e.g. 'localhost'
THINGSBOARD_EDGE_MQTT_PORT = YOUR_TB_EDGE_MQTT_PORT # REPLACE with your Edge MQTT port, e.g. 11883 or 1883
DISTANCE = 0
def on_connect(client, userdata, rc, *extra_params):
client.subscribe('v1/devices/me/rpc/request/+')
client.publish('v1/devices/me/telemetry', get_telemetry(), 1)
print(get_telemetry())
def get_telemetry():
return json.dumps(device)
while(True):
device = {
'distance': DISTANCE,
'gas_consumption': random.randint(10, 15),
'vehicle_speed': random.randint(60, 70),
'engine_temperature': random.randint(195, 220),
'ambient_temperature': random.randint(65, 80),
'tire_temperature': random.randint(90, 110),
'tire_pressure_front_left': random.randint(30, 35),
'tire_pressure_front_right': random.randint(30, 35),
'tire_pressure_rear_left': random.randint(30, 35),
'tire_pressure_rear_right': random.randint(30, 35),
}
DISTANCE = DISTANCE + random.randint(0, 100)
client = mqtt.Client()
client.on_connect = on_connect
client.username_pw_set(ACCESS_TOKEN)
client.connect(THINGSBOARD_EDGE_HOST, THINGSBOARD_EDGE_MQTT_PORT, 60)
try:
time.sleep(5)
client.loop()
except KeyboardInterrupt:
client.loop_stop()
print("Exited!")
Terminal window
python mqtt-generator.py

Verify that the device is receiving all 10 sensor readings on Edge:

Verify that only distance readings are pushed to the ThingsBoard server:

Create a dashboard on the ThingsBoard server to visualize the total distance traveled.

  1. Log in to the ThingsBoard server. Navigate to Dashboards.
  2. Click ”+” and select Create new dashboard. Enter the title “Edge Vehicle” and click Add.
  3. Open the “Edge Vehicle” dashboard to begin editing.
  4. Click the Entity aliases icon in the toolbar. Click Add alias.
  5. Set Alias name to edge device, Filter type to Single entity, Type to Device, and select “In-vehicle monitoring system”. Click Save.
  6. Click Add Widget, find Digital gauges in the Widgets Bundle, and select the gauge widget.
  7. Set Type to Entity, Entity Alias to edge device, and Data Key to distance. Click Add.
  8. Open the widget edit panel. On the Appearance tab, set Max value to 10000 and Unit title to MLS. Click Add.

You have successfully sent telemetry from the device, saved all sensor readings on the Edge, pushed only the filtered distance data to the cloud, and visualized it on the dashboard.