Switchノードは、条件に応じてメッセージの送り先を振り分けるノードです。
郵便局の仕分けセンターに例えると分かりやすいでしょう。
Switchノードは入力メッセージを評価し、条件に一致する出力ポートへメッセージを送信します。
複数の条件を設定でき、プログラミングのif-else文やswitch-case文に相当します。
サンプルフローは、Node-RED本体に同梱されている公式サンプル9つ(「Select output port」から「Route message based on context value」まで)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → function → switch からも、元の9つを開けます。
サンプルフロー(クリックで展開)
参照元:NodeREDエディター内サンプルフロー
[
{
"id": "e110d6b7dc11f739",
"type": "tab",
"label": "switch",
"disabled": false,
"info": "",
"env": []
},
{
"id": "87bd706a.aec93",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Select output port based on input message value",
"info": "Switch node can route input message according to predefined rules.\n",
"x": 260,
"y": 60,
"wires": []
},
{
"id": "ec0f4187.0bf9e",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "0",
"vt": "num"
},
{
"t": "eq",
"v": "0",
"vt": "num"
},
{
"t": "gt",
"v": "0",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 330,
"y": 140,
"wires": [
[
"e3d35f5a.ff33"
],
[
"91114621.cdd9b8"
],
[
"bcbd960b.2f4f48"
]
]
},
{
"id": "903f0813.e99318",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例1: Negative の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 650,
"y": 100,
"wires": []
},
{
"id": "bcbd960b.2f4f48",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Positive",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "positive",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 480,
"y": 180,
"wires": [
[
"efb63613.b0aa08"
]
]
},
{
"id": "91114621.cdd9b8",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Zero",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "zero",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 470,
"y": 140,
"wires": [
[
"6d4214c1.524a9c"
]
]
},
{
"id": "e3d35f5a.ff33",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Negative",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "negative",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 480,
"y": 100,
"wires": [
[
"903f0813.e99318"
]
]
},
{
"id": "6d4214c1.524a9c",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例1: Zero の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 650,
"y": 140,
"wires": []
},
{
"id": "efb63613.b0aa08",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例1: Positive の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 650,
"y": 180,
"wires": []
},
{
"id": "398299e.aab0b66",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例1: -10",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-10",
"payloadType": "num",
"x": 190,
"y": 100,
"wires": [
[
"ec0f4187.0bf9e"
]
]
},
{
"id": "dba0f204.13c4b",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例1: 0",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 190,
"y": 140,
"wires": [
[
"ec0f4187.0bf9e"
]
]
},
{
"id": "bcc219f0.48d8e8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例1: 10",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "10",
"payloadType": "num",
"x": 190,
"y": 180,
"wires": [
[
"ec0f4187.0bf9e"
]
]
},
{
"id": "6ec19fc7.a32ae",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Check all rules",
"info": "Switch node apply all rules if `checking all rules` is selected in settings panel.",
"x": 120,
"y": 260,
"wires": []
},
{
"id": "1644e138.f8d1ef",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "10",
"vt": "num"
},
{
"t": "gt",
"v": "-10",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 290,
"y": 340,
"wires": [
[
"624b4e9f.37fee"
],
[
"a89d6432.b68318"
]
]
},
{
"id": "a7f64dbf.3e27b",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例2: <10 の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 590,
"y": 300,
"wires": []
},
{
"id": "a89d6432.b68318",
"type": "change",
"z": "e110d6b7dc11f739",
"name": ">-10",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": ">-10",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 430,
"y": 380,
"wires": [
[
"f1136da2.23516"
]
]
},
{
"id": "624b4e9f.37fee",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "<10",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "<10",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 430,
"y": 300,
"wires": [
[
"a7f64dbf.3e27b"
]
]
},
{
"id": "f1136da2.23516",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例2: >-10 の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 590,
"y": 380,
"wires": []
},
{
"id": "c7e952e9.88e5e",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例2: -10",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-10",
"payloadType": "num",
"x": 150,
"y": 300,
"wires": [
[
"1644e138.f8d1ef"
]
]
},
{
"id": "8cf8babd.b43db8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例2: 10",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "10",
"payloadType": "num",
"x": 150,
"y": 380,
"wires": [
[
"1644e138.f8d1ef"
]
]
},
{
"id": "6a43ae86.b92ed",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例2: 0",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 150,
"y": 340,
"wires": [
[
"1644e138.f8d1ef"
]
]
},
{
"id": "a12a5708.195688",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Stop after first match",
"info": "Switch node stops application of rules if `stopping after first match` is selected in settings panel and a rule evaluates to `true`.",
"x": 160,
"y": 460,
"wires": []
},
{
"id": "8aebdebf.5d7f2",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "10",
"vt": "num"
},
{
"t": "gt",
"v": "-10",
"vt": "num"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 310,
"y": 540,
"wires": [
[
"5d1851c.a9c5db"
],
[
"e8228605.f32018"
]
]
},
{
"id": "60248c98.69fd44",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例3: <10 の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 610,
"y": 500,
"wires": []
},
{
"id": "e8228605.f32018",
"type": "change",
"z": "e110d6b7dc11f739",
"name": ">-10",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": ">-10",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 580,
"wires": [
[
"e9a51608.c322b8"
]
]
},
{
"id": "5d1851c.a9c5db",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "<10",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "<10",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 500,
"wires": [
[
"60248c98.69fd44"
]
]
},
{
"id": "e9a51608.c322b8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例3: >-10 の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 610,
"y": 580,
"wires": []
},
{
"id": "49222b4b.647a84",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例3: -10",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-10",
"payloadType": "num",
"x": 170,
"y": 500,
"wires": [
[
"8aebdebf.5d7f2"
]
]
},
{
"id": "39e8c133.a56f7e",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例3: 10",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "10",
"payloadType": "num",
"x": 170,
"y": 580,
"wires": [
[
"8aebdebf.5d7f2"
]
]
},
{
"id": "3a22ec96.965a14",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例3: 0",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 170,
"y": 540,
"wires": [
[
"8aebdebf.5d7f2"
]
]
},
{
"id": "6dfd4381.eae2ec",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Select output based on type of payload value",
"info": "Switch node can route input message based on type of payload value.",
"x": 210,
"y": 640,
"wires": []
},
{
"id": "b139dacf.a0d818",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "istype",
"v": "string",
"vt": "string"
},
{
"t": "istype",
"v": "number",
"vt": "number"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 330,
"y": 720,
"wires": [
[
"7c5cd60c.e66b28"
],
[
"86cf5262.20f18"
]
]
},
{
"id": "ca403f12.477a8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例4: Number の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 630,
"y": 760,
"wires": []
},
{
"id": "7c5cd60c.e66b28",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "String",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "string",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 470,
"y": 680,
"wires": [
[
"d9669648.1177e8"
]
]
},
{
"id": "86cf5262.20f18",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Number",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "number",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 480,
"y": 760,
"wires": [
[
"ca403f12.477a8"
]
]
},
{
"id": "d9669648.1177e8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例4: String の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 630,
"y": 680,
"wires": []
},
{
"id": "d3f1c718.dc67e8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "Number:128",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "128",
"payloadType": "num",
"x": 170,
"y": 680,
"wires": [
[
"b139dacf.a0d818"
]
]
},
{
"id": "a59e275e.6d48c8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "String:128",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "128",
"payloadType": "str",
"x": 160,
"y": 760,
"wires": [
[
"b139dacf.a0d818"
]
]
},
{
"id": "175ceb0d.8dbe45",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Use JSONata expression for rules",
"info": "Switch node can use JSONata expression for calculating complex conditions.",
"x": 200,
"y": 860,
"wires": []
},
{
"id": "d89491c3.793a3",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "jsonata_exp",
"v": "(payload % 2) = 0",
"vt": "jsonata"
},
{
"t": "jsonata_exp",
"v": "(payload % 2) = 1",
"vt": "jsonata"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 310,
"y": 940,
"wires": [
[
"d6cb78a6.872908"
],
[
"1f0c62bb.c3d52d"
]
]
},
{
"id": "9ae9a2aa.a895c",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例5: Even の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 610,
"y": 900,
"wires": []
},
{
"id": "1f0c62bb.c3d52d",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Odd",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "odd",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 980,
"wires": [
[
"34b0a9fb.bafdb6"
]
]
},
{
"id": "d6cb78a6.872908",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Even",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "even",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 450,
"y": 900,
"wires": [
[
"9ae9a2aa.a895c"
]
]
},
{
"id": "34b0a9fb.bafdb6",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例5: Odd の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 610,
"y": 980,
"wires": []
},
{
"id": "7beb0333.a55bac",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例5: 7",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "7",
"payloadType": "num",
"x": 170,
"y": 900,
"wires": [
[
"d89491c3.793a3"
]
]
},
{
"id": "a8db47cf.58ba18",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例5: 8",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "8",
"payloadType": "num",
"x": 170,
"y": 980,
"wires": [
[
"d89491c3.793a3"
]
]
},
{
"id": "1c09dc33.934504",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Use JSONata expression for switch value",
"info": "Switch node can use JSONata expression for calculating complex switch value.",
"x": 200,
"y": 1060,
"wires": []
},
{
"id": "c7ca4974.d638f8",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "(payload % 2) = 0",
"propertyType": "jsonata",
"rules": [
{
"t": "true"
},
{
"t": "false"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 290,
"y": 1140,
"wires": [
[
"ac921b1d.c0dbe8"
],
[
"89adcfcc.53d6d"
]
]
},
{
"id": "ab8972e0.e98b7",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例6: Even の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 590,
"y": 1100,
"wires": []
},
{
"id": "89adcfcc.53d6d",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Odd",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "odd",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 430,
"y": 1180,
"wires": [
[
"426c68cf.64f0e8"
]
]
},
{
"id": "ac921b1d.c0dbe8",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Even",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "even",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 430,
"y": 1100,
"wires": [
[
"ab8972e0.e98b7"
]
]
},
{
"id": "426c68cf.64f0e8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例6: Odd の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 590,
"y": 1180,
"wires": []
},
{
"id": "63377f1c.2fbfc",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例6: 7",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "7",
"payloadType": "num",
"x": 150,
"y": 1100,
"wires": [
[
"c7ca4974.d638f8"
]
]
},
{
"id": "ea2fa596.ff1638",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例6: 8",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "8",
"payloadType": "num",
"x": 150,
"y": 1180,
"wires": [
[
"c7ca4974.d638f8"
]
]
},
{
"id": "61ee9b5c.b9e474",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Recreate message sequence",
"info": "Switch node can recreate message sequence from input message sequence for each output port.",
"x": 160,
"y": 1260,
"wires": []
},
{
"id": "e3a71ee7.ec465",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "gt",
"v": "0",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": true,
"outputs": 2,
"x": 490,
"y": 1340,
"wires": [
[
"f5f59cc2.a358f"
],
[
"ea61fc1e.afc62"
]
]
},
{
"id": "5fd951bb.c150e",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例7: Positive の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 810,
"y": 1300,
"wires": []
},
{
"id": "cc02b428.9abb38",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "例7: Negative の結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 810,
"y": 1380,
"wires": []
},
{
"id": "5ad82808.648ef8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例7: 配列",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "[3, -2, -1, 1, -3, 2]",
"payloadType": "json",
"x": 180,
"y": 1340,
"wires": [
[
"5f435601.bb9ef8"
]
]
},
{
"id": "5f435601.bb9ef8",
"type": "split",
"z": "e110d6b7dc11f739",
"name": "",
"splt": "\\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "",
"x": 350,
"y": 1340,
"wires": [
[
"e3a71ee7.ec465"
]
]
},
{
"id": "f5f59cc2.a358f",
"type": "join",
"z": "e110d6b7dc11f739",
"name": "Positive",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 640,
"y": 1300,
"wires": [
[
"5fd951bb.c150e"
]
]
},
{
"id": "ea61fc1e.afc62",
"type": "join",
"z": "e110d6b7dc11f739",
"name": "Negative",
"mode": "auto",
"build": "string",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 640,
"y": 1380,
"wires": [
[
"cc02b428.9abb38"
]
]
},
{
"id": "7ca55649.268c78",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Route a message based on one of its properties",
"info": "Switch node can route a message to different flows according to the value of any property (e.g. `msg.topic`).\n\nSee Node-RED cookbook [item](https://cookbook.nodered.org/basic/route-on-property).",
"x": 240,
"y": 1460,
"wires": []
},
{
"id": "f2cbd8ce.34c4c8",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "Route ",
"property": "topic",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "temperature",
"vt": "str"
},
{
"t": "eq",
"v": "humidity",
"vt": "str"
},
{
"t": "eq",
"v": "pressure",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 370,
"y": 1560,
"wires": [
[
"50651c06.281524"
],
[
"2786389.31cc5c8"
],
[
"e4db256d.a80ca8"
]
]
},
{
"id": "c4de36a5.f14bc8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例8: temperature",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "temperature",
"payload": "27",
"payloadType": "num",
"x": 180,
"y": 1520,
"wires": [
[
"f2cbd8ce.34c4c8"
]
]
},
{
"id": "96073ecd.9207f",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例8: humidity",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "humidity",
"payload": "45",
"payloadType": "num",
"x": 170,
"y": 1560,
"wires": [
[
"f2cbd8ce.34c4c8"
]
]
},
{
"id": "50651c06.281524",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "Temperature",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 550,
"y": 1520,
"wires": []
},
{
"id": "2786389.31cc5c8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "Humidity",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 540,
"y": 1560,
"wires": []
},
{
"id": "e4db256d.a80ca8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "Pressure",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 540,
"y": 1600,
"wires": []
},
{
"id": "11b023ef.147c9c",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "例8: pressure",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "pressure",
"payload": "1001",
"payloadType": "num",
"x": 170,
"y": 1600,
"wires": [
[
"f2cbd8ce.34c4c8"
]
]
},
{
"id": "c619693c.5d47e8",
"type": "comment",
"z": "e110d6b7dc11f739",
"name": "Route a message based on a context value",
"info": "Switch node can route a message to different flows according to the current value of a flow context property.\n\nSee Node-RED cookbook [item](https://cookbook.nodered.org/basic/route-on-context).",
"x": 220,
"y": 1680,
"wires": []
},
{
"id": "fd7378.4448ec88",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "Inject",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 190,
"y": 1800,
"wires": [
[
"85c9adae.e7d27"
]
]
},
{
"id": "85c9adae.e7d27",
"type": "switch",
"z": "e110d6b7dc11f739",
"name": "Context based routing",
"property": "state",
"propertyType": "flow",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "num"
},
{
"t": "eq",
"v": "2",
"vt": "num"
},
{
"t": "eq",
"v": "3",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 400,
"y": 1800,
"wires": [
[
"beb75c77.48c8c"
],
[
"ec521bae.6d9da8"
],
[
"46b7be29.5ee9b"
]
]
},
{
"id": "beb75c77.48c8c",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "Output 1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 640,
"y": 1740,
"wires": []
},
{
"id": "ec521bae.6d9da8",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "Output 2",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 640,
"y": 1800,
"wires": []
},
{
"id": "46b7be29.5ee9b",
"type": "debug",
"z": "e110d6b7dc11f739",
"name": "Output 3",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 640,
"y": 1860,
"wires": []
},
{
"id": "20591916.597ac6",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "Set state 0",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 200,
"y": 1880,
"wires": [
[
"d2c4662.6f8e098"
]
]
},
{
"id": "22bf9e89.7f55f2",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "Set state 1",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 200,
"y": 1920,
"wires": [
[
"d2c4662.6f8e098"
]
]
},
{
"id": "c2e46f80.42ee5",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "Set state 2",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "2",
"payloadType": "num",
"x": 200,
"y": 1960,
"wires": [
[
"d2c4662.6f8e098"
]
]
},
{
"id": "ff766a3e.5c5fe8",
"type": "inject",
"z": "e110d6b7dc11f739",
"name": "Set state 3",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "3",
"payloadType": "num",
"x": 200,
"y": 2000,
"wires": [
[
"d2c4662.6f8e098"
]
]
},
{
"id": "d2c4662.6f8e098",
"type": "change",
"z": "e110d6b7dc11f739",
"name": "Set flow.state",
"rules": [
{
"t": "set",
"p": "state",
"pt": "flow",
"to": "payload",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 420,
"y": 1920,
"wires": [
[]
]
}
]
フローには、公式サンプルの9つの例が、上から順に並んでいます。それぞれの先頭に、英語の comment ノードがあります。名前の無かった inject と debug には、「例1: -10」「例1: Negative の結果」のように、例の番号を付けた名前を付けてあります。
プロパティ: msg.payload
ルール1: < 0 → 出力1
ルール2: == 0 → 出力2
ルール3: > 0 → 出力3
評価の範囲: 全ての条件を適用(この3つの条件は、同時に2つ以上に合うことが無い)
プロパティ: msg.topic
ルール1: == "temperature" → 出力1
ルール2: == "humidity" → 出力2
ルール3: == "pressure" → 出力3
複数センサーからのMQTTメッセージ
↓
Switchノード(msg.topicで判定)
├─ sensors/temperature → 温度処理フロー
├─ sensors/humidity → 湿度処理フロー
├─ sensors/pressure → 気圧処理フロー
└─ otherwise → ログ記録
※ センサー種別ごとに異なる処理を実行
HTTP Inノード
↓
Switchノード(msg.req.methodで判定)
├─ GET → データ取得処理
├─ POST → データ作成処理
├─ PUT → データ更新処理
├─ DELETE → データ削除処理
└─ otherwise → 405 Method Not Allowed
※ RESTful APIのルーティング
エラーメッセージ受信
↓
Switchノード(msg.payload.levelで判定)
├─ "critical" → 緊急通知 + 自動復旧
├─ "error" → アラート送信
├─ "warning" → ログ記録
└─ "info" → 統計更新のみ
※ ログレベルに応じた処理の分岐
ユーザーリクエスト
↓
Switchノード(msg.user.roleで判定)
├─ "admin" → 全機能アクセス
├─ "editor" → 編集機能のみ
├─ "viewer" → 閲覧のみ
└─ otherwise → アクセス拒否
※ 権限ベースのアクセス制御