Node-RED Switchノード ガイド

☁️ クラウドOK 環境とは?

Node-REDのSwitchノードについて、初心者向けに説明します。

1. Switchノードとは

「Switch」の役割

Switchノードは、条件に応じてメッセージの送り先を振り分けるノードです。 郵便局の仕分けセンターに例えると分かりやすいでしょう。

郵便仕分けセンターに例えると:

基本的な動作

Inject → Switch →出力1 →出力2 →出力3

Switchノードは入力メッセージを評価し、条件に一致する出力ポートへメッセージを送信します。 複数の条件を設定でき、プログラミングのif-else文やswitch-case文に相当します。

Switchノードの動作イメージ:

入力
msg.payload
→ Switch
条件評価
→< 0→負の数 →== 0→ゼロ →> 0→正の数

動作を図で見る

入ってきた msg は、条件に合う出力の端子から出ていきます。どの条件にも合わない msg は、「その他」の条件を置いていなければどこからも出ません。

payload: -3payload: 0payload: 7switch入ってくる msg① < 0payload: -3② == 0payload: 0③ > 0payload: 7条件に合う出力から出る
payload の値によって、出ていく端子が変わる

複数の条件に合う msg をどう扱うかは、条件リストの下の「評価の範囲」で決まります。

全ての条件を適用(既定)payload: 20switch> 1020> 020その他出ない最初に合致した条件で終了payload: 20switch> 1020> 0出ないその他出ない
「全ての条件を適用」では、合う条件の端子すべてから同じ msg が出る

2. Switchノードの条件タイプ

比較 値の比較

==, !=, <, >, <=, >= などで比較

例: payload == 100 例: payload > 0 例: payload != "error"

範囲 値の範囲

値が指定した範囲内かどうか

例: 10 <= payload <= 50 例: payload が 0〜100の間

正規表現 パターンマッチ

正規表現で文字列をマッチング

例: ^error.* 例: .*@gmail\.com$

型チェック データ型

値のデータ型をチェック

例: is null 例: is not null 例: is of type number

シーケンス 順序制御

メッセージシーケンスの順序で判定

例: head (最初のN件) 例: tail (最後のN件) 例: index (特定位置)

JSONata 式評価

JSONata式で複雑な条件を記述

例: payload.temp > 30 例: $count(items) > 5

条件タイプ一覧

条件タイプ 記号/名称 説明 例
等しい == 値が等しい payload == 100
等しくない != 値が等しくない payload != 0
より小さい < 値より小さい payload < 0
以下 <= 値以下 payload <= 100
より大きい > 値より大きい payload > 0
以上 >= 値以上 payload >= 0
範囲内 between 範囲内にある 0 <= payload <= 100
含む contains 文字列を含む payload に "error" を含む
正規表現 matches regex 正規表現にマッチ ^[0-9]+$
型チェック is of type データ型を確認 is of type string
空でない is not empty 配列・文字列・オブジェクトが空でない payload is not empty
それ以外 otherwise どの条件にも一致しない場合 デフォルト処理

設定項目

設定項目説明既定値
プロパティ各ルールで評価する対象の値。msg のプロパティや式を指定する。msg.payload
条件振り分けの判定ルール。1 行が 1 つの出力端子に対応し、上から順に評価される。複数追加できる。1 件(==)
評価の範囲条件リストの下にあるドロップダウン。「全ての条件を適用」は一致したすべてのルールの端子へ送る。「最初に合致した条件で終了」は最初に一致した端子だけに送り、残りは評価しない。全ての条件を適用
メッセージ列の補正分割済みメッセージ(msg.parts 付き)を振り分けたとき、各端子で parts を振り直して列として整合させる。Split と組み合わせるときに使う。オフ
名前エディター上の表示名。動作には影響しない。(空)

3. 実用的な使用パターン

サンプルフローのインポート方法:

  1. 下のサンプルフローJSONをコピー
  2. Node-REDエディタで メニュー → 読み込み を選択
  3. JSONをペーストして「読み込み」をクリック

このサンプルフローには、以下で説明する各パターンの実例が含まれています。

サンプルフローは、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 の結果」のように、例の番号を付けた名前を付けてあります。

