Script Switch


The Script Switch node routes the incoming message according to the results of the written JavaScript.

Node Type

Filter.

Input and Output Ability

This node has 1 entry point and 3 exit points. The routes of the 3 exit points will be according to the returned value of the written script, namely True, False, and Failure.

Node Properties

../../../_images/script_switch.png


Settings Tab

Name

The name for this node.


Description

The description for this node.

Script Tab

The JavaScript (ES8 supported) for routing the incoming message. The supported function is transform with two parameters, msg and metadata. The msg is the input message, and the metadata can be the MQTT topic for example, which can be used in the script.


Click the Test button to test your JavaScript by entering the input message to see if your JavaScript works as designed.

Limitations

  • Does not support Javascript ES6.

  • The script must return one of these 3 values: true, false, or failure.

Samples

Input Sample

{
  "MetaData": {
    "messageType": "PostAttribute",
    "assetId": "assetId",
    "deviceKey": "deviceKey",
    "productKey": "productKey123",
    "orgId": "o16109619073861404"
  },
  "Body": {
    "location":"Shanghai"
  }
}

Output Sample

The output will be the same as the input as this node does not modify the message.

Script Sample

// The productKey needs to belong to either <the product entered through the Rule Entry node> or <the product bound to the gateway in Rule Entry node>
// At the same time, the input msg has "Shanghai" as the value for location
if (metadata.productKey == 'productKey123' && msg.location == 'Shanghai') {
  return true;
}

//location is not defined in the input msg
if (typeof msg.location === 'undefined') {
  throw new Error('location not defined in msg');
}

//When neither of the above two conditions are met
return false;