Node-RED Write File / Read File ノードガイド

☁️ クラウドOK 環境とは?
共有クラウドサーバーで学ぶ方へ: 共有サーバーではファイルシステムも全員で共有しています。書き込み先は自分専用のフォルダやファイル名にし、他の人と同じパス(例 /tmp/test.txt)への上書きは避けてください(→ 学習環境ガイド)。

目次

1. 概要

Node-RED には2種類のファイル操作ノードがあります:

Write File(書き込み)

Write File

役割: ファイルへの書き込みや追記、削除

用途:

  • ログファイルの作成
  • データの永続化
  • 設定ファイルの保存

Read File(読み込み)

Read File

役割: ファイルからの読み込み

用途:

  • 設定ファイルの読み込み
  • データファイルの処理
  • テンプレートの読み込み

ファイルキャビネットに例えると:Write File ノードは書類をキャビネットに入れる作業、Read File ノードはキャビネットから書類を取り出す作業です。

動作を図で見る

write file ノードは msg.payload をファイルに書き込み、read file ノードはファイルの中身を読み出して msg.payload に入れます。

payload: "25.3"write file同じ msglog.txt24.825.125.3末尾に 1 行追加(改行付き)
write file は、payload をファイルの末尾に 1 行ずつ書き足す
きっかけの msgread filelog.txt24.825.125.3出ていく msgpayload: "24.825.125.3"読む
read file は、ファイルの中身を payload に入れて送り出す

2. Write File(書き込み)ノード

プロパティ

設定項目説明既定値
ファイル名書き込み先のファイルパス。入力欄左の型選択で パス/msg/JSONata/env を切り替えられる(空)
動作ファイルに対する操作。下の「動作の種類」から選ぶ。「ファイルを削除」を選ぶと、下の3項目(改行・ディレクトリ・文字コード)は表示されなくなるファイルへ追記
メッセージの入力のたびに改行を追加メッセージごとに末尾へ改行を付けるか(チェックボックス)。動作が「ファイルを削除」のときは表示されないオン
ディレクトリが存在しない場合は作成書き込み先の親ディレクトリがなければ自動で作成するか(チェックボックス)。動作が「ファイルを削除」のときは表示されないオフ
文字コード書き込み時の文字エンコーディング。動作が「ファイルを削除」のときは表示されないデフォルト
名前エディター上のノード表示名(空)

「動作」の種類

選択肢説明用途
ファイルへ追記既存の内容を残し、ファイル末尾に書き足すログファイル、時系列データ
ファイルを上書き既存の内容を消してから書き込む設定ファイル、状態保存
ファイルを削除指定したファイルを削除する一時ファイルの削除

動的なファイル名

「ファイル名」の左側の種類を msg. にし、右側に filename と入力すると(msg.filename と表示されます)、メッセージごとに msg.filename でファイルパスを指定できます。ファイル名を空欄のままにすると、エラー(Invalid property expression: zero-length)になって書き込めません。

// Function ノードで動的にファイル名を設定 msg.filename = "/home/pi/logs/sensor_" + new Date().toISOString().slice(0,10) + ".log"; msg.payload = "センサーデータ: " + msg.payload; return msg;

3. Read File(読み込み)ノード

プロパティ

設定項目説明既定値
ファイル名読み込むファイルパス。入力欄左の型選択で パス/msg/JSONata/env を切り替えられる(空)
出力形式ファイル内容をどの形で出力するか。下の「出力形式の種類」から選ぶ文字列
各メッセージに既存の全プロパティを含める出力形式が「行毎のメッセージ」「バッファのストリーム」のとき表示。分割した各メッセージに、入力メッセージの他のプロパティも引き継ぐか(チェックボックス)オフ
文字コード読み込み時の文字エンコーディング(出力形式が「文字列」「行毎のメッセージ」のときだけ表示される)デフォルト
名前エディター上のノード表示名(空)