例1〜6 は、同じ作りです。inject のボタンを押すと、switch が msg を振り分けます。振り分け先の change が、msg.payload をその出力の名前(negative など)に書き換え、debug に出します。どの端子から出たかを、debug に出る文字で見分けるための作りです。

例 comment ノードの見出し(意味) switch の設定 見どころ
例1 Select output port based on input message value(入力の値で、出力の端子を選ぶ) 「プロパティ」は msg.payload。条件は < 0、== 0、> 0 の3つ 基本の形。条件1つが、出力の端子1つに当たる。パターン1で詳しく説明する
例2 Check all rules(すべての条件を調べる) 条件は < 10、> -10 の2つ。「全ての条件を適用」 両方の条件に合う値(0)は、2つの端子の両方から出る
例3 Stop after first match(最初に合ったところで止める) 条件は例2と同じ。「最初に合致した条件で終了」 両方の条件に合う値(0)でも、上の条件の端子からだけ出る。例2と押し比べる
例4 Select output based on type of payload value(payload の型で、出力を選ぶ) 条件は「指定型である」の「文字列」と「数値」 同じ 128 でも、数値か文字列かで行き先が変わる
例5 Use JSONata expression for rules(条件に JSONata 式を使う) 条件は「JSONata式」の (payload % 2) = 0 と (payload % 2) = 1 条件の側に式を書く。2で割った余りで、偶数と奇数に分ける。式の中の payload は msg.payload のこと
例6 Use JSONata expression for switch value(調べる値に JSONata 式を使う) 「プロパティ」が JSONata 式の (payload % 2) = 0。条件は「trueである」と「falseである」 「プロパティ」の側に式を書き、その結果が true か false かで分ける。サンプルの入力(7 と 8)では、結果は例5と同じ
例7 Recreate message sequence(メッセージ列を作り直す) 条件は > 0 と「その他」。「メッセージ列の補正」がオン inject → split → switch →「Positive」または「Negative」(join)→ debug の並び。split で1個ずつに分けた配列を、正の数とそれ以外に分け、それぞれを join で配列に戻す
例8 Route a message based on one of its properties(msg のプロパティの値で振り分ける) 「プロパティ」は msg.topic。条件は == temperature、== humidity、== pressure msg.payload 以外のプロパティで分ける。change は無く、debug に msg.payload がそのまま出る。パターン2で詳しく説明する
例9 Route a message based on a context value(コンテキストの値で振り分ける) 「プロパティ」は flow.state。条件は == 1、== 2、== 3 msg の中身ではなく、フローに覚えさせた値で分ける。「Set state 0」〜「Set state 3」(inject)→「Set flow.state」(change)が、値を覚えさせる側

ボタンを押すと出るもの:

例 押すボタンと、デバッグサイドバーに出るもの
例1 「例1: -10」→「例1: Negative の結果」に negative。「例1: 0」→「例1: Zero の結果」に zero。「例1: 10」→「例1: Positive の結果」に positive
例2 「例2: -10」→「例2: <10 の結果」に <10。「例2: 0」→「例2: <10 の結果」と「例2: >-10 の結果」の両方に出る。「例2: 10」→「例2: >-10 の結果」に >-10
例3 「例3: -10」→「例3: <10 の結果」に <10。「例3: 0」→「例3: <10 の結果」だけに出る(> -10 にも合うが、上の条件で止まる)。「例3: 10」→「例3: >-10 の結果」に >-10
例4 「Number:128」→「例4: Number の結果」に number。「String:128」→「例4: String の結果」に string
例5・例6 「例5: 7」→「例5: Odd の結果」に odd。「例5: 8」→「例5: Even の結果」に even。例6も同じ(7 は odd、8 は even)
例7 「例7: 配列」([3, -2, -1, 1, -3, 2] を送る)→「例7: Positive の結果」に [3,1,2]、「例7: Negative の結果」に [-2,-1,-3]
例8 「例8: temperature」→「Temperature」に 27。「例8: humidity」→「Humidity」に 45。「例8: pressure」→「Pressure」に 1001
例9 フローを読み込んで最初にデプロイしたあと、すぐに「Inject」を押しても、何も出ない(flow.state がまだ無く、どの条件にも合わないため)。「Set state 1」を押してから「Inject」を押すと、「Output 1」に時刻を表す数が出る。「Set state 2」のあとは「Output 2」、「Set state 3」のあとは「Output 3」に出る。「Set state 0」のあとは、どの条件にも合わないので、何も出ない

