Skip to content

Share temperature conversion between a thing model and an RPC ​

This device represents temperature in tenths of a degree Celsius. An uplink value of 234 should appear as 23.4 °C in telemetry. A setpoint of 23.4 °C should put 234 back on the wire. In ThinkLink both scripts can use one server plugin.

Create and test the draft ​

  1. Open Advanced → Plugin → Add in the tenant. In the basic information tab enter “Temperature conversion” and the identifier a123. Select the server runtime and enable the thing model and RPC scopes. System administrators can manage PUBLIC plugins under System Platform.
  2. Register two public functions in the script and test tab. Each receives one JSON input and returns a calculated value.

Name, identifier, runtime and version controls in the plugin workspace

javascript
definePlugin({
  decode({ raw }) {
    return { temperature: raw / 10 };
  },
  encode({ temperature }) {
    return { raw: Math.round(temperature * 10) };
  }
});
  1. Select decode, enter {"raw":234} and click Save and test. Expect {"temperature":23.4}. Select encode and test {"temperature":23.4}. Expect {"raw":234}. These tests run the draft.
  2. Publish and enable the version. Check the running version and enabled state in the list. Publishing alone preserves the previous running version. Calls fail if the plugin is disabled, lacks an active version, or its published apply_to excludes the caller.

Call the plugin from a model and an RPC ​

For a fixed algorithm use await invokePlugin({ plugin: "a123", callback: "decode", input: { raw: 234 } }). Thing models, RPCs, triggers and forwarders use this signature. Select the applicable scopes from thing-model, rpc, trigger and forwarder, or select ALL alone. Supply the bare id_name. The runtime looks in the calling tenant first, consulting PUBLIC only if that identifier is absent. A forwarder uses its script’s tenant. Script calls return the calculated value directly. The HTTP endpoint /plugin/invoke-edge wraps its result in output.

To select algorithms per device, merge the following fields into its server attributes and read them back. Preserve other attributes. A second device can select its own enabled plugin and public functions while keeping the same model and RPC.

json
{"plugin":"a123","callback":{"thing-model":"decode","rpc":"encode"}}

After extracting the numeric raw field from the frame, the model reads the binding and merges the result into the telemetry object it is already building.

javascript
const { plugin, callback } = device.server_attrs;
const values = await invokePlugin({
  plugin, callback: callback["thing-model"], input: { raw }
});
Object.assign(telemetry_data, values);

The RPC reads the same device’s binding. Its existing frame builder, downlink and reply handler consume the resulting integer. The example takes params.temperature in °C.

javascript
const { plugin, callback } = device.server_attrs;
const { raw } = await invokePlugin({
  plugin, callback: callback.rpc, input: { temperature: params.temperature }
});

When adding an optional hook, keep the existing algorithm for unbound devices. If a configured callback is missing or fails, raise an RPC error before sending. Migrate old scripts by removing local/ and PUBLIC/ prefixes and replacing invokeDevicePlugin and legacy plugins instance calls with explicit calls. The business script owns HTTP, telemetry writes and device commands.

Diagnose two plugins independently ​

Both running versions must allow RPC and expose a decode function accepting this input. This diagnostic RPC calls each in order, records its result or error and returns an empty action list. Inspect the log by identifier. The second plugin should return 23.4 if it uses the tutorial algorithm.

javascript
for (const plugin of ["qa-plugin-20261118", "a123"]) {
  try {
    const output = await invokePlugin({ plugin, callback: "decode", input: { raw: 234 } });
    logger.info(plugin, { params: { output } });
  } catch (error) {
    logger.info(plugin, { params: { error: String(error) } });
  }
}
return [];

For an end-to-end check, send a known sample from each device and read back each telemetry value and unit. Use the same RPC to set a temperature and compare the encoded integer, downlink frame and device reply for each binding.