「出力形式」の種類

選択肢説明用途
文字列ファイル全体を1つの文字列で出力する設定ファイル、テンプレート
行毎のメッセージ1行ごとに分割し、複数のメッセージで出力するCSVの行処理、ログ解析
バイナリバッファファイル全体を Buffer オブジェクトで出力するバイナリファイル、画像
バッファのストリームバッファのストリームとして出力する大きなファイルの処理

ポイント:

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

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

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

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

📌 公式サンプルについて: Node-RED本体には、write file と read file の公式サンプルが同梱されています。エディタの メニュー → 読み込み → サンプル → node-red → storage → write file(または read file)から開けます。このガイドでは、次の理由で、公式サンプルではなくサイト独自のサンプルフローを最初に載せています。

下のサンプルフローは、「ディレクトリが存在しない場合は作成」をオンにしてあるので、Windows でも書き込めます(/tmp は、Node-RED を起動したドライブの直下の tmp フォルダとして作られます)。

📥 サンプルフローJSON(クリックで展開)
[ { "id": "file_sample_tab", "type": "tab", "label": "File サンプル", "disabled": false, "info": "" }, { "id": "file_comment1", "type": "comment", "z": "file_sample_tab", "name": "━━━ ファイルへの書き込み(追記) ━━━", "info": "", "x": 200, "y": 40, "wires": [] }, { "id": "file_inject1", "type": "inject", "z": "file_sample_tab", "name": "ログ追記", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "センサー値: 25.5℃", "payloadType": "str", "x": 140, "y": 100, "wires": [["file_func_timestamp"]] }, { "id": "file_func_timestamp", "type": "function", "z": "file_sample_tab", "name": "タイムスタンプ追加", "func": "const now = new Date().toISOString();\nmsg.payload = now + \" - \" + msg.payload;\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 330, "y": 100, "wires": [["file_write_append"]] }, { "id": "file_write_append", "type": "file", "z": "file_sample_tab", "name": "ログ追記", "filename": "/tmp/nodered_sample.log", "filenameType": "str", "appendNewline": true, "createDir": true, "overwriteFile": "false", "encoding": "utf8", "x": 520, "y": 100, "wires": [["file_debug1"]] }, { "id": "file_debug1", "type": "debug", "z": "file_sample_tab", "name": "書き込み完了", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "\"書き込み完了\"", "statusType": "jsonata", "x": 690, "y": 100, "wires": [] }, { "id": "file_comment2", "type": "comment", "z": "file_sample_tab", "name": "━━━ ファイルの読み込み ━━━", "info": "", "x": 180, "y": 180, "wires": [] }, { "id": "file_inject2", "type": "inject", "z": "file_sample_tab", "name": "ファイル読み込み", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 160, "y": 240, "wires": [["file_read"]] }, { "id": "file_read", "type": "file in", "z": "file_sample_tab", "name": "ログ読み込み", "filename": "/tmp/nodered_sample.log", "filenameType": "str", "format": "utf8", "chunk": false, "sendError": false, "encoding": "utf8", "allProps": false, "x": 350, "y": 240, "wires": [["file_debug2"]] }, { "id": "file_debug2", "type": "debug", "z": "file_sample_tab", "name": "ファイル内容", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 530, "y": 240, "wires": [] }, { "id": "file_comment3", "type": "comment", "z": "file_sample_tab", "name": "━━━ 上書き保存 ━━━", "info": "", "x": 160, "y": 320, "wires": [] }, { "id": "file_inject3", "type": "inject", "z": "file_sample_tab", "name": "設定を保存", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 150, "y": 380, "wires": [["file_func_config"]] }, { "id": "file_func_config", "type": "function", "z": "file_sample_tab", "name": "設定オブジェクト", "func": "msg.payload = JSON.stringify({\n \"appName\": \"My IoT App\",\n \"version\": \"1.0.0\",\n \"settings\": {\n \"interval\": 5000,\n \"threshold\": 30\n },\n \"updatedAt\": new Date().toISOString()\n}, null, 2);\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 330, "y": 380, "wires": [["file_write_overwrite"]] }, { "id": "file_write_overwrite", "type": "file", "z": "file_sample_tab", "name": "設定ファイル保存", "filename": "/tmp/nodered_config.json", "filenameType": "str", "appendNewline": false, "createDir": true, "overwriteFile": "true", "encoding": "utf8", "x": 530, "y": 380, "wires": [["file_debug3"]] }, { "id": "file_debug3", "type": "debug", "z": "file_sample_tab", "name": "保存完了", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "\"設定保存完了\"", "statusType": "jsonata", "x": 700, "y": 380, "wires": [] }, { "id": "file_comment4", "type": "comment", "z": "file_sample_tab", "name": "━━━ 動的ファイル名(日付別ログ) ━━━", "info": "", "x": 210, "y": 460, "wires": [] }, { "id": "file_inject4", "type": "inject", "z": "file_sample_tab", "name": "イベント発生", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "ボタンが押されました", "payloadType": "str", "x": 150, "y": 520, "wires": [["file_func_dynamic"]] }, { "id": "file_func_dynamic", "type": "function", "z": "file_sample_tab", "name": "日付別ファイル名", "func": "const today = new Date().toISOString().slice(0, 10);\nmsg.filename = \"/tmp/events_\" + today + \".log\";\n\nconst timestamp = new Date().toISOString();\nmsg.payload = timestamp + \" | \" + msg.payload;\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 330, "y": 520, "wires": [["file_write_dynamic"]] }, { "id": "file_write_dynamic", "type": "file", "z": "file_sample_tab", "name": "動的ファイル", "filename": "filename", "filenameType": "msg", "appendNewline": true, "createDir": true, "overwriteFile": "false", "encoding": "utf8", "x": 520, "y": 520, "wires": [["file_debug4"]] }, { "id": "file_debug4", "type": "debug", "z": "file_sample_tab", "name": "ファイル名確認", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "filename", "targetType": "msg", "statusVal": "filename", "statusType": "msg", "x": 700, "y": 520, "wires": [] }, { "id": "file_comment5", "type": "comment", "z": "file_sample_tab", "name": "━━━ エラーハンドリング ━━━", "info": "", "x": 170, "y": 600, "wires": [] }, { "id": "file_inject5", "type": "inject", "z": "file_sample_tab", "name": "存在しないファイル", "props": [{"p": "filename", "v": "/nonexistent/path/file.txt", "vt": "str"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 170, "y": 660, "wires": [["file_read_error"]] }, { "id": "file_read_error", "type": "file in", "z": "file_sample_tab", "name": "読み込み試行", "filename": "filename", "filenameType": "msg", "format": "utf8", "chunk": false, "sendError": false, "encoding": "utf8", "allProps": false, "x": 360, "y": 660, "wires": [["file_debug5"]] }, { "id": "file_debug5", "type": "debug", "z": "file_sample_tab", "name": "読み込み結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "x": 540, "y": 660, "wires": [] }, { "id": "file_catch", "type": "catch", "z": "file_sample_tab", "name": "ファイルエラー捕捉", "scope": ["file_read_error"], "uncaught": false, "x": 350, "y": 720, "wires": [["file_debug_error"]] }, { "id": "file_debug_error", "type": "debug", "z": "file_sample_tab", "name": "エラー内容", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "error.message", "targetType": "msg", "statusVal": "error.message", "statusType": "msg", "x": 530, "y": 720, "wires": [] } ]

