Script Filter
Use this node to evaluate a custom boolean condition against an incoming message. It is the most flexible filter in the rule engine: you write a TBEL or JavaScript function that inspects the message data, metadata, and type, then returns true or false to route the message accordingly.
Configuration
Section titled “Configuration”- Script language — required. Choose
TBELorJavaScript. - Script body — required. A function that receives three arguments and must return a boolean:
msg— the message data (object or array).metadata— the message metadata. In JavaScript, an object where all values are strings; in TBEL, ajava.util.Map<String, String>.msgType— the message type as a string.
Message processing algorithm
Section titled “Message processing algorithm”- Select the script to execute based on the configured Script language.
- Execute the script function.
- Route the message:
- Returns
true→Trueconnection. - Returns
false→Falseconnection. - Throws an exception or returns a non-boolean →
Failureconnection.
- Returns
Output connections
Section titled “Output connections”| Connection | Condition |
|---|---|
True | The script returned true. |
False | The script returned false. |
Failure | The script returned a non-boolean value, or threw an exception. |
Examples
Section titled “Examples”Example 1 — JS threshold check → True
Section titled “Example 1 — JS threshold check → True”Any numeric telemetry value exceeds its corresponding threshold from metadata.
Incoming message
Data:
{ "temperature": 45.2, "humidity": 80, "pressure": 1010}Metadata:
{ "temperatureThreshold": "45.0", "humidityThreshold": "85"}Message type: POST_TELEMETRY_REQUEST
Node configuration
{ "scriptLang": "JS", "jsScript": "if (msgType !== 'POST_TELEMETRY_REQUEST') {\n return false;\n}\n\nfor (var key in msg) {\n var thresholdKey = key + 'Threshold';\n if (typeof msg[key] === 'number' && metadata[thresholdKey] && !isNaN(metadata[thresholdKey])) {\n if (msg[key] > Number(metadata[thresholdKey])) {\n return true;\n }\n }\n}\n\nreturn false;"}Result: routed via True — temperature (45.2) exceeds its threshold (45.0).
Example 2 — TBEL threshold check → True
Section titled “Example 2 — TBEL threshold check → True”Same logic in TBEL; humidity (88) exceeds its threshold (85).
Incoming message
Data:
{ "temperature": 42.1, "humidity": 88, "pressure": 1010}Metadata:
{ "temperatureThreshold": "45.0", "humidityThreshold": "85"}Node configuration
{ "scriptLang": "TBEL", "tbelScript": "if (msgType != 'POST_TELEMETRY_REQUEST') {\n return false;\n}\n\nforeach (key: msg.keySet()) {\n var thresholdKey = key + 'Threshold';\n if (metadata.containsKey(thresholdKey)) {\n var value = msg[key];\n var threshold = parseDouble(metadata[thresholdKey]);\n if (value > threshold) {\n return true;\n }\n }\n}\n\nreturn false;"}Result: routed via True.
Example 3 — JS runtime error → Failure
Section titled “Example 3 — JS runtime error → Failure”The script expects msg.data.temperature but msg.data is undefined.
{ "scriptLang": "JS", "jsScript": "return msg.data.temperature > 20;"}Result: routed via Failure — runtime error accessing undefined.temperature.
Example 4 — TBEL runtime error → Failure
Section titled “Example 4 — TBEL runtime error → Failure”Same — msg.data is null in TBEL.
{ "scriptLang": "TBEL", "tbelScript": "return msg.data.temperature > 20;"}Result: routed via Failure.
JSON schema
Section titled “JSON schema”{ "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "TbJsFilterNodeConfiguration", "type": "object", "required": ["scriptLang"], "properties": { "scriptLang": { "type": "string", "description": "Scripting language used to execute the function.", "enum": ["TBEL", "JS"] }, "jsScript": { "type": "string", "description": "JavaScript function body that must return a boolean. Used when 'scriptLang' is 'JS'." }, "tbelScript": { "type": "string", "description": "TBEL function body that must return a boolean. Used when 'scriptLang' is 'TBEL'." } }, "additionalProperties": false}