batch ノードは、次々に届く msg を決まった件数や時間で区切り、「列」として扱えるように印を付けます。中身を 1 つにまとめるには、後ろに join ノードを置きます。
サンプルフローは、Node-RED本体に同梱されている公式サンプル(「Number-based group mode」「Time-based group mode」「Concatenate mode」の3つ)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → sequence → batch からも、元の3つを開けます。
📋 サンプルフロー(クリックで展開)
参照元:NodeREDエディター内サンプルフロー
[
{
"id": "9bc9b0abf92b9e57",
"type": "tab",
"label": "batch",
"disabled": false,
"info": "",
"env": []
},
{
"id": "74853568.22b87c",
"type": "batch",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "count",
"count": "5",
"overlap": 0,
"interval": "5",
"allowEmptySequence": false,
"topics": [],
"x": 410,
"y": 260,
"wires": [
[
"8f4f683.99d1998"
]
]
},
{
"id": "3d208473.f31e1c",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Group 5 consecutive messages",
"info": "",
"x": 210,
"y": 88,
"wires": []
},
{
"id": "8f4f683.99d1998",
"type": "join",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": "false",
"timeout": "",
"count": "",
"reduceRight": false,
"x": 550,
"y": 260,
"wires": [
[
"6c47ccb3.bb0184"
]
]
},
{
"id": "6c47ccb3.bb0184",
"type": "debug",
"z": "9bc9b0abf92b9e57",
"name": "結果: 5個ずつ",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 710,
"y": 260,
"wires": []
},
{
"id": "49c2ac1.59a9354",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↑ create message sequence with 5 messages",
"info": "",
"x": 530,
"y": 300,
"wires": []
},
{
"id": "311dd6b4.5aeb7a",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↓ join sequence to array",
"info": "",
"x": 600,
"y": 220,
"wires": []
},
{
"id": "e27c55b0.18e9c8",
"type": "inject",
"z": "9bc9b0abf92b9e57",
"name": "実行: 5個ずつ",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 180,
"y": 180,
"wires": [
[
"9e65f29a.69ca2"
]
]
},
{
"id": "9e65f29a.69ca2",
"type": "function",
"z": "9bc9b0abf92b9e57",
"name": "send: 0-29",
"func": "for(var x = 0; x < 30; x++) {\n node.send({payload: x});\n}",
"outputs": 1,
"noerr": 0,
"x": 350,
"y": 180,
"wires": [
[
"74853568.22b87c"
]
]
},
{
"id": "817acbfb.452af8",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↓ send sequence: 0-29",
"info": "",
"x": 380,
"y": 140,
"wires": []
},
{
"id": "53645699.a35c48",
"type": "batch",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "count",
"count": "5",
"overlap": "1",
"interval": "5",
"allowEmptySequence": false,
"topics": [],
"x": 410,
"y": 528,
"wires": [
[
"4cb873e6.f9996c"
]
]
},
{
"id": "ecff527d.d64cb",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Group 5 consecutive messages with overlap of 1 msg",
"info": "",
"x": 280,
"y": 356,
"wires": []
},
{
"id": "4cb873e6.f9996c",
"type": "join",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": "false",
"timeout": "",
"count": "",
"reduceRight": false,
"x": 550,
"y": 528,
"wires": [
[
"51089be3.4ecbf4"
]
]
},
{
"id": "51089be3.4ecbf4",
"type": "debug",
"z": "9bc9b0abf92b9e57",
"name": "結果: 5個ずつ・1個重ねる",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 710,
"y": 528,
"wires": []
},
{
"id": "84c34533.6284a8",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↑ create message sequence with 5 messages with overlap of 1 msg",
"info": "",
"x": 600,
"y": 568,
"wires": []
},
{
"id": "c7241026.18245",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↓ join sequence to array",
"info": "",
"x": 600,
"y": 488,
"wires": []
},
{
"id": "67d24449.028eec",
"type": "inject",
"z": "9bc9b0abf92b9e57",
"name": "実行: 5個ずつ・1個重ねる",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 180,
"y": 448,
"wires": [
[
"5d909bfb.6faf44"
]
]
},
{
"id": "5d909bfb.6faf44",
"type": "function",
"z": "9bc9b0abf92b9e57",
"name": "send: 0-29",
"func": "for(var x = 0; x < 30; x++) {\n node.send({payload: x});\n}",
"outputs": 1,
"noerr": 0,
"x": 350,
"y": 448,
"wires": [
[
"53645699.a35c48"
]
]
},
{
"id": "e7af9fbb.f30ad",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↓ send sequence: 0-29",
"info": "",
"x": 380,
"y": 408,
"wires": []
},
{
"id": "f4a38f7b.8d62f",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Example: Number-based Group Mode",
"info": "*Number-based Group mode* of batch node can be used to create new message sequences from incoming messages. Recently received *N*-messages are grouped to a sequence. Creating message sequences that has overwrap with adjacent message group is possible.\n",
"x": 190,
"y": 40,
"wires": []
},
{
"id": "e3a5c066.16333",
"type": "batch",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "interval",
"count": 10,
"overlap": 0,
"interval": "5",
"allowEmptySequence": false,
"topics": [],
"x": 390,
"y": 860,
"wires": [
[
"9223c119.c5268"
]
]
},
{
"id": "606bb329.983c9c",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Group messages received within 5s",
"info": "",
"x": 220,
"y": 680,
"wires": []
},
{
"id": "9223c119.c5268",
"type": "join",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": "false",
"timeout": "",
"count": "",
"reduceRight": false,
"x": 530,
"y": 860,
"wires": [
[
"e4d07fa1.78c16"
]
]
},
{
"id": "e4d07fa1.78c16",
"type": "debug",
"z": "9bc9b0abf92b9e57",
"name": "結果: 5秒ごと",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 690,
"y": 860,
"wires": []
},
{
"id": "848f59e5.7528d8",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↑ create message sequence received within 5s",
"info": "",
"x": 520,
"y": 900,
"wires": []
},
{
"id": "cf1bbb5e.ba3e68",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↓ join sequence to array",
"info": "",
"x": 580,
"y": 820,
"wires": []
},
{
"id": "a1e311d5.4dca1",
"type": "inject",
"z": "9bc9b0abf92b9e57",
"name": "実行: 5秒ごと",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 180,
"y": 760,
"wires": [
[
"97da73f2.ae01e"
]
]
},
{
"id": "97da73f2.ae01e",
"type": "function",
"z": "9bc9b0abf92b9e57",
"name": "send: 0-29",
"func": "for(var x = 0; x < 30; x++) {\n node.send({payload: x});\n}",
"outputs": 1,
"noerr": 0,
"x": 350,
"y": 760,
"wires": [
[
"2776c823.77eba8"
]
]
},
{
"id": "2776c823.77eba8",
"type": "delay",
"z": "9bc9b0abf92b9e57",
"name": "",
"pauseType": "rate",
"timeout": "1",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"x": 530,
"y": 760,
"wires": [
[
"e3a5c066.16333"
]
]
},
{
"id": "93520648.6a7988",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↓ send sequence: 0-29",
"info": "",
"x": 380,
"y": 720,
"wires": []
},
{
"id": "5842d64f.9fc608",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Example: Time-based Group Mode",
"info": "*Time-based Group mode* of batch node can be used to create new message sequences from incoming messages received within specified time range. \n",
"x": 180,
"y": 620,
"wires": []
},
{
"id": "cf1fd796.197678",
"type": "inject",
"z": "9bc9b0abf92b9e57",
"name": "実行: 2回つなげる",
"topic": "SEQ",
"payload": "[1,2,3,4,5]",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 200,
"y": 1040,
"wires": [
[
"d1c8ddf0.99b4e"
]
]
},
{
"id": "d1c8ddf0.99b4e",
"type": "split",
"z": "9bc9b0abf92b9e57",
"name": "",
"splt": "\\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "",
"x": 370,
"y": 1040,
"wires": [
[
"f727a5d3.ea1a28"
]
]
},
{
"id": "f727a5d3.ea1a28",
"type": "batch",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "concat",
"count": 10,
"overlap": 0,
"interval": 10,
"allowEmptySequence": false,
"topics": [
{
"topic": "SEQ"
},
{
"topic": "SEQ"
}
],
"x": 510,
"y": 1040,
"wires": [
[
"f4d6dba4.7e8ab8"
]
]
},
{
"id": "f4d6dba4.7e8ab8",
"type": "join",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": "false",
"timeout": "",
"count": "",
"reduceRight": false,
"x": 650,
"y": 1040,
"wires": [
[
"31b81865.611788"
]
]
},
{
"id": "31b81865.611788",
"type": "debug",
"z": "9bc9b0abf92b9e57",
"name": "結果: 2回つなげる",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 810,
"y": 1040,
"wires": []
},
{
"id": "8ad6b9c.7ee8548",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Duplicate",
"info": "",
"x": 140,
"y": 1000,
"wires": []
},
{
"id": "5137b2d0.f4838c",
"type": "inject",
"z": "9bc9b0abf92b9e57",
"name": "実行: 負→正の順につなげる",
"topic": "SEQ",
"payload": "[1,-6,-8,7,2,-3]",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 200,
"y": 1160,
"wires": [
[
"408f3032.eafc1"
]
]
},
{
"id": "408f3032.eafc1",
"type": "split",
"z": "9bc9b0abf92b9e57",
"name": "",
"splt": "\\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "",
"x": 370,
"y": 1160,
"wires": [
[
"dbd6e8b8.cbf2b8"
]
]
},
{
"id": "c70a7fad.9ee08",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Filter & Concat",
"info": "",
"x": 160,
"y": 1120,
"wires": []
},
{
"id": "dbd6e8b8.cbf2b8",
"type": "switch",
"z": "9bc9b0abf92b9e57",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "gt",
"v": "0",
"vt": "num"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": true,
"outputs": 2,
"x": 430,
"y": 1220,
"wires": [
[
"c11e5c5f.876d6"
],
[
"e99c703b.f404"
]
]
},
{
"id": "356c8654.2ad1aa",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↑ Duplicate SEQ",
"info": "",
"x": 540,
"y": 1080,
"wires": []
},
{
"id": "c11e5c5f.876d6",
"type": "change",
"z": "9bc9b0abf92b9e57",
"name": "Topic←POS",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "POS",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 590,
"y": 1180,
"wires": [
[
"3412e439.eda55c"
]
]
},
{
"id": "e99c703b.f404",
"type": "change",
"z": "9bc9b0abf92b9e57",
"name": "Topic←NEG",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "NEG",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 590,
"y": 1240,
"wires": [
[
"3412e439.eda55c"
]
]
},
{
"id": "3412e439.eda55c",
"type": "batch",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "concat",
"count": 10,
"overlap": 0,
"interval": 10,
"allowEmptySequence": false,
"topics": [
{
"topic": "NEG"
},
{
"topic": "POS"
}
],
"x": 750,
"y": 1220,
"wires": [
[
"7f1ce95c.7ddbc8"
]
]
},
{
"id": "e6f01877.16d558",
"type": "debug",
"z": "9bc9b0abf92b9e57",
"name": "結果: 負→正の順につなげる",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 970,
"y": 1160,
"wires": []
},
{
"id": "7f1ce95c.7ddbc8",
"type": "join",
"z": "9bc9b0abf92b9e57",
"name": "",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": "false",
"timeout": "",
"count": "",
"reduceRight": false,
"x": 810,
"y": 1160,
"wires": [
[
"e6f01877.16d558"
]
]
},
{
"id": "c548f2c.641141",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "↑ Order sequence: negative→positive",
"info": "",
"x": 850,
"y": 1260,
"wires": []
},
{
"id": "c571b56c.ae63b8",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Filter pos/neg and make separate sequence↑ (but not a simple sort) ",
"info": "",
"x": 360,
"y": 1280,
"wires": []
},
{
"id": "c851c021.a9688",
"type": "comment",
"z": "9bc9b0abf92b9e57",
"name": "Example: Concatenate Mode",
"info": "*Concatenate mode* of batch node can be used to combine input message sequences to create a new message sequence. Order of the sequences can be specified using message topic assigned to each message in a sequence. Message sequence can be specified multiple times.\n",
"x": 160,
"y": 940,
"wires": []
}
]
フローには、「Example:」で始まる comment ノードが3つあり、その下に、batch ノードのモードごとの例が並んでいます。例は全部で5つです。どの例も、「実行: …」という名前の inject のボタンで動かし、同じ名前の「結果: …」の debug に結果が出ます。
モード: メッセージ数でグループ化
メッセージ数: 5
オーバラップ: 0(重複なし)
入力: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... , 29 (30メッセージ)
出力: [0,1,2,3,4], [5,6,7,8,9], ... , [25,26,27,28,29] (6グループ)
モード: メッセージ数でグループ化
メッセージ数: 5
オーバラップ: 1
入力: 0, 1, 2, ..., 29 (30メッセージ)
出力:
グループ1: [0,1,2,3,4]
グループ2: [4,5,6,7,8] ← 4が共有
グループ3: [8,9,10,11,12] ← 8が共有
...
グループ7: [24,25,26,27,28]
(29 は、次の5個がそろわないので出力されない。重ねる分の 28 と一緒に batch の中に残る)
モード: 時間間隔でグループ化
時間間隔: 5(秒)
メッセージを受信しない場合、空のメッセージを送信: オフ
入力: 1秒間隔で 0, 1, 2, 3, ... , 29
出力: 5秒ごとに、その間に届いた分
例: [0,1,2] → [3,4,5,6,7] → ... → [28,29]
(最初と最後の個数は、ボタンを押すタイミングで変わる)
モード: 列の結合
トピック: SEQ, SEQ(同じトピックを2回)
入力: トピック SEQ の列 1, 2, 3, 4, 5
出力: [1,2,3,4,5,1,2,3,4,5]
モード: 列の結合
トピック: NEG, POS
入力: 1, -6, -8, 7, 2, -3(正の数に POS、それ以外に NEG のトピックを付ける)
出力: [-6,-8,-3,1,7,2]
温度センサーから1秒ごとにデータ受信
↓
Batch(count=5, overlap=4)
↓
5点移動平均ウィンドウ
↓
Join → Change(平均計算: JSONata の $average)
↓
ノイズを除去した温度値
複数センサーからのデータ(異なるtopic)
↓
Batch(concat mode: temp, humidity, pressure)
↓
センサー種別ごとに順序付けて結合
↓
統合レポート生成