Skip to main content

function

Execute custom JavaScript code to transform, filter, or generate messages.

Overview

The function node lets you run arbitrary JavaScript code against each incoming message. Write any transformation, calculation, or decision logic that cannot be expressed with built-in nodes. The code receives msg as input and the return value is stored in msg.payload[output_key].

JS
Runtime
Sandbox
Execution
Multi
Output
Context
Access

Properties

Property Type Default Description
code string "" JavaScript code to execute (required)
output_key string "result" Key in msg.payload where the return value is stored
outputs number 1 Number of output ports (return array for multiple outputs)
noerr number 0 Number of errors previously caught (used by editor)

Execution Features

msg Object

Full access to the incoming message

// Read properties
const temp = msg.payload.temperature;
const topic = msg.topic;

// Modify and return
msg.payload.converted = temp * 9/5 + 32;
return msg;

Multiple Outputs

Return an array to send to different outputs

// outputs: 2
if (msg.payload.value > 100) {
  return [msg, null]; // output 1 only
} else {
  return [null, msg]; // output 2 only
}

Context Storage

Persist data across invocations

// Store a running count
let count = context.get('count') || 0;
count += 1;
context.set('count', count);
msg.payload.count = count;
return msg;

Drop Messages

Return null to discard a message

// Filter out low values
if (msg.payload.value < 10) {
  return null; // message dropped
}
return msg;

Example: Temperature Conversion

Convert Celsius readings to Fahrenheit and add a human-readable label.

// Function node configuration
{
  "code": "const c = msg.payload.temperature;\nconst f = c * 9/5 + 32;\nmsg.payload.fahrenheit = Math.round(f * 10) / 10;\nmsg.payload.label = f > 100 ? 'HOT' : f > 60 ? 'WARM' : 'COOL';\nreturn msg;",
  "output_key": "result"
}

// Input message
{
  "topic": "sensors/room",
  "payload": { "temperature": 23.5 }
}

// Output message
{
  "topic": "sensors/room",
  "payload": {
    "temperature": 23.5,
    "fahrenheit": 74.3,
    "label": "WARM"
  }
}

Example: Data Transformation & Filtering

Filter an array of sensor readings and compute aggregate statistics.

// Function node code
{
  "code": "const readings = msg.payload.readings;\nconst valid = readings.filter(r => r.value !== null && r.value > 0);\nconst avg = valid.reduce((sum, r) => sum + r.value, 0) / valid.length;\nconst max = Math.max(...valid.map(r => r.value));\nconst min = Math.min(...valid.map(r => r.value));\nmsg.payload = { avg: Math.round(avg*100)/100, max, min, count: valid.length };\nreturn msg;"
}

// Input: 10 sensor readings (some null)
{
  "payload": {
    "readings": [
      { "id": 1, "value": 22.5 },
      { "id": 2, "value": null },
      { "id": 3, "value": 24.1 },
      { "id": 4, "value": 23.8 }
    ]
  }
}

// Output: aggregated stats
{
  "payload": { "avg": 23.47, "max": 24.1, "min": 22.5, "count": 3 }
}

Common Use Cases

Unit Conversion

Convert between measurement units (C/F, m/ft, kg/lb)

Data Aggregation

Compute averages, sums, min/max from arrays

Payload Reshaping

Restructure JSON for downstream APIs or databases

Conditional Filtering

Drop messages that fail complex validation rules

Related Nodes