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 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 What to do on script failure:
- Continue – Add Error to JSON
- Stop execution (block the payload)
 

Step-by-Step: How to Configure the Node

1. Add the Node

Drag the JavaScript Node from the Logic & ETL group into your workflow.

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 full list below).

4. Write or Customize the Code

Work directly on the payload object using standard JavaScript syntax. Return the updated payload at the end.

Example:

payload.tempC = (payload.tempF - 32) * (5 / 9);
return payload;

5. Add Device ID (Optional)

Enable this option to automatically add the current device’s ID to payload.deviceId.

6. Add Description (Optional)

Use this field to record purpose, expected input/output, or known edge cases.

7. Set Error Handling

Choose between:

  • Continue – Add Error to JSON: Payload continues with an error field

  • Stop: Node halts the payload flow on failure


 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

 
payload.status = payload.status.toUpperCase();
return payload;

Example 2: Add Device ID to Payload

Enable Add Device ID, or use manually:

 
payload.deviceId = '[[deviceId]]'; // if using template
return payload;

Example 3: Add Field If Missing

 
if (!payload.temperature) {
payload.temperature = 0;
}
return payload;

Q&A

Q: Do I always need to return the payload?
A: Yes. Without return payload;, downstream nodes won’t receive 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.

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