📋 サンプルフロー(クリックで展開)
参照元:NodeREDエディター内サンプルフロー
[
{
"id": "98aa28d4a248e8cb",
"type": "tab",
"label": "function",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ec5a531b.68b65",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "World",
"payloadType": "str",
"x": 290,
"y": 140,
"wires": [
[
"961abba6.04a028"
]
]
},
{
"id": "1b0f8c3e.1fd7e4",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 670,
"y": 140,
"wires": []
},
{
"id": "4e5bf6b2.b4dd58",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "send a message to output port",
"info": "Function node can be used to write JavaScript code to handle messages.\nThe input message can be referrenced by `msg` variable. \nA message returned from body of the function is sent to output port.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 310,
"y": 80,
"wires": []
},
{
"id": "961abba6.04a028",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "return a message",
"func": "// returning message send it to output port\nmsg.payload = \"Hello, \"+msg.payload +\"!\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 140,
"wires": [
[
"1b0f8c3e.1fd7e4"
]
]
},
{
"id": "c2b3b0f1.62189",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 280,
"wires": [
[
"7241db1e.8946c4"
]
]
},
{
"id": "c6191361.0f3c",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 730,
"y": 260,
"wires": []
},
{
"id": "3aa5727c.e5019e",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "send multiple message",
"info": "Function node can send multiple messages to output ports by returning an array of messages. \n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 280,
"y": 220,
"wires": []
},
{
"id": "7241db1e.8946c4",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "return array of messages",
"func": "// returning array of message send elements to output ports\nvar msg1 = { payload:\"first out of output 1\" };\nvar msg2 = { payload:\"second out of output 1\" };\nvar msg3 = { payload:\"third out of output 1\" };\nvar msg4 = { payload:\"only message from output 2\" };\nreturn [ [ msg1, msg2, msg3 ], msg4 ];",
"outputs": 2,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 510,
"y": 280,
"wires": [
[
"c6191361.0f3c"
],
[
"23a53d00.c89b74"
]
]
},
{
"id": "23a53d00.c89b74",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 730,
"y": 300,
"wires": []
},
{
"id": "cc74476e.cbdf68",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "Hello, World!",
"payloadType": "str",
"x": 310,
"y": 420,
"wires": [
[
"7d90286.706e9d8"
]
]
},
{
"id": "909906c3.ea9f58",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 770,
"y": 420,
"wires": []
},
{
"id": "d5fc0512.4cd9c8",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "sending messages asynchronously",
"info": "Function node can asynchronously send a messages to output ports by calling `node.send` function.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 320,
"y": 360,
"wires": []
},
{
"id": "7d90286.706e9d8",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "wait 2s then send message",
"func": "// setTimeout calls calls specified callback function asynchronously after a specified time\nsetTimeout(function () {\n node.send(msg);\n}, 2*1000);\n",
"outputs": 1,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 540,
"y": 420,
"wires": [
[
"909906c3.ea9f58"
]
]
},
{
"id": "89c17d21.15da2",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 660,
"wires": [
[
"1bcca7af.619428"
]
]
},
{
"id": "3b9cd70e.8e66e8",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 650,
"y": 660,
"wires": []
},
{
"id": "f76ea68a.6f3c58",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "handling errors",
"info": "Calling `node.error` function with the original message as a second argument, function node can trigger a catch node in the same tab.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 260,
"y": 600,
"wires": []
},
{
"id": "1bcca7af.619428",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "report error",
"func": "// In function node, calling node.error functions with the original input message as its second argument triggers catch node\n// See debug sidebar and console output\nnode.error(\"Oh no, something bad happened\", msg);\n// execution should stops here",
"outputs": 1,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 660,
"wires": [
[
"3b9cd70e.8e66e8"
]
]
},
{
"id": "74854950.d99558",
"type": "catch",
"z": "98aa28d4a248e8cb",
"name": "",
"scope": [
"1bcca7af.619428"
],
"uncaught": false,
"x": 450,
"y": 720,
"wires": [
[
"32743f74.e718a"
]
]
},
{
"id": "32743f74.e718a",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 630,
"y": 720,
"wires": []
},
{
"id": "fb884166.e42f3",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "↑ error information can be found in msg.error",
"info": "",
"x": 750,
"y": 760,
"wires": []
},
{
"id": "64470964.a291a8",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 540,
"wires": [
[
"ee4f5964.5cdae8"
]
]
},
{
"id": "ed028c2f.924b",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 650,
"y": 540,
"wires": []
},
{
"id": "b1686e70.a9f6d",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "logging events",
"info": "In body of function node code, following logging functions can be used to log events:\n- `node.log`\n- `node.warn`\n- `node.error`\n- `node.trace`\n- `node.debug`\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 260,
"y": 480,
"wires": []
},
{
"id": "ee4f5964.5cdae8",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "log events",
"func": "// In function node, node.log, node.warn, and node.error functions can be used for logging\n// See debug sidebar and console output\nnode.log(\"Something happened\");\nnode.warn(\"Something happened you should know about\");\nnode.error(\"Oh no, something bad happened\");\nreturn msg;",
"outputs": 1,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 540,
"wires": [
[
"ed028c2f.924b"
]
]
},
{
"id": "a3a635eb.191188",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 860,
"wires": [
[
"77f57d92.06fa14"
]
]
},
{
"id": "1a218bea.61ae04",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 630,
"y": 860,
"wires": []
},
{
"id": "a56bde3a.330e1",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "storing data in context",
"info": "The function node code can store data in the context store.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions) and [context store](https://nodered.org/docs/user-guide/context).",
"x": 280,
"y": 800,
"wires": []
},
{
"id": "77f57d92.06fa14",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "counter",
"func": "// initialise the counter to 0 if it doesn't exist already\nvar count = context.get('count')||0;\ncount += 1;\n// store the value back\ncontext.set('count',count);\n// make it part of the outgoing msg object\nmsg.payload = count;\nreturn msg;",
"outputs": 1,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 460,
"y": 860,
"wires": [
[
"1a218bea.61ae04"
]
]
},
{
"id": "ce0b7cca.34817",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "red",
"payloadType": "str",
"x": 290,
"y": 1000,
"wires": [
[
"a4a6c205.8afd4"
]
]
},
{
"id": "c98c52d0.ab51f",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "showing status information",
"info": "The function node code can provide status decoration by calling `node.status` function.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 290,
"y": 940,
"wires": []
},
{
"id": "a4a6c205.8afd4",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "show status",
"func": "// calling node.status show status information below the function node\nswitch (msg.payload) {\n case \"red\":\n node.status({fill:\"red\",shape:\"ring\",text:\"disconnected\"});\n break;\n case \"green\":\n node.status({fill:\"green\",shape:\"dot\",text:\"connected\"});\n break;\n case \"text\":\n node.status({text:\"Just text status\"});\n break;\n case \"clear\":\n node.status({}); // to clear the status \n break;\n}\n\n",
"outputs": 0,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 1000,
"wires": []
},
{
"id": "9f29ae74.8dd11",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "green",
"payloadType": "str",
"x": 290,
"y": 1040,
"wires": [
[
"a4a6c205.8afd4"
]
]
},
{
"id": "83fc1404.ec0b98",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "text",
"payloadType": "str",
"x": 290,
"y": 1080,
"wires": [
[
"a4a6c205.8afd4"
]
]
},
{
"id": "517a869c.0ceab8",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "clear",
"payloadType": "str",
"x": 290,
"y": 1120,
"wires": [
[
"a4a6c205.8afd4"
]
]
},
{
"id": "3ece1efd.306c22",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 380,
"y": 1240,
"wires": [
[
"c9100743.673598"
]
]
},
{
"id": "ed3da95d.62ee98",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 750,
"y": 1240,
"wires": []
},
{
"id": "85b5b8ad.0717d8",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "using external modules (needs change in settings.js)",
"info": "The function node can use external node modules using global context value.\n\nAdditional node modules must be loaded in `settings.js` file and added to the `functionGlobalContext` property. \n\nFor example, following definition in `settings.js` load `os` module on start up of Node-RED. \n\n```\nfunctionGlobalContext: {\n os: require(\"os\")\n}\n```\n\nThe `os` module can be accessed in a function blocks as:\n```\nglobal.get(\"os\")\n```\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions) and [context store](https://nodered.org/docs/user-guide/context).",
"x": 520,
"y": 1180,
"wires": []
},
{
"id": "c9100743.673598",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "use os module",
"func": "// use external os module to access OS information\nvar os = global.get(\"os\");\nmsg.payload = { \n hostname: os.hostname(),\n arch: os.arch(),\n platform: os.platform(),\n release: os.release(),\n free: os.freemem()\n};\nreturn msg;",
"outputs": 1,
"timeout": "",
"initialize": "",
"finalize": "",
"libs": [],
"x": 560,
"y": 1240,
"wires": [
[
"ed3da95d.62ee98"
]
]
},
{
"id": "e397736b.a96af",
"type": "inject",
"z": "98aa28d4a248e8cb",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 1380,
"wires": [
[
"900ad01c.561f"
]
]
},
{
"id": "1e2a78d6.33f617",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 630,
"y": 1380,
"wires": []
},
{
"id": "83d3f2fd.593b4",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "setup and close",
"info": "The function node code provide setup and closecode. \nThe `Setup` tab contains code that will be run whenever the node is started. The `Close` tab contains code that will be run when the node is stopped.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 260,
"y": 1320,
"wires": []
},
{
"id": "900ad01c.561f",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "counter",
"func": "// get value of global counter\nvar count = global.get('count');\ncount += 1;\n// store the value back\nglobal.set('count',count);\n// make it part of the outgoing msg object\nmsg.payload = count;\nreturn msg;\n",
"outputs": 1,
"timeout": "",
"initialize": "// initialize global counter\nglobal.set('count', 0);",
"finalize": "// report current counter to console\nvar count = global.get('count');\nconsole.log(\"count:\", count);",
"libs": [],
"x": 460,
"y": 1380,
"wires": [
[
"1e2a78d6.33f617"
]
]
},
{
"id": "89904b80.4fcd08",
"type": "inject",
"z": "98aa28d4a248e8cb",
"d": true,
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 310,
"y": 1520,
"wires": [
[
"22c235a0.acb41a"
]
]
},
{
"id": "20fa70dc.7107f",
"type": "debug",
"z": "98aa28d4a248e8cb",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 670,
"y": 1520,
"wires": []
},
{
"id": "cddf88c9.9df018",
"type": "comment",
"z": "98aa28d4a248e8cb",
"name": "asynchronous setup",
"info": "The `Setup` code that performs asynchronous work can return a promise object of the work. The completion of the promise is waited before starting function body.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).",
"x": 270,
"y": 1460,
"wires": []
},
{
"id": "22c235a0.acb41a",
"type": "function",
"z": "98aa28d4a248e8cb",
"name": "async. setup",
"func": "// retrieve message value\nmsg.payload = global.get('message');\nreturn msg;",
"outputs": 1,
"timeout": "",
"initialize": "// set initial value of message\nglobal.set(\"message\", \"Initializing, World!\");\n// create promise for async work\nvar promise = new Promise((resolve, reject) => {\n // async work: wait 1s, then set message\n setTimeout(() => {\n global.set(\"message\", \"Hello, World!\");\n // report this work successfuly ended\n resolve();\n }, 1000);\n});\n// return the promise that should be executed before function code\nreturn promise;",
"finalize": "",
"libs": [],
"x": 490,
"y": 1520,
"wires": [
[
"20fa70dc.7107f"
]
]
}
]
// 数値が50以上なら出力1、未満なら出力2に振り分け
if (msg.payload >= 50) {
return [msg, null];
} else {
return [null, msg];
}
// 3つのメッセージを順次送信
var msg1 = { payload: "first" };
var msg2 = { payload: "second" };
var msg3 = { payload: "third" };
return [[msg1, msg2, msg3]];
// 2秒後にメッセージを送信
setTimeout(function() {
node.send(msg);
}, 2000);
// 注意: 非同期処理では return しない
// カウンター: 0から始めて1ずつ増加
var count = context.get('count') || 0;
count += 1;
context.set('count', count);
msg.payload = count;
return msg;
// 接続状態をステータスで表示
if (msg.payload === "connected") {
node.status({
fill: "green",
shape: "dot",
text: "接続中"
});
} else {
node.status({
fill: "red",
shape: "ring",
text: "切断"
});
}
// 入力値の検証とエラー処理
if (typeof msg.payload !== "number") {
node.error("数値ではありません", msg);
return null;
}
msg.payload = msg.payload * 2;
return msg;
// 温度センサーの生データを摂氏に変換
var rawValue = msg.payload;
var celsius = (rawValue - 32) * 5 / 9;
msg.payload = {
raw: rawValue,
celsius: celsius.toFixed(2),
timestamp: new Date().toISOString()
};
return msg;
// 複数のセンサー値を集約
var data = flow.get('sensorData') || [];
data.push(msg.payload);
// 最新10件のみ保持
if (data.length > 10) {
data.shift();
}
flow.set('sensorData', data);
// 平均値を計算
var sum = data.reduce(function(a, b) { return a + b; }, 0);
msg.payload = {
average: sum / data.length,
count: data.length
};
return msg;
// APIレスポンスから必要なデータを抽出
try {
var response = msg.payload;
msg.payload = {
id: response.data.id,
name: response.data.name,
status: response.data.active ? "有効" : "無効"
};
node.status({fill:"green", shape:"dot", text:"成功"});
return msg;
} catch (e) {
node.error("パースエラー: " + e.message, msg);
node.status({fill:"red", shape:"ring", text:"エラー"});
return null;
}
// 重複メッセージを除外
var lastValue = context.get('lastValue');
if (msg.payload === lastValue) {
// 同じ値なら送信しない
return null;
}
// 新しい値を保存
context.set('lastValue', msg.payload);
return msg;