フローの読み解き

フローは、5つの部分に分かれています。各部分の先頭にある comment ノードの名前が、下の表の「部分」と、その下のパターン1〜5に当たります。

部分 ノードの並び 設定と、その理由 ボタンを押すと出るもの
ファイルへの書き込み(追記) 「ログ追記」(inject)→「タイムスタンプ追加」(function)→「ログ追記」(write file)→「書き込み完了」(debug) write file の「ファイル名」は /tmp/nodered_sample.log、「動作」は「ファイルへ追記」、「メッセージの入力のたびに改行を追加」と「ディレクトリが存在しない場合は作成」がオン。function が、行の先頭に時刻を付ける 「書き込み完了」に、書き込んだ1行(2026-11-25T15:30:48.468Z - センサー値: 25.5℃ の形)が出る。write file は、書き込んだあと、受け取った msg をそのまま次へ送る。押すたびに、ファイルに1行ずつ増える
ファイルの読み込み 「ファイル読み込み」(inject)→「ログ読み込み」(read file)→「ファイル内容」(debug) read file の「ファイル名」は、上の部分と同じ /tmp/nodered_sample.log。「出力形式」は「文字列」で、ファイル全体を1つの文字列で出す 「ファイル内容」に、それまでに追記した行がすべて、改行でつながった1つの文字列で出る。上の「ログ追記」を1回も押していないと、ファイルが無いのでエラーになる
上書き保存 「設定を保存」(inject)→「設定オブジェクト」(function)→「設定ファイル保存」(write file)→「保存完了」(debug) write file の「ファイル名」は /tmp/nodered_config.json、「動作」は「ファイルを上書き」、改行の追加はオフ。function が、設定のオブジェクトを、字下げ付きの JSON の文字列にする(JSON.stringify の3つ目の引数の 2 が、字下げの幅) 「保存完了」に、保存した JSON の文字列が出る。押すたびにファイルが作り直され、updatedAt(保存した時刻)だけが変わる。追記と違い、行は増えない
動的ファイル名(日付別ログ) 「イベント発生」(inject)→「日付別ファイル名」(function)→「動的ファイル」(write file)→「ファイル名確認」(debug) write file の「ファイル名」は msg.filename。function が、msg.filename に /tmp/events_2026-11-25.log のような日付入りの名前を入れる。日付が変わると、書き込み先のファイルが自動で変わる 「ファイル名確認」に、書き込んだファイルの名前が出る(この debug は msg.filename を表示する設定)。ファイルには 時刻 | ボタンが押されました の1行が追記される
エラーハンドリング 「存在しないファイル」(inject)→「読み込み試行」(read file)→「読み込み結果」(debug)。別の行に「ファイルエラー捕捉」(catch)→「エラー内容」(debug) inject が、msg.filename に、存在しないファイル(/nonexistent/path/file.txt)を入れて送る。catch の対象は「読み込み試行」だけ。「エラー内容」は msg.error.message を表示する設定 読み込みは失敗し、「読み込み結果」には何も出ない。「エラー内容」に Error: ENOENT: no such file or directory, open '/nonexistent/path/file.txt' が出る。ENOENT は「ファイルやフォルダが無い」を表す

