既定の設定では、trigger ノードは msg が届くとすぐに 1 を送り、250 ミリ秒待ってから 0 を送ります。待っているあいだに届いた msg は無視されます。
サンプルフローは、Node-RED本体に同梱されている公式サンプル4つ(「Outputs two value with interval」から「Timeout processing using trigger node」まで)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → function → trigger からも、元の4つを開けます。
📋 サンプルフロー(クリックで展開)
参照元:NodeREDエディター内サンプルフロー
[
{
"id": "503396c8fbe672b0",
"type": "tab",
"label": "trigger",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ec5a531b.68b65",
"type": "inject",
"z": "503396c8fbe672b0",
"name": "例1: 実行",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 100,
"wires": [
[
"cb5e0c78.4bf3d"
]
]
},
{
"id": "1b0f8c3e.1fd7e4",
"type": "debug",
"z": "503396c8fbe672b0",
"name": "例1: 結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 490,
"y": 100,
"wires": []
},
{
"id": "cb5e0c78.4bf3d",
"type": "trigger",
"z": "503396c8fbe672b0",
"name": "",
"op1": "1",
"op2": "0",
"op1type": "str",
"op2type": "str",
"duration": "2",
"extend": false,
"units": "s",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 320,
"y": 100,
"wires": [
[
"1b0f8c3e.1fd7e4"
]
]
},
{
"id": "4e5bf6b2.b4dd58",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "Oputputs two values with interval",
"info": "Outputs 1. Then output 0 after a certain period of time.\n\n*This could be used, for example, to blink an LED attached to a Raspberry Pi GPIO pin.*",
"x": 170,
"y": 40,
"wires": []
},
{
"id": "87bd706a.aec93",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "Trigger a flow if a message isn't received after a defined time",
"info": "Trigger node can be used to wait a specified amount of time to send a message.\n\nSee Node-RED [cookbook](https://cookbook.nodered.org/basic/trigger-timeout).",
"x": 300,
"y": 320,
"wires": []
},
{
"id": "b1b5829a.b43ff",
"type": "debug",
"z": "503396c8fbe672b0",
"name": "例2: 入力",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 550,
"y": 380,
"wires": []
},
{
"id": "e4820019.7741b",
"type": "inject",
"z": "503396c8fbe672b0",
"name": "例2: 実行",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 200,
"y": 380,
"wires": [
[
"cf16fb34.738f28",
"b1b5829a.b43ff"
]
]
},
{
"id": "cf16fb34.738f28",
"type": "trigger",
"z": "503396c8fbe672b0",
"name": "Watchdog",
"op1": "",
"op2": "timeout",
"op1type": "nul",
"op2type": "str",
"duration": "5",
"extend": true,
"units": "s",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 370,
"y": 420,
"wires": [
[
"124f656.5f0c09b"
]
]
},
{
"id": "124f656.5f0c09b",
"type": "debug",
"z": "503396c8fbe672b0",
"name": "例2: タイムアウト",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 550,
"y": 420,
"wires": []
},
{
"id": "d55745be.e4ecd8",
"type": "inject",
"z": "503396c8fbe672b0",
"name": "例3: 実行",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 660,
"wires": [
[
"ecac46bf.9751c8",
"cf35bbed.ce1298"
]
]
},
{
"id": "ecac46bf.9751c8",
"type": "debug",
"z": "503396c8fbe672b0",
"name": "例3: 結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 670,
"y": 660,
"wires": []
},
{
"id": "cf35bbed.ce1298",
"type": "trigger",
"z": "503396c8fbe672b0",
"name": "",
"op1": "reset",
"op2": "true",
"op1type": "str",
"op2type": "bool",
"duration": "2",
"extend": true,
"units": "s",
"reset": "",
"bytopic": "all",
"outputs": 1,
"x": 320,
"y": 700,
"wires": [
[
"febbebba.455238"
]
]
},
{
"id": "febbebba.455238",
"type": "trigger",
"z": "503396c8fbe672b0",
"name": "",
"op1": "0",
"op2": "0",
"op1type": "num",
"op2type": "str",
"duration": "-2",
"extend": false,
"units": "s",
"reset": "reset",
"bytopic": "all",
"outputs": 1,
"x": 480,
"y": 700,
"wires": [
[
"ecac46bf.9751c8"
]
]
},
{
"id": "4bad6a7.9a1d194",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "Send placeholder messages when a stream stops sending",
"info": "Trigger node can be used to detect when a message has not arrived after a defined interval and a second Trigger node to send the placeholder messages at a regular interval..\n\nSee Node-RED [cookbook](https://cookbook.nodered.org/basic/trigger-placeholder).",
"x": 250,
"y": 600,
"wires": []
},
{
"id": "d24083e7.fedc7",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "Timeout processing using trigger node",
"info": "It is possible to configure timeout processing by combining two Trigger nodes. \n\nIn the first Trigger node settings panel, `send second message to separate output` checkbox is checked. With this specification, this trigger node will have two output ports. When the node receives an input message, it outputs the message to the first port, and after a specified period of time, it outputs the specified message to the second port.\n\nThe second trigger node is specified to handle each message ID (`msg._msgid`). This means that only the first received message among the messages with the same ID is transmitted. The time specified for it represents the retention time of information for message filtering and must be longer than the processing time for the timeout.\n",
"x": 190,
"y": 840,
"wires": []
},
{
"id": "8dcb51d5.90fbb",
"type": "inject",
"z": "503396c8fbe672b0",
"name": "例4: 1秒かかる処理",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 150,
"y": 940,
"wires": [
[
"69b526f1.347808"
]
]
},
{
"id": "3b2238f8.71f6c8",
"type": "debug",
"z": "503396c8fbe672b0",
"name": "例4: 結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 870,
"y": 980,
"wires": []
},
{
"id": "69b526f1.347808",
"type": "trigger",
"z": "503396c8fbe672b0",
"name": "",
"op1": "",
"op2": "0",
"op1type": "pay",
"op2type": "str",
"duration": "3",
"extend": false,
"units": "s",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 2,
"x": 320,
"y": 980,
"wires": [
[
"3802c45b.84121c"
],
[
"a3c01d51.86cbb"
]
]
},
{
"id": "7096b59a.c265fc",
"type": "trigger",
"z": "503396c8fbe672b0",
"name": "",
"op1": "",
"op2": "",
"op1type": "pay",
"op2type": "nul",
"duration": "10",
"extend": false,
"units": "s",
"reset": "",
"bytopic": "topic",
"topic": "_msgid",
"outputs": 1,
"x": 690,
"y": 980,
"wires": [
[
"3b2238f8.71f6c8"
]
]
},
{
"id": "3802c45b.84121c",
"type": "function",
"z": "503396c8fbe672b0",
"name": "Target Process",
"func": "// Wait for a specified time period, then send a message.\n// Must preserve incoming message id.\nvar wait = msg.payload;\nsetTimeout(function() {\n msg.payload = \"done: \"+wait+\"s\";\n node.send(msg);\n}, wait*1000);\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 500,
"y": 900,
"wires": [
[
"7096b59a.c265fc"
]
]
},
{
"id": "a3c01d51.86cbb",
"type": "change",
"z": "503396c8fbe672b0",
"name": "Set timeout flag",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "timeout",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 500,
"y": 1020,
"wires": [
[
"7096b59a.c265fc"
]
]
},
{
"id": "ae217597.bedf88",
"type": "inject",
"z": "503396c8fbe672b0",
"name": "例4: 5秒かかる処理",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "5",
"payloadType": "num",
"x": 150,
"y": 1020,
"wires": [
[
"69b526f1.347808"
]
]
},
{
"id": "dc6f5bbe.dc8bd8",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "↓ target process",
"info": "",
"x": 500,
"y": 860,
"wires": []
},
{
"id": "fe41a803.a14158",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "↓ send second message after specified time",
"info": "",
"x": 430,
"y": 940,
"wires": []
},
{
"id": "fbb2dff0.0bacb",
"type": "comment",
"z": "503396c8fbe672b0",
"name": "↓ pass first message with same ID",
"info": "",
"x": 760,
"y": 940,
"wires": []
}
]
フローには、公式サンプルの4つの例が、上から順に並んでいます。それぞれの先頭に、英語の comment ノードがあります。例の番号が、下のパターンの番号に当たります。trigger ノードには名前の無いものが多いので、ここでは種類の名前で呼びます。
送信データ: 何も送らない
再送信データ: 文字列 "timeout"
待機時間: 5秒
延長: ✓ 有効(入力ごとにタイマーリセット)
動作: 5秒間入力がない → "timeout"を出力
送信データ: ペイロード(入力値をそのまま)
再送信データ: 文字列 "0"(または"timeout")
待機時間: 3秒
出力: 別の出力に送信(2ポート)
出力1 → 処理フローへ(即座)
出力2 → タイムアウト処理へ(3秒後)
演習4: 処理タイムアウト上級
📝 課題:
処理を開始し、3秒以内に完了しなければタイムアウトとするフローを作成してください。2出力モードを使用します。
💼 使いどころ: 時間のかかる処理に上限時間を設け、完了とタイムアウトのうち先に届いた方だけを結果として扱う場面で使います。
🎯 要求仕様:
- Triggerノードの2出力モードを使用
- 出力1 → 処理を実行(ChangeノードとDelayノードで、1秒/5秒かかる処理を再現する)
- 出力2 → 3秒後にタイムアウト
- 処理完了またはタイムアウトの早い方を結果として出力
📊 期待される動作:
- 1秒処理 → "done: 1s"が出力(タイムアウト前に完了)
- 5秒処理 → "timeout"が出力(処理完了前にタイムアウト)
✅ 成功の条件:
- 「1秒処理」Injectを押すと、約1秒後に
"done: 1s" がデバッグパネルに表示される(タイムアウトメッセージは表示されない)
- 「5秒処理」Injectを押すと、約3秒後に
"timeout" がデバッグパネルに表示される(処理完了メッセージは表示されない)
- どちらの場合も最終的なデバッグパネルへの出力は1つだけ(重複して表示されない)
- 2出力モードのTriggerノードで出力1(処理実行)と出力2(タイムアウト)が別々のパスに接続されている
💡 ヒント
フローの構成:
- Triggerノード: 2出力モード、3秒タイムアウト
- 出力1 → Changeノード → Delayノード(遅延処理)→ 結果へ。Changeノードで
msg.delay に「JSONata式」型の payload * 1000(秒をミリ秒に換算)、msg.payload に "done: " & payload & "s" を設定し、Delayノードの「動作」を「msg.delay に遅延を設定」にする。Delayノードは msg.delay のミリ秒だけ待ってからメッセージを送る
- 出力2 → Changeノード("timeout"設定)→ 結果へ
- 結果用Triggerノード: msg._msgidで重複排除
重複排除のTrigger設定:
- 送信データ: ペイロード
- 再送信データ: 何も送らない
- トピック別: msg._msgid
- 待機時間: 10秒(処理時間より長く)
- 「msg.delayを用いて遅延時間を上書き」はオフのままにする。Delayノードを通ったメッセージには
msg.delay が残るため、オンにすると待機時間がその値(1秒処理なら1000ミリ秒)に置き換わり、後から届くタイムアウトを止められない
✅ 解答例フロー
[
{
"id": "trigger_node_guide_tab_4",
"type": "tab",
"label": "trigger_node_guide 例5",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ex4_inject_1s",
"type": "inject",
"z": "trigger_node_guide_tab_4",
"name": "1秒処理",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 160,
"y": 80,
"wires": [
[
"ex4_trigger_timeout"
]
]
},
{
"id": "ex4_inject_5s",
"type": "inject",
"z": "trigger_node_guide_tab_4",
"name": "5秒処理(タイムアウト)",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "5",
"payloadType": "num",
"x": 200,
"y": 140,
"wires": [
[
"ex4_trigger_timeout"
]
]
},
{
"id": "ex4_trigger_timeout",
"type": "trigger",
"z": "trigger_node_guide_tab_4",
"name": "3秒タイムアウト",
"op1": "",
"op2": "timeout",
"op1type": "pay",
"op2type": "str",
"duration": "3",
"extend": "false",
"overrideDelay": false,
"units": "s",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 2,
"x": 400,
"y": 100,
"wires": [
[
"ex4_change_work"
],
[
"ex4_change"
]
]
},
{
"id": "ex4_change_work",
"type": "change",
"z": "trigger_node_guide_tab_4",
"name": "処理時間と結果を設定",
"rules": [
{
"t": "set",
"p": "delay",
"pt": "msg",
"to": "payload * 1000",
"tot": "jsonata"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "\"done: \" & payload & \"s\"",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 540,
"y": 60,
"wires": [
[
"ex4_delay_work"
]
]
},
{
"id": "ex4_delay_work",
"type": "delay",
"z": "trigger_node_guide_tab_4",
"name": "処理にかかる時間",
"pauseType": "delayv",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 740,
"y": 60,
"wires": [
[
"ex4_trigger_dedup"
]
]
},
{
"id": "ex4_change",
"type": "change",
"z": "trigger_node_guide_tab_4",
"name": "タイムアウト設定",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "timeout",
"tot": "str"
}
],
"x": 600,
"y": 140,
"wires": [
[
"ex4_trigger_dedup"
]
]
},
{
"id": "ex4_trigger_dedup",
"type": "trigger",
"z": "trigger_node_guide_tab_4",
"name": "重複排除",
"op1": "",
"op2": "",
"op1type": "pay",
"op2type": "nul",
"duration": "10",
"extend": "false",
"overrideDelay": false,
"units": "s",
"reset": "",
"bytopic": "topic",
"topic": "_msgid",
"outputs": 1,
"x": 780,
"y": 100,
"wires": [
[
"ex4_debug"
]
]
},
{
"id": "ex4_debug",
"type": "debug",
"z": "trigger_node_guide_tab_4",
"name": "結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 930,
"y": 100,
"wires": []
}
]
MQTTでデバイスからハートビート受信
↓
Triggerノード(延長モード)
- 1つ目: 何も送らない
- 2つ目: "offline"
- 待機時間: 30秒
- 延長: 有効
↓
30秒間ハートビートなし
↓
"offline"出力 → アラート送信
※ デバイスの生存監視
ボタン押下 or センサーイベント
↓
Triggerノード
- 1つ目: 1
- 2つ目: 0
- 待機時間: 2秒
↓
GPIO出力ノード
↓
LEDが2秒間点灯して消灯
※ Raspberry PiのLED制御
ユーザーアクティビティ(API呼び出し等)
↓
Triggerノード(延長モード)
- 1つ目: 何も送らない
- 2つ目: "session_timeout"
- 待機時間: 15分
- 延長: 有効
↓
15分間アクティビティなし
↓
セッション無効化処理
※ Webアプリケーションのセッション管理
API呼び出し要求
↓
Triggerノード(2出力)
- 出力1: 即座にAPIへ
- 出力2: 5秒後にタイムアウト
↓
├─ 出力1 → HTTP Requestノード → 成功処理
└─ 出力2 → リトライカウンター → 再試行
※ API呼び出しのタイムアウト処理