例9の flow.state は、フローコンテキスト(同じタブの中のノードが共有して使える、値の置き場所)です。値を入れるボタンと、「Inject」のボタンは、配線でつながっていません。switch が、msg とは別のところに置いた値を見て行き先を決めていることが分かります。

パターン1: 数値の正負判定

数値が正・ゼロ・負かを判定して振り分けるパターンです。サンプルフローの例1に当たります。

用途: 数値の符号(正・ゼロ・負)で処理を振り分け

例1: -10 / 0 / 10 → Switch →Negative→例1: Negative の結果 →Zero→例1: Zero の結果 →Positive→例1: Positive の結果

ルール設定:

1 payload < 0 → 出力1(負の数)
2 payload == 0 → 出力2(ゼロ)
3 payload > 0 → 出力3(正の数)
-10 → 出力1: negative
0 → 出力2: zero
10 → 出力3: positive

設定例:

プロパティ: msg.payload ルール1: < 0 → 出力1 ルール2: == 0 → 出力2 ルール3: > 0 → 出力3 評価の範囲: 全ての条件を適用(この3つの条件は、同時に2つ以上に合うことが無い)

パターン2: トピックによるルーティング

msg.topicの値でメッセージを振り分けるパターンです。サンプルフローの例8に当たります。

用途: 送信元や種類を表すトピック名による処理先の振り分け

temp/humid/press → Switch
(topic)
→Temperature →Humidity →Pressure

センサー振り分けの用途:

temperature → 出力1: 温度処理
humidity → 出力2: 湿度処理
pressure → 出力3: 気圧処理

設定例:

プロパティ: msg.topic ルール1: == "temperature" → 出力1 ルール2: == "humidity" → 出力2 ルール3: == "pressure" → 出力3

ほかの例と、サンプルフローに無い条件:

4. 実践演習

演習1: 成績判定初級

テストの点数に応じて評価(A/B/C/D)を出力するフローを作成してください。

💼 使いどころ: 点数など数値の範囲に応じて処理を振り分けたい場面で使います。

要求仕様:

期待される動作:

成功の条件:

ヒント

Switchノードの設定:

  • プロパティ: msg.payload
  • ルール1: >= 90 → A
  • ルール2: >= 70 → B
  • ルール3: >= 50 → C
  • ルール4: otherwise → D

「最初に一致で終了」を有効にする(または条件を排他的に設定)。

解答例フロー
[ { "id": "switch_node_guide_tab_1", "type": "tab", "label": "switch_node_guide 例2", "disabled": false, "info": "", "env": [] }, { "id": "ex1_inject", "type": "inject", "z": "switch_node_guide_tab_1", "name": "85点", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "85", "payloadType": "num", "x": 150, "y": 100, "wires": [ [ "ex1_switch" ] ] }, { "id": "ex1_switch", "type": "switch", "z": "switch_node_guide_tab_1", "name": "成績判定", "property": "payload", "propertyType": "msg", "rules": [ { "t": "gte", "v": "90", "vt": "num" }, { "t": "gte", "v": "70", "vt": "num" }, { "t": "gte", "v": "50", "vt": "num" }, { "t": "else" } ], "checkall": "false", "repair": false, "outputs": 4, "x": 300, "y": 100, "wires": [ [ "ex1_debug_a" ], [ "ex1_debug_b" ], [ "ex1_debug_c" ], [ "ex1_debug_d" ] ] }, { "id": "ex1_debug_a", "type": "debug", "z": "switch_node_guide_tab_1", "name": "A評価", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 470, "y": 40, "wires": [] }, { "id": "ex1_debug_b", "type": "debug", "z": "switch_node_guide_tab_1", "name": "B評価", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 470, "y": 80, "wires": [] }, { "id": "ex1_debug_c", "type": "debug", "z": "switch_node_guide_tab_1", "name": "C評価", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 470, "y": 120, "wires": [] }, { "id": "ex1_debug_d", "type": "debug", "z": "switch_node_guide_tab_1", "name": "D評価", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 470, "y": 160, "wires": [] } ]

