💉 Node-RED Injectノード ガイド

このガイドでは、Node-REDのInjectノードについて、初心者の方でも理解できるように詳しく説明します。

📚 1. Injectノードとは?

🤔 「Inject」って何?

Injectノードは、フローにメッセージを送り込む起点となるノードです。 日常生活で例えると、スタートボタンや目覚まし時計のようなものです。

⏰ 目覚まし時計に例えると:

📦 基本的な動作

Inject 処理 Debug

Injectノードは、Node-REDフローの出発点として使用されます。 ボタンをクリックするか、設定したスケジュールに従って自動的にメッセージを生成し、 後続のノードに送信します。

Injectノードの操作方法:

timestamp

↑ ノード左側のボタンをクリックすると実行

⚙️ 2. Injectノードの4つのトリガー方式

手動 クリック実行

ノード左のボタンをクリックして実行

テスト時やデバッグに最適 好きなタイミングで実行可能

繰り返し 定期実行

指定した間隔で自動実行

例: 5秒ごと、1分ごと センサーポーリングに最適

スケジュール 時刻指定

cron式で特定の時刻に実行

例: 毎日9時、平日16時 定期レポートに最適

起動時 自動実行

Node-RED起動時に自動実行

初期化処理に最適 コンテキスト変数の設定など

📋 設定項目一覧

設定項目 説明 備考
msg.payload 送信するデータの内容 様々な型を選択可能
msg.topic メッセージのトピック(分類用) 任意の文字列
追加プロパティ msg.payloadとtopic以外のプロパティ 複数追加可能
繰り返し 定期実行の間隔 秒/分/時間で指定
指定した時刻 cron式でスケジュール指定 曜日・時刻を細かく設定
Node-RED起動時にも実行 デプロイ時・再起動時に自動実行 遅延時間も設定可能
名前 ノードの表示名 用途がわかる名前を推奨

🎯 送信できるデータ型

Injectノードでは、様々な型のデータを送信できます:

タイムスタンプ 文字列 数値 真偽値 JSON 環境変数 フロー/グローバル
データ型 説明
タイムスタンプ 現在時刻(ミリ秒) 1702700000000
文字列 テキストデータ "Hello, World!"
数値 整数・小数 42, 3.14
真偽値 true または false true
JSON オブジェクト・配列 {"temp": 25, "humidity": 60}
環境変数 環境変数の値 env.MY_API_KEY
フロー変数 フローコンテキストの値 flow.counter

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

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

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

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

📋 サンプルフロー(クリックで展開)

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

[ { "id": "4b6969e842efe7d9", "type": "tab", "label": "inject", "disabled": false, "info": "", "env": [] }, { "id": "eca30de2.7f1b1", "type": "comment", "z": "4b6969e842efe7d9", "name": "Trigger a flow whenever Node-RED starts", "info": "Inject node can be used to trigger a flow whenever Node-RED starts.", "x": 260, "y": 80, "wires": [] }, { "id": "8189f250.ddfb2", "type": "inject", "z": "4b6969e842efe7d9", "name": "起動時実行", "props": [{"p": "payload"}, {"p": "topic", "vt": "str"}], "repeat": "", "crontab": "", "once": true, "onceDelay": 0.1, "topic": "", "payload": "Started!", "payloadType": "str", "x": 250, "y": 140, "wires": [["9a0150e8.6d0b3"]] }, { "id": "9a0150e8.6d0b3", "type": "debug", "z": "4b6969e842efe7d9", "name": "起動確認", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 500, "y": 140, "wires": [] }, { "id": "64c1d7b3.a65b38", "type": "comment", "z": "4b6969e842efe7d9", "name": "Trigger a flow at regular intervals", "info": "Inject node can be used to trigger a flow at regular intervals.", "x": 230, "y": 240, "wires": [] }, { "id": "7c820e88.2db33", "type": "inject", "z": "4b6969e842efe7d9", "name": "5秒ごと", "props": [{"p": "payload"}, {"p": "topic", "vt": "str"}], "repeat": "5", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 240, "y": 300, "wires": [["8f135212.6a9a9"]] }, { "id": "8f135212.6a9a9", "type": "debug", "z": "4b6969e842efe7d9", "name": "定期実行", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 500, "y": 300, "wires": [] }, { "id": "a4060d5d.a3c92", "type": "comment", "z": "4b6969e842efe7d9", "name": "Trigger a flow at a specific time", "info": "Inject node can be used to trigger a flow at a specific time.", "x": 230, "y": 420, "wires": [] }, { "id": "468db06c.bafdd", "type": "inject", "z": "4b6969e842efe7d9", "name": "平日16時", "props": [{"p": "payload"}, {"p": "topic", "vt": "str"}], "repeat": "", "crontab": "00 16 * * 1,2,3,4,5", "once": false, "onceDelay": 0.1, "topic": "", "payload": "It is 4pm on a weekday!", "payloadType": "str", "x": 250, "y": 480, "wires": [["1ccc3174.cb926f"]] }, { "id": "1ccc3174.cb926f", "type": "debug", "z": "4b6969e842efe7d9", "name": "スケジュール", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 510, "y": 480, "wires": [] } ]