パターン1: ファイルへの書き込み(追記)

サンプルフローの「ファイルへの書き込み(追記)」を参照してください。

メッセージ → 追記 → 完了

用途: タイムスタンプを付けたデータをログファイルへ追記

設定: 動作: ファイルへ追記、メッセージの入力のたびに改行を追加: ✓

パターン2: ファイルの読み込み

サンプルフローの「ファイルの読み込み」を参照してください。

読み込み → Read File → 内容表示

用途: 保存済みログファイルの内容をまとめて読み込み

設定: 出力形式: 文字列

パターン3: 上書き保存

サンプルフローの「上書き保存」を参照してください。

新データ → 上書き → 保存完了

用途: 設定用オブジェクトをJSONファイルとして上書き保存

設定: 動作: ファイルを上書き(既存ファイルを置き換え)

パターン4: 動的ファイル名(日付別ログ)

サンプルフローの「動的ファイル名(日付別ログ)」を参照してください。

データ → 日付でファイル名生成 → 動的ファイル

用途: 日付ごとに切り替わるファイルへのイベント記録

設定: ファイル名: msg.filename

パターン5: エラーハンドリング

サンプルフローの「エラーハンドリング」を参照してください。

読み込み試行 → 成功時 | Catch → エラー時

用途: 存在しないファイルへのアクセスで発生するエラーの捕捉

