Skip to content
Stand with Ukraine flag

Send RPC Request to a Related Device

This recipe shows how to send an RPC command to a device based on its telemetry and data fetched from a related sensor device.

A Rotating System periodically reports its current turbineDirection.

ThingsBoard:

  • fetches the latest windDirection from the related Wind Direction Sensor
  • calculates whether the turbine should spin left or right to align with the wind
  • sends an RPC command (spinLeft or spinRight) with the rotation angle to the Rotating System

Telemetry from both devices is also aggregated and stored on the parent Wind Turbine asset.

Entities

  • Asset Wind Turbine with type Wind Turbine
  • Device Wind Direction Sensor with type Direction Sensor
  • Device Rotating System with type Rotating System

Relations

  • Wind Turbine -- Contains --> Wind Direction Sensor
  • Wind Turbine -- Contains --> Rotating System
  • Rotating System -- Uses --> Wind Direction Sensor

Rule chains

  • modified Root Rule Chain
  • new Rotating System Controller rule chain

Create one asset and two devices:

Asset

  • Name: Wind Turbine
  • Type: Wind Turbine

Device 1

  • Name: Wind Direction Sensor
  • Type: Direction Sensor

Device 2

  • Name: Rotating System
  • Type: Rotating System

Create the following three relations:

Relation 1

  • From: Wind Turbine (Asset)
  • To: Wind Direction Sensor (Device)
  • Relation type: Contains

Relation 2

  • From: Wind Turbine (Asset)
  • To: Rotating System (Device)
  • Relation type: Contains

Relation 3

  • From: Rotating System (Device)
  • To: Wind Direction Sensor (Device)
  • Relation type: Uses

You can import a ready-made rule chain or build it manually.

Option 1. Import rule chain

  • Download the Rotating System Controller rule chain as JSON file and import it into your instance.

Option 2. Create manually

Add the Message Type Switch node and connect it to the Input node.

Routes incoming messages by type. Only Post telemetry messages proceed to the next node.

  • Name: Message Type Switch

Add the Save Time Series node and connect it to the Message Type Switch node with relation type Post telemetry.

Stores incoming telemetry from Wind Direction Sensor and Rotating System in the database.

  • Name: Save Time Series

Add the Related Entity Data node and connect it to the Save Time Series node with relation type Success.

This node follows the Uses relation from the Rotating System to the Wind Direction Sensor and loads its latest windDirection telemetry into message metadata.

Fill in the fields:

  • Name: Fetch Wind Sensor Telemetry
  • Direction: From originator
  • Max relation level: 1
  • Relation filters:
    • Relation type: Uses
    • Entity type: Device
  • Data to fetch: Latest telemetry
  • Latest telemetry mapping:
    • Source attribute key: windDirection
    • Target key: windDirection

Add the Transformation Script node and connect it to the Related Entity Data node with relation type Success.

This node computes the angular difference between turbineDirection (from the message) and windDirection (from metadata) to determine the optimal spin direction, then builds the RPC payload.

The RPC payload contains two properties: method (spinLeft or spinRight) and params (rotation angle in degrees, rounded to 2 decimal places).

Fill in the fields:

  • Name: New RPC Message

Script:

newMsg = {};
if (msg.containsKey("turbineDirection") && metadata.containsKey("windDirection")) {
value = msg.turbineDirection - metadata.windDirection;
if (value < 0) {
value = -value;
}
if ((value < 180 && msg.turbineDirection < metadata.windDirection) ||
(value > 180 && msg.turbineDirection > metadata.windDirection)) {
newMsg.method = "spinLeft";
}
if ((value < 180 && msg.turbineDirection > metadata.windDirection) ||
(value > 180 && msg.turbineDirection < metadata.windDirection)) {
newMsg.method = "spinRight";
}
newMsg.params = value;
}
return {msg: newMsg, metadata: metadata, msgType: msgType};

Add the Filter Script node and connect it to the Transformation Script node with relation type Success.

This node passes the message only if the transformation produced a valid RPC payload.

Fill in the fields:

  • Name: Check RPC Message

Script:

return msg.containsKey("method") && msg.method != null;

Add the RPC call request node and connect it to the Filter Script node with relation type True.

This node sends the computed spin command to the Rotating System device.

Fill in the fields:

  • Name: Rotating System
  • Timeout: 60

Add the Change Originator node and connect it to the Save Timeseries node with relation type Success.

This node changes the originator from the source device (Wind Direction Sensor or Rotating System) to the parent Wind Turbine asset, so telemetry is stored on the asset level.

Fill in the fields:

  • Name: Change Originator to Wind Turbine
  • Originator source: Related entity
  • Direction: To originator
  • Max relation level: 1
  • Relation type: Contains
  • Entity type: Asset

Step 3.8 Save Timeseries node (Wind Turbine)

Section titled “Step 3.8 Save Timeseries node (Wind Turbine)”

Add a second Save Timeseries node and connect it to the Change Originator node with relation type Success.

This node stores the device telemetry under the Wind Turbine asset.

Fill in the fields:

  • Name: Save to Wind Turbine
  • Input ⇾ Message Type Switch
  • Message Type Switch ⇾ Post telemetry ⇾ Save Time Series
  • Save Time Series ⇾ Success ⇾ Fetch Wind Sensor Telemetry
  • Save Time Series ⇾ Success ⇾ Change Originator to Wind Turbine
  • Fetch Wind Sensor Telemetry ⇾ Success ⇾ New RPC Message
  • New RPC Message ⇾ Success ⇾ Check RPC Message
  • Check RPC Message ⇾ True ⇾ Rotating System
  • Change Originator to Wind Turbine ⇾ Success ⇾ Save to Wind Turbine

Save the rule chain.

Step 3.10 Mark the new rule chain as “root”

Section titled “Step 3.10 Mark the new rule chain as “root””

In the Rotating System Controller line, click Make rule chain root and confirm.

For example, the Rotating System sends turbineDirection = 135 and the related Wind Direction Sensor has windDirection = 100:

value = |135 - 100| = 35
value < 180 AND turbineDirection (135) < windDirection (100) → spinRight

The rule engine sends an RPC command to the Rotating System:

{"method": "spinRight", "params": 35}

To trace execution, enable debug mode on the Rotating System Controller rule chain.

Run both device emulators to publish live telemetry, then import the dashboard to observe the RPC commands triggered by the rule engine.

  1. Download WindDirectionEmulator.js and run it:

    Terminal window
    ACCESS_TOKEN=$ACCESS_TOKEN node WindDirectionEmulator.js

    Replace $ACCESS_TOKEN with the access token of the Wind Direction Sensor device.

  2. Download RotatingSystemEmulator.js and run it:

    Terminal window
    ACCESS_TOKEN=$ACCESS_TOKEN node RotatingSystemEmulator.js

    Replace $ACCESS_TOKEN with the access token of the Rotating System device.

Download the pre-built dashboard as a JSON file and import it. Once open, you should see live wind direction, turbine direction, and the rotation command (spinLeft / spinRight) sent by the rule engine.