Skip to main content

file-write

Write data to files on the local filesystem with support for append and overwrite modes.

Overview

The file-write node writes msg.payload to a file on the local filesystem. It supports write (overwrite) and append modes, automatic directory creation, configurable encoding, and optional newline appending. The filename can be set in the node configuration or dynamically via msg.filename.

Write
Overwrite file
Append
Add to end
mkdir
Auto create dirs
Binary
Buffer support

Properties

Property Type Default Description
filename string "" Output file path. Can be overridden by msg.filename.
action string "write" Write mode: write (overwrite) or append
createDir boolean false Create parent directories if they do not exist
encoding string "utf-8" File encoding: utf-8, ascii, base64, binary, hex
addNewline boolean true Append a newline character after each write
overwriteFile boolean true Overwrite the entire file on each write (when action is write)

Write Modes

write (Overwrite)

Replaces the entire file with msg.payload on each message. The file is created if it does not exist.

Write 1: "Hello"    -> File: Hello
Write 2: "World"    -> File: World

append

Adds msg.payload to the end of the file. Ideal for log files and data collection.

Write 1: "Hello"    -> File: Hello
Write 2: "World"    -> File: Hello\nWorld

Example: Append Sensor Data to CSV Log

Continuously append temperature readings to a CSV file. Each sensor reading is formatted as a CSV line and appended with a newline.

// Function node to format CSV line:
const ts = new Date().toISOString();
const temp = msg.payload.temperature;
const hum = msg.payload.humidity;
const sensor = msg.payload.sensor_id;

msg.payload = ts + "," + sensor + "," + temp + "," + hum;
return msg;

// file-write node configuration:
{
  "filename": "/data/logs/sensor-readings.csv",
  "action": "append",
  "createDir": true,
  "encoding": "utf-8",
  "addNewline": true,
  "overwriteFile": false
}

// Resulting CSV file after multiple writes:
// 2024-01-15T10:00:00Z,dht22-01,23.5,65
// 2024-01-15T10:05:00Z,dht22-01,23.7,64
// 2024-01-15T10:10:00Z,dht22-01,23.4,66

Example: Write JSON Config File

Save updated configuration as a formatted JSON file. Overwrites the entire file each time to ensure the config is always consistent.

// Function node to prepare config:
const config = flow.get('config') || {};
config.lastUpdated = new Date().toISOString();
config.sensors = config.sensors || [];

// Pretty-print JSON for readability
msg.payload = JSON.stringify(config, null, 2);
return msg;

// file-write node configuration:
{
  "filename": "/etc/edgeflow/sensors.json",
  "action": "write",
  "createDir": true,
  "encoding": "utf-8",
  "addNewline": true,
  "overwriteFile": true
}

// Written file:
// {
//   "lastUpdated": "2024-01-15T14:30:00Z",
//   "sensors": [
//     { "id": "dht22-01", "pin": 4 },
//     { "id": "bme280-01", "bus": 1 }
//   ]
// }

Example: Dynamic Filename by Date

Create daily log files by dynamically setting the filename based on the current date.

// Function node to set dynamic filename:
const now = new Date();
const date = now.toISOString().split('T')[0]; // 2024-01-15
msg.filename = "/data/logs/" + date + "-readings.csv";
msg.payload = now.toISOString() + "," + msg.payload;
return msg;

// file-write node configuration:
{
  "filename": "",
  "action": "append",
  "createDir": true,
  "addNewline": true
}

// Creates files like:
// /data/logs/2024-01-14-readings.csv
// /data/logs/2024-01-15-readings.csv

Common Use Cases

Data Logging

Append sensor readings to CSV or text log files

Configuration Management

Save and update JSON or YAML configuration files

Report Generation

Generate daily or weekly report files

Binary Data Storage

Save camera images, firmware, or binary sensor dumps

Related Nodes