Read and Write Data To a File In Node-Red

3 min read 29 days ago
Published on Sep 11, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial guides you through reading and writing data to a file in Node-RED. You will learn how to use file input and output nodes to handle text, JSON, and CSV formatted data and how to dynamically set file names using a function node. This is a fundamental skill for automating data logging and processing in your Node-RED applications.

Step 1: Set Up Node-RED Environment

  • Install Node-RED: Ensure you have Node-RED installed on your machine. If not, follow the installation guide on the official Node-RED website.
  • Access Node-RED: Open your web browser and navigate to the Node-RED editor, usually found at http://localhost:1880.

Step 2: Create a Flow for File Operations

  • Open the Flow Editor: In the Node-RED editor, you will create a new flow.

  • Add File Input Node:

    • Drag and drop the "file in" node from the node palette onto your flow.
    • Double-click the node to configure it.
    • Set the file path from which you want to read data. Ensure the file exists or create a new one for testing.
  • Add File Output Node:

    • Drag and drop the "file" output node from the palette.
    • Double-click to configure this node as well.
    • Choose the desired file path and set the file action to either append or overwrite.

Step 3: Write Different Data Formats

  • Text Data:

    • Connect an inject node to send a text payload to the file output node.
    • Example payload: "Hello, Node-RED!".
  • JSON Data:

    • To write JSON data, use a JSON object as the payload. Example:
      {
        "name": "Node-RED",
        "type": "Automation Tool"
      }
      
    • Ensure that the file output node is set to handle JSON formatting.
  • CSV Data:

    • For CSV, structure your data as an array of objects. Example:
      [
        {"name": "John", "age": 30},
        {"name": "Jane", "age": 25}
      ]
      
    • Use a CSV node to convert the data before sending it to the file output node.

Step 4: Use a Function Node for Dynamic File Names

  • Add a Function Node:
    • Drag and drop a function node into your flow.
    • Connect this node to the file output node.
    • Double-click to edit the function. Use the following code to set a dynamic file name:
      msg.filename = "data_" + Date.now() + ".json"; // Generates a unique filename
      return msg;
      
  • Connect the Function Node: Link the inject node to the function node and then to the file output node.

Step 5: Deploy and Test Your Flow

  • Deploy Your Flow: Click the "Deploy" button in the top right corner of the Node-RED editor to save your changes.
  • Trigger the Flow: Use the inject node to send test data through the flow.
  • Check the Output: Navigate to the specified file path and verify that the data has been correctly written to the file in the desired format.

Conclusion

By following these steps, you can successfully read and write data to a file using Node-RED. This tutorial covered setting up file input and output nodes, handling various data formats, and using a function node for dynamic file naming. As a next step, consider exploring related Node-RED functionalities such as using the CSV node for advanced data manipulation or automating data logging processes. Happy coding!