📋 Node-RED YAML ノード完全ガイド

📚 目次

📖 1. 概要

YAML ノードは、YAML文字列とJavaScriptオブジェクトを相互変換するノードです。YAML(YAML Ain't Markup Language)は人間が読み書きしやすい設定ファイル形式として広く使われています。

📝 レシピカードに例えると:YAMLは料理のレシピカードのような形式です。材料と手順が見やすく整理されていて、人間にも読みやすく、コンピュータにも処理できます。YAML ノードはこのレシピカード形式とプログラムで扱える形式を相互変換します。

設定ファイル読込 YAML 設定値使用

💡 YAMLの特徴:

⚙️ 2. 設定詳細

プロパティ

プロパティ説明デフォルト
Property変換対象のプロパティmsg.payload
名前ノードの表示名

💡 自動変換:

入力がYAML文字列の場合 → オブジェクトに変換
入力がオブジェクトの場合 → YAML文字列に変換

📝 3. YAML基本文法

基本的な書き方

YAML形式

# コメント name: 田中太郎 age: 30 active: true score: 85.5

対応するJSON

{ "name": "田中太郎", "age": 30, "active": true, "score": 85.5 }

ネストした構造

YAML形式

person: name: 山田花子 address: city: 東京 zip: "100-0001"

対応するJSON

{ "person": { "name": "山田花子", "address": { "city": "東京", "zip": "100-0001" } } }

配列の表現

YAML形式

fruits: - apple - banana - orange users: - name: 田中 role: admin - name: 鈴木 role: user

対応するJSON

{ "fruits": ["apple", "banana", "orange"], "users": [ {"name": "田中", "role": "admin"}, {"name": "鈴木", "role": "user"} ] }

データ型

YAML例JavaScript値
文字列name: Hello"Hello"
数値count: 4242
浮動小数点pi: 3.143.14
真偽値active: truetrue
nullvalue: null または value: ~null
日付date: 2024-01-15Date オブジェクト

🔧 4. 実用的な使用パターン

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

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

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

📥 サンプルフローJSON(クリックで展開)
[ { "id": "yaml_sample_tab", "type": "tab", "label": "YAML サンプル", "disabled": false, "info": "" }, { "id": "yaml_comment1", "type": "comment", "z": "yaml_sample_tab", "name": "━━━ YAML → オブジェクト変換 ━━━", "info": "", "x": 180, "y": 40, "wires": [] }, { "id": "yaml_inject1", "type": "inject", "z": "yaml_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["yaml_template1"]] }, { "id": "yaml_template1", "type": "template", "z": "yaml_sample_tab", "name": "YAML設定", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "# アプリケーション設定\napp:\n name: My Application\n version: 1.0.0\n debug: true\n\n# データベース設定\ndatabase:\n host: localhost\n port: 5432\n name: mydb\n\n# 機能フラグ\nfeatures:\n - auth\n - logging\n - cache", "output": "str", "x": 280, "y": 100, "wires": [["yaml_parse"]] }, { "id": "yaml_parse", "type": "yaml", "z": "yaml_sample_tab", "property": "payload", "name": "YAML→Obj", "x": 430, "y": 100, "wires": [["yaml_debug1"]] }, { "id": "yaml_debug1", "type": "debug", "z": "yaml_sample_tab", "name": "オブジェクト出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 600, "y": 100, "wires": [] }, { "id": "yaml_comment2", "type": "comment", "z": "yaml_sample_tab", "name": "━━━ オブジェクト → YAML変換 ━━━", "info": "", "x": 180, "y": 180, "wires": [] }, { "id": "yaml_inject2", "type": "inject", "z": "yaml_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 240, "wires": [["yaml_func1"]] }, { "id": "yaml_func1", "type": "function", "z": "yaml_sample_tab", "name": "Docker Compose構造", "func": "msg.payload = {\n \"version\": \"3.8\",\n \"services\": {\n \"web\": {\n \"image\": \"nginx:latest\",\n \"ports\": [\"80:80\"],\n \"volumes\": [\"./html:/usr/share/nginx/html\"]\n },\n \"db\": {\n \"image\": \"postgres:14\",\n \"environment\": {\n \"POSTGRES_DB\": \"mydb\",\n \"POSTGRES_USER\": \"user\",\n \"POSTGRES_PASSWORD\": \"password\"\n }\n }\n }\n};\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 320, "y": 240, "wires": [["yaml_build"]] }, { "id": "yaml_build", "type": "yaml", "z": "yaml_sample_tab", "property": "payload", "name": "Obj→YAML", "x": 490, "y": 240, "wires": [["yaml_debug2"]] }, { "id": "yaml_debug2", "type": "debug", "z": "yaml_sample_tab", "name": "docker-compose.yml", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 670, "y": 240, "wires": [] }, { "id": "yaml_comment3", "type": "comment", "z": "yaml_sample_tab", "name": "━━━ 設定値の抽出 ━━━", "info": "", "x": 160, "y": 320, "wires": [] }, { "id": "yaml_inject3", "type": "inject", "z": "yaml_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 380, "wires": [["yaml_template2"]] }, { "id": "yaml_template2", "type": "template", "z": "yaml_sample_tab", "name": "センサー設定YAML", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "sensors:\n temperature:\n pin: 4\n interval: 5000\n threshold:\n min: 10\n max: 40\n humidity:\n pin: 5\n interval: 10000\n threshold:\n min: 30\n max: 80", "output": "str", "x": 310, "y": 380, "wires": [["yaml_parse2"]] }, { "id": "yaml_parse2", "type": "yaml", "z": "yaml_sample_tab", "property": "payload", "name": "YAML→Obj", "x": 470, "y": 380, "wires": [["yaml_func2"]] }, { "id": "yaml_func2", "type": "function", "z": "yaml_sample_tab", "name": "温度センサー設定抽出", "func": "var tempConfig = msg.payload.sensors.temperature;\nmsg.payload = {\n sensor: \"temperature\",\n gpioPin: tempConfig.pin,\n readInterval: tempConfig.interval,\n alertMin: tempConfig.threshold.min,\n alertMax: tempConfig.threshold.max\n};\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 660, "y": 380, "wires": [["yaml_debug3"]] }, { "id": "yaml_debug3", "type": "debug", "z": "yaml_sample_tab", "name": "温度設定", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.readInterval", "statusType": "msg", "x": 860, "y": 380, "wires": [] } ]

使用パターン

パターン1: YAML → オブジェクト変換

サンプルフローの「YAML → オブジェクト変換」を参照してください。

YAML文字列 YAML→Obj オブジェクト

ポイント: 設定ファイルの読み込みや外部YAMLデータの解析に使用

パターン2: オブジェクト → YAML変換

サンプルフローの「オブジェクト → YAML変換」を参照してください。

オブジェクト生成 Obj→YAML ファイル保存

ポイント: 設定ファイルの生成やYAML形式での出力に使用

パターン3: 設定値の抽出

サンプルフローの「設定値の抽出」を参照してください。

センサー設定YAML YAML→Obj 温度センサー設定抽出 設定値

ポイント:

📝 5. 演習問題

演習1: 基本的なYAML変換 初級

📋 課題: シンプルなYAML文字列をオブジェクトに変換してください。

✅ 成功の条件:

💡 ヒント

Template ノードでYAML文字列を作成し、YAML ノードで変換します。

✅ 解答例フロー
[ {"id": "ex1_tab", "type": "tab", "label": "演習1"}, {"id": "ex1_inject", "type": "inject", "z": "ex1_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex1_template"]]}, {"id": "ex1_template", "type": "template", "z": "ex1_tab", "name": "YAML", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "name: Node-RED\nversion: 3.0\nenabled: true", "output": "str", "x": 270, "y": 100, "wires": [["ex1_yaml"]]}, {"id": "ex1_yaml", "type": "yaml", "z": "ex1_tab", "property": "payload", "name": "YAML→Obj", "x": 410, "y": 100, "wires": [["ex1_debug"]]}, {"id": "ex1_debug", "type": "debug", "z": "ex1_tab", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 550, "y": 100, "wires": []} ]

演習2: オブジェクトからYAML生成 初級

📋 課題: JavaScriptオブジェクトからYAML文字列を生成してください。

✅ 成功の条件:

💡 ヒント

Change ノードの「値の代入」で、型を「JSON」に設定してオブジェクトを作成し、YAML ノードで変換します。

✅ 解答例フロー
[ {"id": "ex2_tab", "type": "tab", "label": "演習2"}, {"id": "ex2_inject", "type": "inject", "z": "ex2_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex2_change"]]}, {"id": "ex2_change", "type": "change", "z": "ex2_tab", "name": "オブジェクト", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\"server\":{\"host\":\"localhost\",\"port\":8080},\"logging\":true}", "tot": "json"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 280, "y": 100, "wires": [["ex2_yaml"]]}, {"id": "ex2_yaml", "type": "yaml", "z": "ex2_tab", "property": "payload", "name": "Obj→YAML", "x": 430, "y": 100, "wires": [["ex2_debug"]]}, {"id": "ex2_debug", "type": "debug", "z": "ex2_tab", "name": "YAML出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 580, "y": 100, "wires": []} ]

演習3: ネストした設定の取得 中級

📋 課題: ネストしたYAML設定から特定の値を抽出してください。

✅ 成功の条件:

💡 ヒント

Change ノードの「値の代入」で、型を「JSONata」に設定し、payload.database.primary のようにネストしたプロパティにアクセスします。JSONata式で文字列連結には & 演算子を使います。

✅ 解答例フロー
[ {"id": "ex3_tab", "type": "tab", "label": "演習3"}, {"id": "ex3_inject", "type": "inject", "z": "ex3_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex3_template"]]}, {"id": "ex3_template", "type": "template", "z": "ex3_tab", "name": "設定YAML", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "database:\n primary:\n host: db1.example.com\n port: 5432\n replica:\n host: db2.example.com\n port: 5432", "output": "str", "x": 280, "y": 100, "wires": [["ex3_yaml"]]}, {"id": "ex3_yaml", "type": "yaml", "z": "ex3_tab", "property": "payload", "name": "YAML→Obj", "x": 430, "y": 100, "wires": [["ex3_change"]]}, {"id": "ex3_change", "type": "change", "z": "ex3_tab", "name": "Primary DB取得", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\"host\": payload.database.primary.host, \"port\": payload.database.primary.port, \"connectionString\": payload.database.primary.host & \":\" & $string(payload.database.primary.port)}", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 600, "y": 100, "wires": [["ex3_debug"]]}, {"id": "ex3_debug", "type": "debug", "z": "ex3_tab", "name": "DB設定", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.connectionString", "statusType": "msg", "x": 770, "y": 100, "wires": []} ]

演習4: 配列を含むYAML 中級

📋 課題: 配列を含むYAMLを解析し、配列の要素数と内容を取得してください。

✅ 成功の条件:

💡 ヒント

Change ノードの「値の代入」で、型を「JSONata」に設定します。配列の要素数は $count() 関数、フィルタリングは [role="admin"] のようなフィルタ式、名前の抽出は .name で取得できます。

✅ 解答例フロー
[ {"id": "ex4_tab", "type": "tab", "label": "演習4"}, {"id": "ex4_inject", "type": "inject", "z": "ex4_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex4_template"]]}, {"id": "ex4_template", "type": "template", "z": "ex4_tab", "name": "ユーザーYAML", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "users:\n - name: 田中\n role: admin\n - name: 鈴木\n role: editor\n - name: 佐藤\n role: viewer", "output": "str", "x": 290, "y": 100, "wires": [["ex4_yaml"]]}, {"id": "ex4_yaml", "type": "yaml", "z": "ex4_tab", "property": "payload", "name": "YAML→Obj", "x": 450, "y": 100, "wires": [["ex4_change"]]}, {"id": "ex4_change", "type": "change", "z": "ex4_tab", "name": "ユーザー情報", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\"totalUsers\": $count(payload.users), \"admins\": payload.users[role=\"admin\"].name, \"allNames\": payload.users.name}", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 610, "y": 100, "wires": [["ex4_debug"]]}, {"id": "ex4_debug", "type": "debug", "z": "ex4_tab", "name": "ユーザー統計", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.totalUsers", "statusType": "msg", "x": 790, "y": 100, "wires": []} ]

✅ 6. まとめ

🎯 重要ポイント:

⚠️ 注意事項:

🏭 7. 実務活用例

ケース1: アプリケーション設定管理

config.yaml ファイルからアプリケーション設定を読み込んでフローで使用。

ケース2: Kubernetes マニフェスト生成

パラメータからKubernetes Deployment/Service のYAMLを動的生成。

ケース3: CI/CD パイプライン設定

GitHub Actions や GitLab CI の設定ファイルを生成・更新。

ケース4: IoTデバイス設定

センサーやアクチュエータの設定をYAMLファイルで管理。

📚 Node-RED 公式ドキュメント

🏠