0
# Function Nodes
1
2
Function nodes provide data processing, transformation, and custom logic execution capabilities. These nodes enable complex data manipulation, conditional routing, and custom JavaScript code execution within Node-RED flows.
3
4
## Capabilities
5
6
### Function Node
7
8
Execute custom JavaScript code with access to Node-RED utilities and optional external npm modules. Supports sandboxed execution with full message processing capabilities.
9
10
```javascript { .api }
11
/**
12
* Function node for custom JavaScript execution
13
* Registers as node type: "function"
14
*/
15
function FunctionNode(config) {
16
RED.nodes.createNode(this, config);
17
18
// Configuration properties:
19
// config.func - JavaScript function code
20
// config.outputs - Number of outputs (default 1)
21
// config.libs - Array of external libraries/modules
22
// config.timeout - Execution timeout in seconds
23
}
24
```
25
26
**Usage Examples:**
27
28
```javascript
29
// Function node configuration with external modules
30
{
31
"id": "function1",
32
"type": "function",
33
"func": "// Access external modules\nconst moment = global.get('moment');\n\nmsg.timestamp = moment().format();\nreturn msg;",
34
"outputs": 1,
35
"libs": [
36
{
37
"var": "moment",
38
"module": "moment"
39
}
40
],
41
"timeout": 30
42
}
43
44
// Multi-output function
45
{
46
"func": "if (msg.payload > 100) {\n return [msg, null];\n} else {\n return [null, msg];\n}",
47
"outputs": 2
48
}
49
```
50
51
### Switch Node
52
53
Route messages based on conditional rules. Supports multiple routing conditions with various comparison operators.
54
55
```javascript { .api }
56
/**
57
* Switch node for conditional message routing
58
* Registers as node type: "switch"
59
*/
60
function SwitchNode(config) {
61
RED.nodes.createNode(this, config);
62
63
// Configuration properties:
64
// config.property - Property to test (e.g., "payload", "topic")
65
// config.rules - Array of routing rules
66
// config.checkall - Check all rules or stop at first match
67
// config.repair - Repair message sequence
68
}
69
70
// Rule structure for switch conditions
71
interface SwitchRule {
72
t: string; // Rule type: "eq", "neq", "lt", "lte", "gt", "gte", "btwn", "cont", "regex", etc.
73
v: any; // Value to compare against
74
vt: string; // Value type: "str", "num", "bool", "json", "msg", "flow", "global"
75
v2?: any; // Second value for between operations
76
v2t?: string; // Second value type
77
}
78
```
79
80
**Usage Examples:**
81
82
```javascript
83
// Switch based on payload value
84
{
85
"property": "payload",
86
"rules": [
87
{"t": "gt", "v": "100", "vt": "num"},
88
{"t": "btwn", "v": "50", "vt": "num", "v2": "100", "v2t": "num"},
89
{"t": "lt", "v": "50", "vt": "num"}
90
],
91
"checkall": false,
92
"repair": false
93
}
94
95
// Switch based on message topic
96
{
97
"property": "topic",
98
"rules": [
99
{"t": "eq", "v": "temperature", "vt": "str"},
100
{"t": "eq", "v": "humidity", "vt": "str"},
101
{"t": "regex", "v": "sensor.*", "vt": "str"}
102
]
103
}
104
```
105
106
### Change Node
107
108
Modify message properties with set, change, delete, and move operations. Supports JSONata expressions and various data types.
109
110
```javascript { .api }
111
/**
112
* Change node for modifying message properties
113
* Registers as node type: "change"
114
*/
115
function ChangeNode(config) {
116
RED.nodes.createNode(this, config);
117
118
// Configuration properties:
119
// config.rules - Array of change operations
120
}
121
122
// Change rule structure
123
interface ChangeRule {
124
t: string; // Rule type: "set", "change", "delete", "move"
125
p: string; // Property path to modify
126
pt: string; // Property type: "msg", "flow", "global"
127
to?: any; // Target value (for set/change)
128
tot?: string; // Target value type
129
from?: any; // Source value (for change operations)
130
fromt?: string; // Source value type
131
}
132
```
133
134
**Usage Examples:**
135
136
```javascript
137
// Set and modify properties
138
{
139
"rules": [
140
{
141
"t": "set",
142
"p": "payload.timestamp",
143
"pt": "msg",
144
"to": "",
145
"tot": "date"
146
},
147
{
148
"t": "change",
149
"p": "payload.status",
150
"pt": "msg",
151
"from": "active",
152
"fromt": "str",
153
"to": "enabled",
154
"tot": "str"
155
},
156
{
157
"t": "delete",
158
"p": "payload.temp",
159
"pt": "msg"
160
}
161
]
162
}
163
```
164
165
### Range Node
166
167
Map numeric values between input and output ranges with scaling and rounding options.
168
169
```javascript { .api }
170
/**
171
* Range node for numeric value mapping
172
* Registers as node type: "range"
173
*/
174
function RangeNode(config) {
175
RED.nodes.createNode(this, config);
176
177
// Configuration properties:
178
// config.minin - Input minimum value
179
// config.maxin - Input maximum value
180
// config.minout - Output minimum value
181
// config.maxout - Output maximum value
182
// config.action - Action for out-of-range values ("scale", "clamp", "drop")
183
// config.round - Round output values
184
}
185
```
186
187
**Usage Examples:**
188
189
```javascript
190
// Scale sensor values (0-1023) to percentage (0-100)
191
{
192
"minin": "0",
193
"maxin": "1023",
194
"minout": "0",
195
"maxout": "100",
196
"action": "scale",
197
"round": true
198
}
199
200
// Convert temperature scales
201
{
202
"minin": "0",
203
"maxin": "100",
204
"minout": "32",
205
"maxout": "212",
206
"action": "clamp"
207
}
208
```
209
210
### Template Node
211
212
Generate text using Mustache templating with access to message and context data.
213
214
```javascript { .api }
215
/**
216
* Template node for text generation using Mustache templates
217
* Registers as node type: "template"
218
*/
219
function TemplateNode(config) {
220
RED.nodes.createNode(this, config);
221
222
// Configuration properties:
223
// config.template - Mustache template string
224
// config.field - Target field for output (default: "payload")
225
// config.fieldType - Field type: "msg", "flow", "global"
226
// config.format - Template format: "handlebars", "mustache", "plain"
227
// config.syntax - Template syntax: "mustache", "plain"
228
// config.output - Output format: "str", "json"
229
}
230
```
231
232
**Usage Examples:**
233
234
```javascript
235
// Email template
236
{
237
"template": "Hello {{payload.name}},\n\nYour order #{{payload.orderId}} has been {{payload.status}}.\n\nThank you!",
238
"field": "payload",
239
"format": "mustache",
240
"output": "str"
241
}
242
243
// JSON template
244
{
245
"template": "{\n \"user\": \"{{payload.username}}\",\n \"timestamp\": \"{{timestamp}}\",\n \"value\": {{payload.value}}\n}",
246
"output": "json"
247
}
248
```
249
250
### Delay Node
251
252
Delay, throttle, or limit message rate with various timing options.
253
254
```javascript { .api }
255
/**
256
* Delay node for message timing control
257
* Registers as node type: "delay"
258
*/
259
function DelayNode(config) {
260
RED.nodes.createNode(this, config);
261
262
// Configuration properties:
263
// config.pauseType - Type: "delay", "delayv", "random", "rate", "queue"
264
// config.timeout - Delay time in specified units
265
// config.timeoutUnits - Time units: "milliseconds", "seconds", "minutes", "hours"
266
// config.rate - Rate limit (messages per time period)
267
// config.nbRateUnits - Number of time units for rate
268
// config.rateUnits - Rate time units
269
// config.randomFirst - Random delay minimum
270
// config.randomLast - Random delay maximum
271
// config.randomUnits - Random delay units
272
// config.drop - Drop messages when rate limited
273
}
274
```
275
276
**Usage Examples:**
277
278
```javascript
279
// Fixed delay
280
{
281
"pauseType": "delay",
282
"timeout": "5",
283
"timeoutUnits": "seconds"
284
}
285
286
// Rate limiting
287
{
288
"pauseType": "rate",
289
"rate": "10",
290
"nbRateUnits": "1",
291
"rateUnits": "second",
292
"drop": true
293
}
294
295
// Random delay
296
{
297
"pauseType": "random",
298
"randomFirst": "1",
299
"randomLast": "5",
300
"randomUnits": "seconds"
301
}
302
```
303
304
### Trigger Node
305
306
Send messages after delays or conditions with retriggering support.
307
308
```javascript { .api }
309
/**
310
* Trigger node for conditional message sending with delays
311
* Registers as node type: "trigger"
312
*/
313
function TriggerNode(config) {
314
RED.nodes.createNode(this, config);
315
316
// Configuration properties:
317
// config.op1 - First output payload
318
// config.op2 - Second output payload
319
// config.op1type - First payload type
320
// config.op2type - Second payload type
321
// config.duration - Delay duration
322
// config.units - Duration units
323
// config.extend - Extend delay on retrigger
324
// config.reset - Reset payload
325
// config.resetType - Reset payload type
326
}
327
```
328
329
### Exec Node
330
331
Execute system commands with configurable arguments, working directory, and timeout.
332
333
```javascript { .api }
334
/**
335
* Exec node for executing system commands
336
* Registers as node type: "exec"
337
*/
338
function ExecNode(config) {
339
RED.nodes.createNode(this, config);
340
341
// Configuration properties:
342
// config.command - Command to execute
343
// config.addpay - Append payload to command
344
// config.append - Additional arguments
345
// config.useSpawn - Use spawn instead of exec
346
// config.timer - Timeout in seconds
347
// config.winHide - Hide window on Windows
348
// config.oldrc - Use old return code behavior
349
}
350
```
351
352
### RBE Node
353
354
Report By Exception - filter messages that haven't changed based on specified properties.
355
356
```javascript { .api }
357
/**
358
* RBE (Report By Exception) node for filtering unchanged values
359
* Registers as node type: "rbe"
360
*/
361
function RBENode(config) {
362
RED.nodes.createNode(this, config);
363
364
// Configuration properties:
365
// config.func - Function type: "rbe", "deadband", "narrowband"
366
// config.gap - Deadband gap value
367
// config.start - Start value for deadband
368
// config.inout - Input/output property name
369
}
370
```
371
372
## Message Processing Patterns
373
374
Function nodes follow standard Node-RED message processing:
375
376
```javascript { .api }
377
// Standard input handler for function nodes
378
this.on("input", function(msg, send, done) {
379
try {
380
// Process based on node type
381
var result = processMessage(msg, config);
382
383
// Send result(s)
384
if (Array.isArray(result)) {
385
send(result); // Multiple outputs
386
} else {
387
send(result); // Single output
388
}
389
390
done();
391
} catch (error) {
392
done(error);
393
}
394
});
395
396
// Multi-output handling
397
function handleMultipleOutputs(msg) {
398
// Return array with one element per output
399
// null elements indicate no message for that output
400
return [msg1, null, msg3]; // Send to outputs 1 and 3 only
401
}
402
```