パターン1: 起動時に自動実行

用途: Node-RED起動時に初期化処理を実行

Inject
(起動時)
Debug

📌 起動時実行の使い道:

設定例:

msg.payload: "Started!" (文字列) 繰り返し: なし Node-RED起動時にも実行: ✓ 遅延時間: 0.1秒(デフォルト) 動作: Node-RED起動時(デプロイ時含む)に "Started!" というメッセージを送信

パターン2: 定期実行(インターバル)

用途: 一定間隔でメッセージを送信

Inject
(5秒ごと)
Debug

⏱️ 5秒間隔の定期実行:

時間軸
1
2
3
4
5

0秒 → 5秒 → 10秒 → 15秒 → 20秒...

設定例:

msg.payload: タイムスタンプ 繰り返し: 5秒ごと 動作: 5秒ごとに現在のタイムスタンプ(ミリ秒)を送信 センサーのポーリングやヘルスチェックに最適

パターン3: スケジュール実行(cron式)

用途: 特定の時刻・曜日に実行

Inject
(平日16時)
Debug

📌 cron式の書式:

分 時 日 月 曜日 00 16 * * 1,2,3,4,5 00 = 0分(00分ちょうど) 16 = 16時 * = 毎日 * = 毎月 1,2,3,4,5 = 月〜金曜日

設定例:

msg.payload: "It is 4pm on a weekday!" (文字列) 指定した時刻: 16:00, 月〜金曜日 動作: 平日の16:00に自動実行 日報送信やバックアップ処理に最適

📅 cron式クイックリファレンス

スケジュール cron式 説明
毎分 * * * * * 1分ごとに実行
毎時0分 0 * * * * 1時間ごとに実行
毎日9時 0 9 * * * 毎朝9:00に実行
平日9時 0 9 * * 1-5 月〜金の9:00に実行
毎週月曜9時 0 9 * * 1 月曜日の9:00に実行
毎月1日0時 0 0 1 * * 月初の0:00に実行
毎日9時と18時 0 9,18 * * * 1日2回実行
5分ごと */5 * * * * 0, 5, 10, 15...分に実行

パターン4: JSONオブジェクトの送信

用途: 構造化されたデータを送信

Inject
(JSON)
処理 Debug

設定例:

msg.payload: {"sensor": "temp", "value": 25.5, "unit": "℃"} データ型: JSON 動作: オブジェクト形式のデータを送信 APIテストやデータ変換のテストに最適

パターン5: 複数プロパティの設定

用途: payload以外のプロパティも設定

Inject
(複数プロパティ)
MQTT Out

設定例:

msg.payload: 25.5 msg.topic: "sensors/temperature" msg.qos: 1 msg.retain: true 動作: MQTTパブリッシュに必要な全プロパティを 1つのInjectノードで設定

🏋️ 4. 実践演習

演習1: 手動トリガーの作成初級

📝 課題:

ボタンクリックで「こんにちは、Node-RED!」と表示するフローを作成してください。

🎯 要求仕様:

📊 期待される動作:

✅ 成功の条件:

💡 ヒント

Injectノードの設定:

  • msg.payload: 文字列型を選択
  • 値: "こんにちは、Node-RED!"
  • 繰り返し: なし

Debugノードの設定:

  • 出力: msg.payload
✅ 解答例フロー
[ { "id": "ex1_inject", "type": "inject", "name": "挨拶", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "こんにちは、Node-RED!", "payloadType": "str", "x": 150, "y": 100, "wires": [["ex1_debug"]] }, { "id": "ex1_debug", "type": "debug", "name": "表示", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 330, "y": 100, "wires": [] } ]

演習2: 定期実行の設定初級

📝 課題:

10秒ごとに現在時刻を表示するフローを作成してください。

🎯 要求仕様:

📊 期待される動作:

✅ 成功の条件:

💡 ヒント

Injectノードの設定:

  • msg.payload: タイムスタンプを選択
  • 繰り返し: 「一定の時間間隔」を選択
  • 間隔: 10秒
✅ 解答例フロー
[ { "id": "ex2_inject", "type": "inject", "name": "10秒ごと", "props": [{"p": "payload"}], "repeat": "10", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 160, "y": 100, "wires": [["ex2_debug"]] }, { "id": "ex2_debug", "type": "debug", "name": "時刻表示", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 350, "y": 100, "wires": [] } ]

演習3: JSONデータの送信中級

📝 課題:

センサーデータを模したJSONオブジェクトを送信するフローを作成してください。

