English
RPC execution control and retries
Helper integration and executor actions are separate layers. See tklHelper for the model entry, feedback conditions and existing mappings. Its default retry count is 2 and the maximum is 5; the depth and deadline limits below still apply.
This guide describes parsePayload, setBatch, cancelRpc, delays, cancellation and retries. Scripts still return action arrays. See RPC Model for setup. Replace example identifiers with actual bound RPCs, models and devices.
Action reference
| Action | dnMsg | Purpose |
|---|---|---|
parsePayload | { msg, thing_model_ids?, thing_model_id_names? } | Run raw input through thing-model parsing and subsequent data processing. |
writeTelemetry | { thing_model_id?, thing_model_id_name?, data } | Write parsed fields, skipping the parser script and retaining downstream telemetry processing. |
setBatch | { batch_id } | Declare an execution batch for deduplication while running. |
cancelRpc | { scope, id?, id_name?, batch_id? } | Cancel active executions for a device, RPC or batch. |
Business-action target defaults to the calling device EUI and can select another device in the same organization. setBatch cannot have a target. Use device.parent when the downlink protocol requires parent routing; parsing, telemetry and cancellation should target the device to be processed.
Per-action waiting
sleepTimeMs waits before the current action, defaulting to 0. Actions are handled sequentially; delays are not absolute offsets from RPC start. Delays of 1000, 2000 and 3000 ms start actions at approximately seconds 1, 3 and 6, plus preceding processing time.
For normal downlinks (default or omitted type) after the first business action, the wait is at least 100 ms: max(sleepTimeMs, 100). setBatch is removed before execution and does not count toward action spacing. Cancellation wakes a pending wait; it cannot recall commands already sent.
Batches and retries
An array may contain at most one setBatch. Its batch_id must be a nonblank string; target and nonzero delays are invalid. The executor scans declarations after the script returns and before any business action, regardless of declaration position. Without a declaration, it generates a UUID.
The key is organization + calling-device EUI + actual RPC ID + batch_id. If the same process has an unfinished, uncancelled execution holding this key, the newer claimant is rejected with system_warn; the older execution continues. The key can be reused after completion, failure or cancellation. It is neither permanent idempotency nor device-success confirmation. Inline HTTP calls inside the script occur before batch checks and are not protected by this deduplication.
Before retrying, establish whether the original execution is still active, whether the operation is idempotent, and what the device reports. An active identical batch is rejected; an ended batch can run again and repeat side effects. Read back state before retrying a write with an unknown outcome. To replace an older execution, use a new batch ID or omit setBatch, then cancel the older execution.
Cancel all active RPCs on a device
javascript
return [{
type: "cancelRpc",
target: device.eui,
dnMsg: { scope: "device" }
}];Device scope rejects id, id_name and batch_id.
Cancel a specific RPC
javascript
return [{
type: "cancelRpc",
dnMsg: { scope: "rpc", id_name: "control_light" }
}];Use id: "actual-rpc-database-id" instead of id_name if needed. RPC scope rejects batch_id. id takes precedence over id_name; an invalid ID never falls back to the name. When both are supplied, ID selects the RPC. Names follow device RPC lookup order (built-in first, then a bound match), and are not display names. The selected RPC must be available and bound to the target device.
Cancel a specific batch
javascript
return [{
type: "cancelRpc",
dnMsg: { scope: "batch", id_name: "control_light", batch_id: "batch-001" }
}];Batch scope requires both an RPC selector and a nonblank batch_id; it does not cancel across RPCs by batch ID alone.
Replace an older execution
This script belongs to control_light. The caller supplies params.request_id as a nonblank ID different from the active older batch. The attribute action illustrates subsequent work; substitute the required protocol downlink as appropriate.
javascript
return [
{ type: "setBatch", dnMsg: { batch_id: params.request_id } },
{ type: "cancelRpc", dnMsg: { scope: "rpc", id_name: "control_light" } },
{ type: "modifyAttrs", sleepTimeMs: 0,
dnMsg: { server_attrs: { desired_light: params.desired_light } } }
];Cancellation excludes the execution issuing it. No match is a success. Do not reuse an occupied batch ID: the batch check runs before cancelRpc, so it would reject the new execution before cancellation. Replacement is not transactional: old commands may still reach the device, and concurrent replacements have no cross-instance ordering guarantee.
Three timing mechanisms
| Mechanism | Waiting or scheduling unit | Effect of cancelRpc | Prevent future calls |
|---|---|---|---|
| RPC action delay | sleepTimeMs inside the current action array | Wake the wait and stop subsequent actions. | Does not change other RPC entry points. |
| Trigger-delayed call | A registered RPC call awaiting its trigger delay | Device/RPC scope can cancel it; a batch cannot match before the script declares it. | Disable or adjust the trigger rule separately. |
| Cron periodic task | Cron schedule and bound device actions | Affect registered RPC executions only, without changing the schedule. | Disable the relevant scheduled task or device action. |
Cancellation matches the organization and EUI at RPC invocation, not each action's target. If device A invokes an RPC that sends an action to B, cancelling B does not cancel A's RPC. Disabling Cron prevents future dispatch; it does not cancel an already started RPC. Handle both separately when both must stop.
Execution registrations, delayed waits and batch claims live only in the current Node.js process. Restart does not resume executions; instances have no shared lock. Saved Cron configuration is distinct from unfinished executions. Use device-affine routing or additional shared coordination for multiple instances; this implementation is not a distributed lock.
Cancellation is cooperative: waits wake and action checkpoints stop further execution. Returned axios actions receive an AbortSignal, which does not guarantee the remote server has not processed the request. Commands already sent, data already written and arbitrary running user scripts cannot be recalled or rolled back. Cancellation forces system_warn logging, including RPC, EUI, execution ID, batch and cancelling RPC, even when routine RPC logging is disabled.
Raw parsing versus direct telemetry
javascript
return [{
type: "parsePayload",
target: device.eui,
sleepTimeMs: 0,
dnMsg: {
msg: { temperature: 25 },
thing_model_ids: ["actual-thing-model-id"],
thing_model_id_names: ["temperature"]
}
}];msg must be an object shaped like the decoded MQTT object passed to a parser; required fields depend on the model script. RPC selectors belong inside dnMsg. HTTP parse-payload uses request-level selectors instead.
thing_model_ids and thing_model_id_names form a deduplicated union executed in device binding order, not selector order. Every selector must uniquely match an available bound model. Missing, unauthorized, unbound or ambiguous matches fail. Explicit arrays must be nonempty and contain nonblank strings; omitting both selects all bound models. If the example ID and name select the same model, it runs once; if they select different models, both run. Selection neither changes bindings nor propagates to subsequent parsing by associated virtual devices.
parsePayload updates device activity time and enters thing-model parsing, result processing, and configured telemetry/attributes/history, forwarding, triggers, virtual devices and protocol notifications. writeTelemetry takes already parsed data and skips parser scripts. Its singular thing_model_id and thing_model_id_name must still agree when both are supplied; the union rule does not apply to them.
A selector or parsing failure stops remaining RPC actions and forces system_error logging. Activity updates, earlier model outputs and notifications already emitted are not rolled back. Read actual state before retrying. See HTTP data parsing and direct writes.
Ten levels per call branch
Each RPC entry or thing-model parser entry counts as one level, starting at 1. Level 10 runs; level 11 is blocked before its script executes. Triggers do not add a level. Delays, derived actions and virtual devices inherit context; parallel branches count independently. An over-limit branch stops while independent branches continue. This is not a limit of ten actions or ten distinct models.
The limit forces RPC system_warn logging with trace_id, the ordered RPC/model path, actual IDs, names and EUIs. Completed side effects remain. A new external HTTP/MQTT message starts a new flow; device replies without trusted correlation cannot automatically continue the original path. Use business-state change conditions or rate limits to prevent feedback loops.
Some forwarding, triggers and derived actions run asynchronously; RPC success does not prove all downstream notifications completed, history buffers flushed or physical-device actions succeeded.