📋 サンプルフロー(クリックで展開)
参照元:NodeREDエディター内サンプルフロー
[
{
"id": "328b545581935c25",
"type": "tab",
"label": "template",
"disabled": false,
"info": "",
"env": []
},
{
"id": "eaf91a6b.a55da8",
"type": "comment",
"z": "328b545581935c25",
"name": "1. Advanced mustache example",
"info": "Template node can create a string value using Mustache syntax.",
"x": 230,
"y": 80,
"wires": []
},
{
"id": "61fbfe34.14a02",
"type": "inject",
"z": "328b545581935c25",
"name": "Price of fruits",
"props": [
{"p": "payload"},
{"p": "topic", "vt": "str"}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "Fruits",
"payload": "[{\"name\":\"apple\",\"price\":100},{\"name\":\"orange\",\"price\":80},{\"name\":\"banana\",\"price\":210}]",
"payloadType": "json",
"x": 230,
"y": 140,
"wires": [["bf0cb02.d8e4b5"]]
},
{
"id": "bf0cb02.d8e4b5",
"type": "template",
"z": "328b545581935c25",
"name": "価格リスト",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "# Price List of {{topic}}\n\n{{! outputs list of prices }}\n{{#payload}}\n- {{name}}: {{price}}円\n{{/payload}}",
"output": "str",
"x": 410,
"y": 140,
"wires": [["153eb0ff.5622df"]]
},
{
"id": "153eb0ff.5622df",
"type": "debug",
"z": "328b545581935c25",
"name": "文字列出力",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 590,
"y": 140,
"wires": []
},
{
"id": "fe821493.2e0e28",
"type": "comment",
"z": "328b545581935c25",
"name": "2. Parse result as JSON",
"info": "Template node can create JSON output.",
"x": 210,
"y": 240,
"wires": []
},
{
"id": "931f94e8.592cd8",
"type": "inject",
"z": "328b545581935c25",
"name": "Hello World",
"props": [
{"p": "payload"},
{"p": "topic", "vt": "str"}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "message",
"payload": "Hello, World!",
"payloadType": "str",
"x": 230,
"y": 300,
"wires": [["bb2b0dad.b24b5"]]
},
{
"id": "bb2b0dad.b24b5",
"type": "template",
"z": "328b545581935c25",
"name": "JSON template",
"field": "payload",
"fieldType": "msg",
"format": "json",
"syntax": "mustache",
"template": "{\n \"key\": \"{{topic}}\",\n \"value\": \"{{payload}}\"\n}",
"output": "json",
"x": 420,
"y": 300,
"wires": [["baf2e48.2b97418"]]
},
{
"id": "baf2e48.2b97418",
"type": "debug",
"z": "328b545581935c25",
"name": "JSON出力",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 590,
"y": 300,
"wires": []
},
{
"id": "6ad06659.a1e4e8",
"type": "comment",
"z": "328b545581935c25",
"name": "3. Parse result as YAML",
"info": "Template node can create YAML output.",
"x": 210,
"y": 400,
"wires": []
},
{
"id": "8d6be9a2.c3fa58",
"type": "inject",
"z": "328b545581935c25",
"name": "Hello World",
"props": [
{"p": "payload"},
{"p": "topic", "vt": "str"}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "message",
"payload": "Hello, World!",
"payloadType": "str",
"x": 230,
"y": 460,
"wires": [["69369c3.4a98164"]]
},
{
"id": "69369c3.4a98164",
"type": "template",
"z": "328b545581935c25",
"name": "YAML template",
"field": "payload",
"fieldType": "msg",
"format": "yaml",
"syntax": "mustache",
"template": "key: {{topic}}\nvalue: {{payload}}",
"output": "yaml",
"x": 420,
"y": 460,
"wires": [["11fb2934.f5de27"]]
},
{
"id": "11fb2934.f5de27",
"type": "debug",
"z": "328b545581935c25",
"name": "YAML出力",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 590,
"y": 460,
"wires": []
}
]
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
<p>現在時刻: {{timestamp}}</p>
<ul>
{{#items}}
<li>{{name}}: {{value}}</li>
{{/items}}
{{^items}}
<li>データがありません</li>
{{/items}}
</ul>
</body>
</html>
演習1: シンプルな挨拶文初級
📝 課題:
名前を受け取って「こんにちは、〇〇さん!」という挨拶文を生成してください。
💼 使いどころ: 受け取った名前から、決まった形式の挨拶文を組み立てる場面で使います。
🎯 要求仕様:
- Inject、Template、Debugノードを使用
- 入力: msg.payload = "田中"
- 出力: "こんにちは、田中さん!"
📊 期待される動作:
- Injectをクリック → デバッグに挨拶文が表示
💡 ヒント
Injectノードの設定:
Templateノードの設定:
- テンプレート:
こんにちは、{{payload}}さん!
- 出力形式: 文字列
✅ 解答例フロー
[
{
"id": "template_node_guide_tab_1",
"type": "tab",
"label": "template_node_guide 例2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ex1_inject",
"type": "inject",
"z": "template_node_guide_tab_1",
"name": "名前入力",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "田中",
"payloadType": "str",
"x": 160,
"y": 100,
"wires": [
[
"ex1_template"
]
]
},
{
"id": "ex1_template",
"type": "template",
"z": "template_node_guide_tab_1",
"name": "挨拶文",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "こんにちは、{{payload}}さん!",
"output": "str",
"x": 330,
"y": 100,
"wires": [
[
"ex1_debug"
]
]
},
{
"id": "ex1_debug",
"type": "debug",
"z": "template_node_guide_tab_1",
"name": "結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 490,
"y": 100,
"wires": []
}
]
演習2: 商品リストの生成中級
📝 課題:
商品の配列データから、番号付きリストを生成してください。
💼 使いどころ: 複数商品の名前と価格を、見出し付きの一覧テキストにまとめる場面で使います。
🎯 要求仕様:
- 入力: [{"name": "りんご", "price": 150}, {"name": "みかん", "price": 100}, {"name": "ぶどう", "price": 300}]
- 出力形式:
【商品リスト】
・りんご: 150円
・みかん: 100円
・ぶどう: 300円
💡 ヒント
Templateノードの設定:
{{#payload}} ... {{/payload}} で配列をループ
- ループ内で
{{name}} と {{price}} を参照
✅ 解答例フロー
[
{
"id": "template_node_guide_tab_2",
"type": "tab",
"label": "template_node_guide 例3",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ex2_inject",
"type": "inject",
"z": "template_node_guide_tab_2",
"name": "商品データ",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "[{\"name\":\"りんご\",\"price\":150},{\"name\":\"みかん\",\"price\":100},{\"name\":\"ぶどう\",\"price\":300}]",
"payloadType": "json",
"x": 170,
"y": 100,
"wires": [
[
"ex2_template"
]
]
},
{
"id": "ex2_template",
"type": "template",
"z": "template_node_guide_tab_2",
"name": "リスト生成",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "【商品リスト】\n{{#payload}}\n・{{name}}: {{price}}円\n{{/payload}}",
"output": "str",
"x": 350,
"y": 100,
"wires": [
[
"ex2_debug"
]
]
},
{
"id": "ex2_debug",
"type": "debug",
"z": "template_node_guide_tab_2",
"name": "結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"x": 510,
"y": 100,
"wires": []
}
]
演習3: センサーデータのJSON変換中級
📝 課題:
センサーから受け取ったデータをJSON形式に変換して出力してください。
💼 使いどころ: 測定した数値と機器名を、後続処理で扱えるひとつのオブジェクトにまとめる場面で使います。
🎯 要求仕様:
- 入力: msg.topic = "sensor01", msg.payload = 25.5
- 出力: {"device": "sensor01", "temperature": 25.5, "unit": "℃"}
- 出力形式: オブジェクト(Parsed JSON)
💡 ヒント
Templateノードの設定:
- 形式: JSON
- 出力形式: Parsed JSON
- テンプレートでJSONを構築
✅ 解答例フロー
[
{
"id": "template_node_guide_tab_3",
"type": "tab",
"label": "template_node_guide 例4",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ex3_inject",
"type": "inject",
"z": "template_node_guide_tab_3",
"name": "センサーデータ",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "sensor01",
"payload": "25.5",
"payloadType": "num",
"x": 180,
"y": 100,
"wires": [
[
"ex3_template"
]
]
},
{
"id": "ex3_template",
"type": "template",
"z": "template_node_guide_tab_3",
"name": "JSON変換",
"field": "payload",
"fieldType": "msg",
"format": "json",
"syntax": "mustache",
"template": "{\n \"device\": \"{{topic}}\",\n \"temperature\": {{payload}},\n \"unit\": \"℃\"\n}",
"output": "json",
"x": 360,
"y": 100,
"wires": [
[
"ex3_debug"
]
]
},
{
"id": "ex3_debug",
"type": "debug",
"z": "template_node_guide_tab_3",
"name": "結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 510,
"y": 100,
"wires": []
}
]
演習4: 条件付きHTMLレポート上級
📝 課題:
センサーデータの状態に応じて、異なる表示を行うHTMLレポートを生成してください。
💼 使いどころ: 状態や警告の有無によって表示内容を切り替える、HTML形式のレポートを組み立てる場面で使います。
🎯 要求仕様:
- 入力: {status: "active", temperature: 28, alerts: ["高温警告", "湿度注意"]}
- statusがactiveなら「🟢 稼働中」、そうでなければ「🔴 停止中」
- alertsがあれば警告リストを表示、なければ「異常なし」
- HTML形式で出力
💡 ヒント
条件分岐の使い方:
{{#status}}...{{/status}}: statusがtruthyの場合
{{^status}}...{{/status}}: statusがfalsyの場合
- 文字列 "active" はtruthyとして扱われる
- 空配列 [] はfalsyとして扱われる
statusの判定:
- Changeノードでstatusが"active"かどうかをブール値に変換すると判定しやすい
- または、statusの値をそのまま表示する方法でも可
✅ 解答例フロー
[
{
"id": "template_node_guide_tab_4",
"type": "tab",
"label": "template_node_guide 例5",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ex4_inject",
"type": "inject",
"z": "template_node_guide_tab_4",
"name": "センサー状態",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"status\":\"active\",\"temperature\":28,\"alerts\":[\"高温警告\",\"湿度注意\"]}",
"payloadType": "json",
"x": 180,
"y": 100,
"wires": [
[
"ex4_change"
]
]
},
{
"id": "ex4_change",
"type": "change",
"z": "template_node_guide_tab_4",
"name": "状態判定",
"rules": [
{
"t": "set",
"p": "isActive",
"pt": "msg",
"to": "$$.payload.status = \"active\"",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 360,
"y": 100,
"wires": [
[
"ex4_template"
]
]
},
{
"id": "ex4_template",
"type": "template",
"z": "template_node_guide_tab_4",
"name": "HTMLレポート",
"field": "payload",
"fieldType": "msg",
"format": "html",
"syntax": "mustache",
"template": "<!DOCTYPE html>\n<html>\n<head>\n <title>センサーレポート</title>\n <style>\n body { font-family: sans-serif; padding: 20px; }\n .status { font-size: 24px; margin: 20px 0; }\n .alert { color: red; }\n .ok { color: green; }\n </style>\n</head>\n<body>\n <h1>📊 センサーレポート</h1>\n \n <div class=\"status\">\n {{#isActive}}🟢 稼働中{{/isActive}}\n {{^isActive}}🔴 停止中{{/isActive}}\n </div>\n \n <p>温度: {{payload.temperature}}℃</p>\n \n <h2>アラート</h2>\n {{#payload.alerts.length}}\n <ul class=\"alert\">\n {{#payload.alerts}}\n <li>⚠️ {{.}}</li>\n {{/payload.alerts}}\n </ul>\n {{/payload.alerts.length}}\n {{^payload.alerts.length}}\n <p class=\"ok\">✅ 異常なし</p>\n {{/payload.alerts.length}}\n</body>\n</html>",
"output": "str",
"x": 540,
"y": 100,
"wires": [
[
"ex4_debug"
]
]
},
{
"id": "ex4_debug",
"type": "debug",
"z": "template_node_guide_tab_4",
"name": "HTML出力",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 710,
"y": 100,
"wires": []
}
]
センサーアラートデータ
↓
Templateノード
↓
件名: [{{severity}}] {{sensor_name}}のアラート
本文:
{{sensor_name}}で異常を検知しました。
日時: {{timestamp}}
値: {{value}} {{unit}}
閾値: {{threshold}} {{unit}}
{{#details}}
詳細: {{.}}
{{/details}}
※ アラートメールの動的生成
HTTP In(GET /api/status)
↓
データ取得処理
↓
Templateノード(Parsed JSON)
{
"status": "{{status}}",
"timestamp": "{{timestamp}}",
"data": {
"temperature": {{temp}},
"humidity": {{humid}}
}
}
↓
HTTP Response
※ 構造化されたAPIレスポンスの生成
複数センサーデータ
↓
Templateノード(HTML)
<div class="dashboard">
{{#sensors}}
<div class="sensor-card">
<h3>{{name}}</h3>
<p class="value">{{value}} {{unit}}</p>
<p class="status {{status}}">{{statusText}}</p>
</div>
{{/sensors}}
</div>
↓
HTTP Response
※ 動的なダッシュボードページの生成
設定パラメータ
↓
Templateノード(YAML出力)
server:
host: {{host}}
port: {{port}}
database:
connection: {{db_connection}}
pool_size: {{pool_size}}
logging:
level: {{log_level}}
{{#log_file}}
file: {{log_file}}
{{/log_file}}
↓
ファイル書き込み
※ 設定ファイルの動的生成