演習2: コマンド処理中級

文字列コマンド(start/stop/restart)を判定し、それぞれ異なる処理を行うフローを作成してください。

💼 使いどころ: 受け取った文字列がどの既知コマンドに該当するかを判定し、未知の入力と区別する場面で使います。

要求仕様:

期待される動作:

成功の条件:

ヒント

Switchノードの設定:

  • プロパティ: msg.payload
  • ルール: 文字列の完全一致(==)を使用
  • 最後に「otherwise」で不明コマンドをキャッチ

Changeノードの設定:

  • 各出力ポートにChangeノードを接続
  • msg.payloadを対応するメッセージに変更
解答例フロー
[ { "id": "switch_node_guide_tab_2", "type": "tab", "label": "switch_node_guide 例3", "disabled": false, "info": "", "env": [] }, { "id": "ex2_inject_start", "type": "inject", "z": "switch_node_guide_tab_2", "name": "start", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "start", "payloadType": "str", "x": 150, "y": 80, "wires": [ [ "ex2_switch" ] ] }, { "id": "ex2_inject_stop", "type": "inject", "z": "switch_node_guide_tab_2", "name": "stop", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "stop", "payloadType": "str", "x": 150, "y": 120, "wires": [ [ "ex2_switch" ] ] }, { "id": "ex2_inject_other", "type": "inject", "z": "switch_node_guide_tab_2", "name": "unknown", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "hello", "payloadType": "str", "x": 150, "y": 160, "wires": [ [ "ex2_switch" ] ] }, { "id": "ex2_switch", "type": "switch", "z": "switch_node_guide_tab_2", "name": "コマンド判定", "property": "payload", "propertyType": "msg", "rules": [ { "t": "eq", "v": "start", "vt": "str" }, { "t": "eq", "v": "stop", "vt": "str" }, { "t": "eq", "v": "restart", "vt": "str" }, { "t": "else" } ], "checkall": "false", "repair": false, "outputs": 4, "x": 320, "y": 120, "wires": [ [ "ex2_change_start" ], [ "ex2_change_stop" ], [ "ex2_change_restart" ], [ "ex2_change_unknown" ] ] }, { "id": "ex2_change_start", "type": "change", "z": "switch_node_guide_tab_2", "name": "起動メッセージ", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "システムを起動します", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 520, "y": 60, "wires": [ [ "ex2_debug" ] ] }, { "id": "ex2_change_stop", "type": "change", "z": "switch_node_guide_tab_2", "name": "停止メッセージ", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "システムを停止します", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 520, "y": 100, "wires": [ [ "ex2_debug" ] ] }, { "id": "ex2_change_restart", "type": "change", "z": "switch_node_guide_tab_2", "name": "再起動メッセージ", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "システムを再起動します", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 530, "y": 140, "wires": [ [ "ex2_debug" ] ] }, { "id": "ex2_change_unknown", "type": "change", "z": "switch_node_guide_tab_2", "name": "不明メッセージ", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "不明なコマンドです", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 520, "y": 180, "wires": [ [ "ex2_debug" ] ] }, { "id": "ex2_debug", "type": "debug", "z": "switch_node_guide_tab_2", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 710, "y": 120, "wires": [] } ]

演習3: 温度アラート中級

温度センサーの値に応じて、正常/警告/危険のアラートレベルを判定するフローを作成してください。

💼 使いどころ: 計測値がどのしきい値範囲に入るかで、警告レベルを判定する場面で使います。

要求仕様:

期待される動作:

成功の条件:

ヒント

Switchノードの設定:

  • プロパティ: msg.payload
  • 「is between」条件を使用
  • 複数の条件を組み合わせる

条件の順序:

  • より具体的な条件を先に配置
  • または「最初に一致で終了」を使用