🎯 要求仕様:

📊 期待される動作:

✅ 成功の条件:

💡 ヒント

Injectノードの設定:

  • msg.payload: JSON型を選択
  • 値: {"device": "sensor01", "temperature": 25.5, "humidity": 60}
  • msg.topic: 文字列 "sensors/room1"

Debugノードの設定:

  • 出力: msgオブジェクト全体(complete msg object)
✅ 解答例フロー
[ { "id": "ex3_inject", "type": "inject", "name": "センサーデータ", "props": [ {"p": "payload"}, {"p": "topic", "vt": "str"} ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "sensors/room1", "payload": "{\"device\":\"sensor01\",\"temperature\":25.5,\"humidity\":60}", "payloadType": "json", "x": 170, "y": 100, "wires": [["ex3_debug"]] }, { "id": "ex3_debug", "type": "debug", "name": "データ確認", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "x": 370, "y": 100, "wires": [] } ]

演習4: 複合トリガーの実装上級

📝 課題:

起動時と1分ごとに「ヘルスチェック」を実行し、現在時刻を人間が読める形式で表示するフローを作成してください。

🎯 要求仕様:

📊 期待される動作:

✅ 成功の条件:

💡 ヒント

Injectノードの設定:

  • msg.payload: タイムスタンプ
  • 繰り返し: 60秒
  • Node-RED起動時にも実行: ✓

Functionノードの設定:

  • new Date(msg.payload) でDateオブジェクト作成
  • toLocaleString() で日本語形式に変換
✅ 解答例フロー
[ { "id": "ex4_inject", "type": "inject", "name": "ヘルスチェック", "props": [{"p": "payload"}], "repeat": "60", "crontab": "", "once": true, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 170, "y": 100, "wires": [["ex4_function"]] }, { "id": "ex4_function", "type": "function", "name": "日時フォーマット", "func": "const date = new Date(msg.payload);\nconst options = {\n year: 'numeric',\n month: '2-digit',\n day: '2-digit',\n hour: '2-digit',\n minute: '2-digit',\n second: '2-digit',\n hour12: false\n};\nconst formatted = date.toLocaleString('ja-JP', options);\nmsg.payload = formatted + \" - ヘルスチェックOK\";\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 390, "y": 100, "wires": [["ex4_debug"]] }, { "id": "ex4_debug", "type": "debug", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "x": 570, "y": 100, "wires": [] } ]

🎓 5. まとめ

Injectノードの重要ポイント

⚠️ よくある間違い

📚 次のステップ

Injectノードをマスターしたら、以下のノードも学んでみましょう:

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

よくある問題と解決方法

問題 原因 解決方法
定期実行が動かない デプロイしていない デプロイボタンをクリック
起動時に実行されない チェックが入っていない 「Node-RED起動時にも実行」を有効化
cron式が動作しない 構文エラー スペース区切りで5つの値を確認
JSONがエラーになる 構文エラー ダブルクォート使用、カンマ確認
タイムスタンプが数字 仕様通り Functionで日時形式に変換
繰り返しが止まらない 仕様通り ノードを無効化またはフローを停止

💡 7. 実務での活用例

ケース1: システム起動通知

Node-RED起動 ↓ Injectノード(起動時実行) ↓ "システムが起動しました" + タイムスタンプ ↓ Slack/Teams/メールで通知 ※ サーバー再起動の監視に活用

ケース2: センサーポーリング

Injectノード(30秒ごと) ↓ HTTP Request(センサーAPIを呼び出し) ↓ データ加工 ↓ データベースに保存 / ダッシュボードに表示 ※ 定期的なデータ収集に活用

ケース3: 日次レポート

Injectノード(毎日18:00、cron: 0 18 * * *) ↓ データベースから集計 ↓ レポート生成 ↓ メール送信 ※ 自動レポート生成に活用

ケース4: IoTデバイスのテスト

Injectノード(JSONデータ) { "device_id": "sensor01", "temperature": 25.5, "humidity": 60 } ↓ MQTT Publish ↓ IoTデバイスをシミュレート ※ 実機なしでのテストに活用

📖 8. 詳細設定リファレンス

msg.payloadの型一覧

設定画面での表示 内部値(payloadType)
タイムスタンプ timestamp date
文字列 string str
数値 number num
真偽値 boolean bool
JSON JSON json
バッファ buffer bin
環境変数 env variable env
フロー変数 flow. flow
グローバル変数 global. global

cron式フィールド

位置 フィールド 値の範囲 特殊文字
1 0-59 * , -
2 0-23 * , -
3 1-31 * , -
4 1-12 * , -
5 曜日 0-6(日-土) * , -

🔗 9. 追加リソース


このガイドが役に立ちましたら、実際のプロジェクトで練習してみてください!
Injectノードは全てのフローの出発点です。

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

🏠