JavaScript Node Configuration Guide

Run custom JavaScript code on each incoming payload to transform, calculate, or enrich data.

What It Does

The JavaScript Node lets you run custom JavaScript logic on each incoming payload. It provides full control to manipulate, transform, or enrich the data before passing it to downstream nodes.

Common use cases:

  • Conditional logic not supported by other nodes

  • Complex payload restructuring

  • Device-specific transformations

  • Timestamp formatting or encoding conversions


⚙️ Configuration Options

Field Required Description
Node name Required Unique label to identify the node
JavaScript Templates Optional Choose from a list of ready-made snippets for common transformations
Add Device ID Optional Adds deviceId to the payload automatically as payload.deviceId
Description Optional Internal notes for documentation and maintenance
Error handling action Required

What to do on script failure:
- Continue – Add Error to JSON
- Continue - Ignore Error
- Stop - Log error

 

Step-by-Step: How to Configure the Node

1. Add the Node

Drag the JavaScript Node from the Logic panel to your canvas.

2. Set the Node Name

Use a meaningful label (e.g., Normalize Format, Check Range, Hex Decode).

3. Use a JavaScript Snippet (Optional)

You can start with a predefined script from the Choose Snippet dropdown (see the full list below) to speed up development. These snippets can be used as-is or modified to suit your needs.

4. Write or Customize the Code

Rayven’s JavaScript node requires the input to be parsed into a JSON object and the output to be assigned to the result variable. Work directly on the parsed object using standard JavaScript syntax, then stringify it before assigning it to result.

Example:

var myObject = JSON.parse(input);
myObject.tempC = (myObject.tempF - 32) * (5 / 9);
result = JSON.stringify(myObject);

5. Set Error Handling

Choose between:

  • Continue – Add Error to JSON: The payload continues through the flow, with an error field added.
  • Continue – Ignore Error: The payload continues through the flow, and the error is ignored.
  • Stop – Log Error: The node stops the payload flow on failure and logs the error.

 Available Code Snippets (Predefined Scripts)

Snippet Name Description
Add UID Adds UID from current context to the payload
Add Missing Field Adds a default value if a key is missing
Convert Base64 to Hex Decodes Base64 string to hex format
Convert Binary to Signed Decimal Converts binary input into a signed integer
Convert Joules to kWh Applies energy unit conversion
Delete Array Element Removes a specific entry from an array
Epoch Time in ms Format Converts timestamp to UNIX epoch in milliseconds
Field name has dash or spaces Safely access fields with non-standard keys
Find Distance Between Two Lat/Long Points Calculates geospatial distance
For Loop Template for iterating over an array or values
If field does not exist - add it Adds fallback field with default if missing
Last Element of Array Extracts the last entry from an array
Loop Over Child Elements and Add Value Applies a transformation across all children
Power Factor calculation Calculates power factor using voltage/current/phase angle
Trim Fields from JSON Removes unnecessary or sensitive fields
 

You can insert a snippet and then modify it to fit your specific payload structure.


Example Configurations

Example 1: Capitalize Status

 var myObject = JSON.parse(input);
myObject.status = myObject.status.toUpperCase();
result = JSON.stringify(myObject);

Example 2: Add Device ID to Payload

var myObject = JSON.parse(input);
myObject.deviceId = deviceCode;
result = JSON.stringify(myObject);

Example 3: Add Field If Missing

var myObject = JSON.parse(input);
if (!myObject.temperature) {
    myObject.temperature = 0;
}
result = JSON.stringify(myObject);

Q&A

Q: Do I always need to return the payload?
A: Yes. In the JavaScript node, this means assigning the updated object to result using JSON.stringify(). Without this, downstream nodes won’t receive your updated data.

Q: Can I use external libraries (e.g., lodash, moment)?
A: No. Only native JavaScript is supported in the sandbox.

Q: What happens if there's a runtime error?
A: The behavior depends on your selected Error Handling setting (see Step 5).

Q: Can I write multi-line or nested logic?
A: Yes — full JavaScript (ES5/ES6) is supported including loops, functions, if/else.