解答例フロー
[ { "id": "switch_node_guide_tab_3", "type": "tab", "label": "switch_node_guide 例4", "disabled": false, "info": "", "env": [] }, { "id": "ex3_inject_normal", "type": "inject", "z": "switch_node_guide_tab_3", "name": "22度", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "22", "payloadType": "num", "x": 150, "y": 80, "wires": [ [ "ex3_switch" ] ] }, { "id": "ex3_inject_warning", "type": "inject", "z": "switch_node_guide_tab_3", "name": "28度", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "28", "payloadType": "num", "x": 150, "y": 120, "wires": [ [ "ex3_switch" ] ] }, { "id": "ex3_inject_danger", "type": "inject", "z": "switch_node_guide_tab_3", "name": "35度", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "35", "payloadType": "num", "x": 150, "y": 160, "wires": [ [ "ex3_switch" ] ] }, { "id": "ex3_switch", "type": "switch", "z": "switch_node_guide_tab_3", "name": "温度判定", "property": "payload", "propertyType": "msg", "rules": [ { "t": "btwn", "v": "18", "vt": "num", "v2": "26", "v2t": "num" }, { "t": "btwn", "v": "10", "vt": "num", "v2": "18", "v2t": "num" }, { "t": "btwn", "v": "26", "vt": "num", "v2": "30", "v2t": "num" }, { "t": "else" } ], "checkall": "false", "repair": false, "outputs": 4, "x": 320, "y": 120, "wires": [ [ "ex3_change_normal" ], [ "ex3_change_warning_low" ], [ "ex3_change_warning_high" ], [ "ex3_change_danger" ] ] }, { "id": "ex3_change_normal", "type": "change", "z": "switch_node_guide_tab_3", "name": "正常", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "🟢 正常: 快適な温度です", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 490, "y": 60, "wires": [ [ "ex3_debug" ] ] }, { "id": "ex3_change_warning_low", "type": "change", "z": "switch_node_guide_tab_3", "name": "警告(低)", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "🟡 警告: 温度が低めです", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 500, "y": 100, "wires": [ [ "ex3_debug" ] ] }, { "id": "ex3_change_warning_high", "type": "change", "z": "switch_node_guide_tab_3", "name": "警告(高)", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "🟡 警告: 温度が高めです", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 500, "y": 140, "wires": [ [ "ex3_debug" ] ] }, { "id": "ex3_change_danger", "type": "change", "z": "switch_node_guide_tab_3", "name": "危険", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "🔴 危険: 異常温度です!", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 490, "y": 180, "wires": [ [ "ex3_debug" ] ] }, { "id": "ex3_debug", "type": "debug", "z": "switch_node_guide_tab_3", "name": "アラート", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 670, "y": 120, "wires": [] } ]

演習4: 配列の正負分離上級

数値の配列を入力し、正の数と負の数を別々の配列に分離してください。

💼 使いどころ: 数値配列の要素を正負で仕分けて、それぞれ別の配列にまとめる場面で使います。

要求仕様:

期待される動作:

成功の条件:

ヒント

フローの構成:

  1. Inject: JSON配列を入力
  2. Split: 配列を個別メッセージに分割
  3. Switch: > 0 と < 0 で振り分け
  4. Join: 各出力を配列に再結合(手動モード・カウント=3)

Joinノードの設定:

  • モード: 手動(custom)
  • 結合の数: 3(各出力に振り分けられる件数)
  • 各メッセージ部: 配列(array)

なぜ自動モードではないのか: Joinの自動モードは「分割前の総数(ここでは6件)」が揃うまで待って再結合します。間にSwitchを挟むと各Joinには3件しか届かないため、自動モードでは6件に到達せず出力されません。振り分けてから再結合する場合は、手動モードで件数(ここでは3)を指定します。