ポイント: Catch ノードでファイルエラー(存在しない、権限なし等)をキャッチ

5. 演習問題

演習1: 基本的なログ出力 初級

課題: Inject ノードのボタンを押すたびに、タイムスタンプ付きのメッセージをログファイルに追記してください。

💼 使いどころ: 何らかのイベントが起きるたびに、発生時刻を添えてログファイルへ追記する場面で使います。

成功の条件:

💡 ヒント

タイムスタンプを付ける: Change ノードの「値の代入」で msg.timestamp に「日時」型の YYYY-MM-DDTHH:mm:ss.sssZ(ISO 8601形式)を設定し、Template ノードで {{{timestamp}}} - {{{payload}}} のように1行の文字列を組み立てます。{{{ }}}(波かっこ3つ)は、値を HTML 用に書き換えずにそのまま入れる書き方です。

ファイルへの書き込み: file ノードの「動作」を「ファイルへ追記」に設定し、「メッセージの入力のたびに改行を追加」をオンにします。

✅ 解答例フロー
[ {"id": "ex1_tab", "type": "tab", "label": "演習1"}, {"id": "ex1_inject", "type": "inject", "z": "ex1_tab", "name": "ログ追加", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "イベント発生", "payloadType": "str", "x": 140, "y": 100, "wires": [["ex1_change"]]}, {"id": "ex1_change", "type": "change", "z": "ex1_tab", "name": "時刻を用意", "rules": [{"t": "set", "p": "timestamp", "pt": "msg", "to": "iso", "tot": "date"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 300, "y": 100, "wires": [["ex1_template"]]}, {"id": "ex1_template", "type": "template", "z": "ex1_tab", "name": "ログの行を作る", "field": "payload", "fieldType": "msg", "format": "text", "syntax": "mustache", "template": "{{{timestamp}}} - {{{payload}}}", "output": "str", "x": 470, "y": 100, "wires": [["ex1_file"]]}, {"id": "ex1_file", "type": "file", "z": "ex1_tab", "name": "ログ追記", "filename": "/tmp/exercise1.log", "filenameType": "str", "appendNewline": true, "createDir": true, "overwriteFile": "false", "encoding": "utf8", "x": 640, "y": 100, "wires": [["ex1_debug"]]}, {"id": "ex1_debug", "type": "debug", "z": "ex1_tab", "name": "完了", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "\"追記完了\"", "statusType": "jsonata", "x": 790, "y": 100, "wires": []} ]

演習2: ファイルの読み込み 初級

課題: 演習1で作成したログファイルを読み込み、内容をDebugに表示してください。

💼 使いどころ: 保存済みのログファイルの中身をまとめて取り出し、内容を確認する場面で使います。

成功の条件:

💡 ヒント

file in ノードを使用し、「出力形式」を「文字列」に設定します。

✅ 解答例フロー
[ {"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": 140, "y": 100, "wires": [["ex2_file_in"]]}, {"id": "ex2_file_in", "type": "file in", "z": "ex2_tab", "name": "ログ読み込み", "filename": "/tmp/exercise1.log", "filenameType": "str", "format": "utf8", "chunk": false, "sendError": false, "encoding": "utf8", "allProps": false, "x": 300, "y": 100, "wires": [["ex2_debug"]]}, {"id": "ex2_debug", "type": "debug", "z": "ex2_tab", "name": "内容表示", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 470, "y": 100, "wires": []} ]

演習3: JSON設定ファイルの保存と読み込み 中級

課題: 設定オブジェクトをJSONファイルとして保存し、別のフローで読み込んでオブジェクトとして使用してください。

💼 使いどころ: アプリの設定内容をファイルに保存しておき、必要なときに読み込んで使う場面で使います。

成功の条件:

💡 ヒント

保存時: 設定オブジェクトは Inject ノードの msg.payload に「JSON」型で書きます。JSON ノードの「動作」を「常にJSON文字列に変換」にして文字列にし、file ノードで書き込みます。「JSON文字列フォーマット」をオンにすると、改行と字下げの入った読みやすい形で保存されます。file ノードはオブジェクトを受け取ると自動で JSON 文字列にしますが、その場合は1行に詰めて書かれます。

読み込み時: file in ノードで文字列として読み込み、JSON ノードでオブジェクトに戻します。

✅ 解答例フロー
[ {"id": "ex3_tab", "type": "tab", "label": "演習3"}, {"id": "ex3_inject1", "type": "inject", "z": "ex3_tab", "name": "設定保存", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 140, "y": 80, "wires": [["ex3_json_str"]], "payload": "{\"server\":\"localhost\",\"port\":8080,\"debug\":true}", "payloadType": "json"}, {"id": "ex3_json_str", "type": "json", "z": "ex3_tab", "name": "JSON文字列に変換", "property": "payload", "action": "str", "pretty": true, "x": 290, "y": 80, "wires": [["ex3_file_write"]]}, {"id": "ex3_file_write", "type": "file", "z": "ex3_tab", "name": "JSON保存", "filename": "/tmp/config.json", "filenameType": "str", "appendNewline": false, "createDir": true, "overwriteFile": "true", "encoding": "utf8", "x": 450, "y": 80, "wires": [["ex3_debug1"]]}, {"id": "ex3_debug1", "type": "debug", "z": "ex3_tab", "name": "保存完了", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "\"保存完了\"", "statusType": "jsonata", "x": 620, "y": 80, "wires": []}, {"id": "ex3_inject2", "type": "inject", "z": "ex3_tab", "name": "設定読込", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 140, "y": 160, "wires": [["ex3_file_read"]]}, {"id": "ex3_file_read", "type": "file in", "z": "ex3_tab", "name": "JSON読込", "filename": "/tmp/config.json", "filenameType": "str", "format": "utf8", "chunk": false, "sendError": false, "encoding": "utf8", "allProps": false, "x": 290, "y": 160, "wires": [["ex3_json"]]}, {"id": "ex3_json", "type": "json", "z": "ex3_tab", "name": "JSON解析", "property": "payload", "action": "", "pretty": false, "x": 450, "y": 160, "wires": [["ex3_debug2"]]}, {"id": "ex3_debug2", "type": "debug", "z": "ex3_tab", "name": "設定オブジェクト", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.server", "statusType": "msg", "x": 630, "y": 160, "wires": []} ]

演習4: 日付別ローテーションログ 上級

課題: センサーデータを日付別のファイルに保存し、7日以上前のファイルを自動削除する仕組みを作成してください。

💼 使いどころ: センサーデータを日付ごとのファイルに分けて記録し、古い日付のファイルを整理する場面で使います。

成功の条件:

💡 ヒント

ファイル名の指定: file ノードの「ファイル名」を msg.filename にすると、メッセージごとにファイル名を変えられます。削除は「動作」で「ファイルを削除」を使用します。

日付入りのファイル名を作る: Change ノードの「値の代入」で、msg.filename に「JSONata式」型の式を設定します。

  • 今日: "/tmp/sensor_" & $substring($now(), 0, 10) & ".log"。$now() は現在時刻を 2026-10-02T03:04:05.678Z の形で返し、$substring(…, 0, 10) で先頭10文字(日付の部分)を取り出します。& は文字列をつなぐ演算子です
  • 7日前: $now() の代わりに $fromMillis($millis() - 7 * 24 * 60 * 60 * 1000) を使います。$millis() は現在時刻のミリ秒で、そこから7日分のミリ秒を引いて日時に戻しています

書き込む行は、同じ Change ノードの2つ目のルールで msg.payload に「JSONata式」型の $now() & "," & payload を設定して作ります(例: 2026-10-02T03:04:05.678Z,25.5)。

日付は UTC(協定世界時)で決まります。日本時間の午前0時〜9時の間は、前日の日付のファイルになります。

✅ 解答例フロー
[ {"id": "ex4_tab", "type": "tab", "label": "演習4"}, {"id": "ex4_inject", "type": "inject", "z": "ex4_tab", "name": "センサーデータ", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "25.5", "payloadType": "num", "x": 160, "y": 80, "wires": [["ex4_change_write"]]}, {"id": "ex4_change_write", "type": "change", "z": "ex4_tab", "name": "日付別ファイル名", "rules": [{"t": "set", "p": "filename", "pt": "msg", "to": "\"/tmp/sensor_\" & $substring($now(), 0, 10) & \".log\"", "tot": "jsonata"}, {"t": "set", "p": "payload", "pt": "msg", "to": "$now() & \",\" & payload", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 350, "y": 80, "wires": [["ex4_file_write"]]}, {"id": "ex4_file_write", "type": "file", "z": "ex4_tab", "name": "ログ追記", "filename": "filename", "filenameType": "msg", "appendNewline": true, "createDir": true, "overwriteFile": "false", "encoding": "utf8", "x": 530, "y": 80, "wires": [["ex4_debug1"]]}, {"id": "ex4_debug1", "type": "debug", "z": "ex4_tab", "name": "保存先", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "filename", "targetType": "msg", "statusVal": "filename", "statusType": "msg", "x": 690, "y": 80, "wires": []}, {"id": "ex4_inject_del", "type": "inject", "z": "ex4_tab", "name": "古いファイル削除", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 170, "y": 160, "wires": [["ex4_change_del"]]}, {"id": "ex4_change_del", "type": "change", "z": "ex4_tab", "name": "7日前のファイル名", "rules": [{"t": "set", "p": "filename", "pt": "msg", "to": "\"/tmp/sensor_\" & $substring($fromMillis($millis() - 7 * 24 * 60 * 60 * 1000), 0, 10) & \".log\"", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 370, "y": 160, "wires": [["ex4_file_del"]]}, {"id": "ex4_file_del", "type": "file", "z": "ex4_tab", "name": "ファイル削除", "filename": "filename", "filenameType": "msg", "appendNewline": true, "createDir": false, "overwriteFile": "delete", "encoding": "utf8", "x": 550, "y": 160, "wires": [["ex4_debug2"]]}, {"id": "ex4_debug2", "type": "debug", "z": "ex4_tab", "name": "削除結果", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "filename", "targetType": "msg", "statusVal": "\"削除試行\"", "statusType": "jsonata", "x": 700, "y": 160, "wires": []} ]

6. まとめ

重要ポイント:

注意事項:

別のマシンとファイルをやり取りするには:

fileノードが扱うのは、Node-RED が動いているマシンの中のファイルです。計測器やカメラなど別のマシンとファイルを受け渡す場合は、FTP / SFTP ノードガイドを参照してください。転送で取得したファイルの中身を使うには、そのあとに Read File を繋ぐ必要があります。

7. 実務活用例

ケース1: センサーデータのログ記録

IoTセンサーからのデータを時系列でログファイルに記録。日付別ローテーション。

ケース2: 設定ファイルの管理

JSON/YAML形式の設定ファイルを読み込み、フロー起動時に設定を適用。

ケース3: CSVデータのバッチ処理

CSVファイルを1行ずつ読み込み、データベースへのインポート処理。

ケース4: レポート生成

収集したデータからレポートを生成し、ファイルとして保存・メール送信。

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

🏠