解答例フロー
[ { "id": "switch_node_guide_tab_4", "type": "tab", "label": "switch_node_guide 例5", "disabled": false, "info": "", "env": [] }, { "id": "ex4_inject", "type": "inject", "z": "switch_node_guide_tab_4", "name": "数値配列", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "[3, -2, -1, 1, -3, 2]", "payloadType": "json", "x": 160, "y": 100, "wires": [ [ "ex4_split" ] ] }, { "id": "ex4_split", "type": "split", "z": "switch_node_guide_tab_4", "name": "分割", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "x": 310, "y": 100, "wires": [ [ "ex4_switch" ] ] }, { "id": "ex4_switch", "type": "switch", "z": "switch_node_guide_tab_4", "name": "正負判定", "property": "payload", "propertyType": "msg", "rules": [ { "t": "gt", "v": "0", "vt": "num" }, { "t": "lt", "v": "0", "vt": "num" } ], "checkall": "true", "repair": false, "outputs": 2, "x": 460, "y": 100, "wires": [ [ "ex4_join_positive" ], [ "ex4_join_negative" ] ] }, { "id": "ex4_join_positive", "type": "join", "z": "switch_node_guide_tab_4", "name": "正の数", "mode": "custom", "build": "array", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": false, "timeout": "", "count": "3", "reduceRight": false, "x": 620, "y": 80, "wires": [ [ "ex4_debug_positive" ] ] }, { "id": "ex4_join_negative", "type": "join", "z": "switch_node_guide_tab_4", "name": "負の数", "mode": "custom", "build": "array", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": false, "timeout": "", "count": "3", "reduceRight": false, "x": 620, "y": 120, "wires": [ [ "ex4_debug_negative" ] ] }, { "id": "ex4_debug_positive", "type": "debug", "z": "switch_node_guide_tab_4", "name": "正の配列", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 790, "y": 80, "wires": [] }, { "id": "ex4_debug_negative", "type": "debug", "z": "switch_node_guide_tab_4", "name": "負の配列", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 790, "y": 120, "wires": [] } ]

5. まとめ

Switchノードのポイント

よくある間違い

次のステップ

次は以下のノードも学習するとよいでしょう:

6. トラブルシューティング

よくある問題と解決方法

問題 原因 解決方法
どの出力にも送信されない 条件に一致しない otherwiseルールを追加、または条件を見直し
複数の出力に送信される 全ルール確認が有効 「最初に一致で終了」を有効化、または条件を排他的に
数値比較が動作しない 文字列として比較されている 比較値の型を「数値」に設定
正規表現がマッチしない パターンエラー 正規表現をテストツールで確認
期待と異なる出力に送信 条件の順序問題 条件の順序を見直し(厳しい条件を先に)
範囲チェックが想定外 境界値の扱い betweenは両端を含むことを確認

7. 実務での活用例

ケース1: IoTセンサーデータの振り分け

複数センサーからのMQTTメッセージ ↓ Switchノード(msg.topicで判定) ├─ sensors/temperature → 温度処理フロー ├─ sensors/humidity → 湿度処理フロー ├─ sensors/pressure → 気圧処理フロー └─ otherwise → ログ記録 ※ センサー種別ごとに異なる処理を実行

ケース2: HTTPリクエストのルーティング

HTTP Inノード ↓ Switchノード(msg.req.methodで判定) ├─ GET → データ取得処理 ├─ POST → データ作成処理 ├─ PUT → データ更新処理 ├─ DELETE → データ削除処理 └─ otherwise → 405 Method Not Allowed ※ RESTful APIのルーティング

ケース3: エラーレベル別処理

エラーメッセージ受信 ↓ Switchノード(msg.payload.levelで判定) ├─ "critical" → 緊急通知 + 自動復旧 ├─ "error" → アラート送信 ├─ "warning" → ログ記録 └─ "info" → 統計更新のみ ※ ログレベルに応じた処理の分岐

ケース4: ユーザー権限チェック

ユーザーリクエスト ↓ Switchノード(msg.user.roleで判定) ├─ "admin" → 全機能アクセス ├─ "editor" → 編集機能のみ ├─ "viewer" → 閲覧のみ └─ otherwise → アクセス拒否 ※ 権限ベースのアクセス制御

8. 条件タイプ詳細リファレンス

比較演算子

演算子 内部値 説明
==eq等しい
!=neq等しくない
<ltより小さい
<=lte以下
>gtより大きい
>=gte以上
betweenbtwn範囲内
containscont含む
matches regexregex正規表現一致
is truetrue真
is falsefalse偽
is nullnullnull
is not nullnnullnullでない
is of typeistype型チェック
is emptyempty空
is not emptynempty空でない
otherwiseelseその他

9. 追加リソース


参照元:NodeREDエディター内サンプルフロー

🏠