
Course description and main motivation behind it .
Topics Covered in the Chapter: "Installing and Configuring Node-RED on Linux"
Introduction to Node-RED Installation on Linux
A focused tutorial on installing Node-RED on Linux, specifically using Ubuntu as the example distribution.
Discussion on the flexibility of Linux environments for Node-RED deployment, from local machines to virtual environments.
Importance of Official Documentation
Explanation of why using the Node-RED community's official documentation is crucial for ensuring compatibility with the latest updates.
Step-by-step guidance on locating and utilizing the "User Guide" and "Installing Node-RED" sections.
System Requirements and Preparation
Overview of the prerequisites, including supported Linux distributions and the need for a stable Node.js installation.
Steps to update and prepare the Linux system for installation.
Installation Process
Detailed instructions on downloading and running the Node-RED installation script.
Guidance on handling common Linux-specific issues, such as permissions or dependencies.
Configuration and Testing
Post-installation steps to configure and launch Node-RED.
Verifying the installation through the web interface and ensuring functionality.
Tips, Best Practices, and Use Cases
Recommendations for maintaining a stable Node-RED setup on Linux, including regular updates and utilizing community forums for support.
Insights into the flexibility of Node-RED on Linux for local and cloud-based IoT applications.
Topics Covered in the Chapter: "Installing Node-RED on Mac"
Introduction to Node-RED Installation on Mac
Overview of the process for setting up Node-RED on a macOS system.
Highlighting the specific system used in the tutorial (Intel-based MacBook Pro) and noting potential differences for Apple Silicon (ARM architecture) users.
System Prerequisites
Discussion on the importance of having Node.js installed as a prerequisite.
Step-by-step guidance on downloading and installing the LTS (Long-Term Support) version of Node.js from the official Node.js website.
Installation Process
Instructions for running the Node-RED installation commands on macOS.
Tips on navigating macOS-specific nuances, such as permissions and configurations.
Testing and Verification
Steps to launch Node-RED and verify the installation by accessing the user interface through the browser.
Suggestions for troubleshooting potential installation issues.
Best Practices and Resources
Emphasizing the importance of consulting the Node-RED documentation for updates and compatibility details.
Discussion on how to maintain the Node-RED environment on macOS and leverage its capabilities for IoT and automation projects.
Topics Covered in the Chapter: "Installing and Configuring Node-RED on Raspberry Pi"
Introduction to Node-RED Installation on Raspberry Pi
A hands-on tutorial focused on setting up Node-RED on a Raspberry Pi 3.
Explanation of the connection methods used in the tutorial (e.g., RealVNC) and alternatives you can use based on your setup.
Brief discussion on the versatility of Raspberry Pi models for IoT and automation projects.
Using Official Node-RED Documentation
Emphasis on the importance of following the Node-RED community's official documentation to ensure compatibility with the latest updates.
A guided walkthrough on locating the "User Guide" and "Installing Node-RED" sections for Raspberry Pi-specific instructions.
System Prerequisites
Overview of essential system checks to ensure your Raspberry Pi is ready for installation, including operating system compatibility and necessary software dependencies.
Tips for identifying and resolving common compatibility issues before beginning the setup.
Step-by-Step Installation Process
Detailed instructions on downloading and executing the recommended Node-RED installation script tailored for Raspberry Pi.
Guidance on post-installation tasks, including launching Node-RED, accessing its interface, and verifying a successful setup.
Tips and Best Practices
Suggestions for maintaining your Node-RED installation, including regular updates and leveraging community resources for troubleshooting.
Discussion on how the flexibility of Node-RED and Raspberry Pi makes them an ideal combination for home automation and IoT projects.
Topics Covered in the Chapter: "Node-RED in Docker Containers"
Introduction to Node-RED in Docker
Explanation of the benefits of using Docker containers for Node-RED, focusing on increased flexibility and portability for automation projects.
Brief overview of the lesson's purpose: to guide users in running Node-RED within Docker containers.
Docker Fundamentals
High-level explanation of Docker as a tool for packaging applications and their dependencies into containers.
Emphasis on container isolation and consistency across different deployment environments (local machine, server, cloud).
Advantages of Dockerizing Node-RED
Discussion of the pros, including isolation to prevent interference with the host system, consistency in running the Node-RED environment, and portability for easy deployment across various platforms.
Explanation of easy management of Node-RED instances using Docker tools and the benefits of version control for managing different Node-RED versions.
Considerations for Using Docker with Node-RED
Addressing potential cons such as the initial learning curve for Docker, resource usage considerations (CPU and memory), and the potential complexity for very simple Node-RED setups.
Finding and Running the Official Node-RED Docker Image
Guidance on locating the official Node-RED Docker image on Docker Hub.
Step-by-step instructions on using the docker run command to start a new Node-RED container.
Introduction to Installing Node-RED on Windows
Overview of the setup process for Node-RED on a Windows machine (can be physical or virtual).
Downloading and Installing Node.js
Instructions for downloading Node.js from the official website and installing it via the Windows Installer (MSI).
Checking Node.js and npm Versions
Verifying installation by checking versions of Node.js and npm using the Command Prompt and PowerShell.
Installing Node-RED Globally
Using npm install -g to install Node-RED globally and addressing network and hardware factors affecting installation speed.
Starting Node-RED
Running Node-RED from the command line and accessing its editor via localhost.
Stopping and Restarting Node-RED
Explaining the effects of stopping Node-RED (e.g., CTRL+C) and restarting the instance for continuous access.
Setting Up Node-RED as a Windows Service
Introduction to using NSSM (Non-Sucking Service Manager) to configure Node-RED as a Windows service for persistent operation.
Configuring NSSM with Node-RED
Detailed instructions on how to set up NSSM with correct paths, startup directories, and arguments for automating Node-RED's startup.
Node-RED Settings File Overview
Introduction to the settings.js file, which Node-RED looks for during startup.
Default location: hidden .node-red folder in the user's directory.
Behavior when the file is missing: Node-RED creates a default version.
Accessing and Editing the Settings File
How to locate the .node-red folder by enabling visibility for hidden files (e.g., in Windows).
Opening the file with a text editor like Notepad++.
Understanding the settings.js File Structure
The file contains over 500 lines, primarily configuration options and comments.
It serves as a template and should be approached carefully to avoid syntax errors that prevent Node-RED from starting.
Important Configuration Options
Flows Storage: Defined in the flows.json file.
User Directory: Location for credentials, library data, settings, and nodes.
Admin Authentication: Setup of user-specific rights for modifying or viewing flows.
Application Port: Node-RED default port (localhost:1880).
Resources and Guidance
Reference to official Node-RED documentation for details on each configuration option.
Encouragement to learn step by step, focusing on the most critical settings.
Assurance of further exploration in subsequent videos.
Closing Remarks
Reassurance that the process will be broken down into manageable steps.
Additional resources and links provided for self-paced learning.
Overview of Node-RED Environment
The environment is accessible locally via the IP 127.0.0.1.
Nodes can be moved, modified, or removed freely, with changes deployed instantly.
Importance of Securing the Environment
The default setup allows unrestricted access, which poses a security risk.
Steps are necessary to enforce authentication and control access.
Preparation for Configuration
Locate the settings.js file (as explained in prior tutorials).
Create a backup of the file before making changes to safeguard against errors.
Configuring Admin Authentication
Navigate to the adminAuth section in the settings file (around line 73).
Duplicate the section as a reference, then uncomment the lines to activate.
Replace the default hashed password with a new hashed password for enhanced security.
Creating and Hashing Passwords
Use the node-red admin hash-pw command in the terminal to generate hashed passwords.
Input the desired plain-text password (e.g., adminTestPass) and copy the resulting hash.
Replace the default password hash in the settings file with the new one.
Adding a Viewer User
Duplicate the admin user configuration to create a viewer user.
Assign a hashed password and limited permissions (read-only access).
Ensure these changes are saved securely and remove unnecessary comments for clarity.
Restarting Node-RED and Testing Changes
Restart the Node-RED service to apply the updated settings.
Troubleshoot syntax errors, such as missing commas in arrays, as they prevent Node-RED from starting.
Confirm the changes by logging in as an admin and a viewer, verifying the respective permissions.
Key Takeaways and Closing Remarks
The settings.js file is crucial for Node-RED's functionality and security.
Avoid syntax mistakes, as they can disrupt the startup process.
Properly configured authentication ensures that unauthorized users cannot modify flows or deploy changes.
Introduction:
A warm welcome to the course.
Theme Recommendation:
Encouragement to use a dark editor theme for better eye health and long-term comfort.
Exploring Theme Options:
Overview of available dark themes (e.g., Aurora, Cobalt, Dark).
How to browse and select a theme from the Node-RED Contrib Theme Collection.
Installing a Dark Theme:
Step-by-step guide to installing a theme using the "Manage Palette" feature in Node-RED.
Reminder to use Node-RED version 4 or later.
Customizing Themes:
Instructions on editing the settings.js file to change themes.
Explanation of how to apply changes and restart Node-RED for customization.
Closing Remarks:
Emphasis on the benefits of using a dark theme for development.
Encouragement to customize the setup for a comfortable workflow.
Accessing the Node-RED Editor:
Instructions to access the Node-RED editor via a web browser.
Explanation of starting Node-RED and resolving port conflicts.
Options to access Node-RED using localhost or the local IP address.
Overview of the Node-RED Editor:
Explanation of the top bar components:
Node-RED branding.
Deploy button for saving flows.
Logged-in user and main menu.
Description of the left-side panel:
Node palette showing installed nodes.
Filtering nodes for quick access.
Toggling visibility of the node palette.
Central workspace:
Area for creating flows by dragging and connecting nodes.
Managing and deleting nodes.
Navigation options for large flows.
Navigation and Tools:
Zooming in, zooming out, and resetting zoom levels.
Navigation window for managing complex or large flows.
Right-Side Panel Features:
Information panel for an overview of flows.
Help panel for node documentation.
Debug message window for runtime logs.
Configuration nodes for settings like MQTT or VPN.
Context Data:
Mention of context data as an essential topic to be covered in detail later.
Closing Remarks:
Overview of the editor's key features and navigation.
Promise of deeper exploration in future lessons.
This chapter provided a comprehensive introduction to the Node-RED editor interface, focusing on navigation, workspace organization, and essential tools for working with flows.
Comprehensive Overview of Node-RED's Right Side Panel
This detailed guide covers the key features and functionalities of Node-RED's right side panel, essential for efficient workflow management and debugging.
Info Tab: Flow Management and Navigation
Flow Operations
Hide/Unhide: Temporarily remove flows from view
Disable/Enable: Stop or restart flow execution
Lock/Unlock: Prevent or allow node movement within the workspace
Search and Navigation
Search across flows and nodes
Quick location of specific flows or nodes
Find nodes in the workspace with orange highlighting
Flow ID generation for easy sharing
Copy URL for direct flow access
Additional Features
Tips and tricks section (optional)
Keyboard shortcut: CTRL+G and I
Help Section: Comprehensive Documentation
Node-RED Information
Changelog for the latest version
Welcome tour and video guides
Detailed node documentation
Node Details
Input/output specifications
Configuration options
Usage examples
Searchable help for specific nodes
Debug Messages Panel: Powerful Debugging Tool
Filtering Options
All nodes view
Current flow debugging
Specific node selection
Filter by flow
Message Management
Clear filtered messages
Clear all debug messages
Real-time message tracking
Configuration Nodes: Centralized Configuration Management
Configuration Scopes
Global configuration nodes
Flow-specific configurations
Node-specific configurations
Configuration Tracking and Management
View unused configuration nodes
Track node usage across flows
Easily modify configuration scopes
Navigate to associated nodes
This comprehensive overview of Node-RED's right side panel showcases its powerful management tools, making it an invaluable resource for both beginners and advanced users in streamlining their Node-RED workflows.
Topics Discussed in the Chapter: Flows, Nodes, and Connections
Understanding Flows:
Explanation of the term "flow" and its informal usage.
Differentiation between individual flows and the overarching flow container.
Methods to create, rename, reorder, and manage flows.
Adding descriptions and using the right-side panel to view flow properties.
Enabling, disabling, hiding, and listing flows.
Working with Nodes:
Adding nodes to the workspace using drag-and-drop or the control key.
Properties of nodes, such as single input/multiple outputs.
Exploring node functionality like inject nodes, switches, and debug nodes.
Indications of misconfigurations (e.g., invalid node properties) and their resolution.
Node Properties:
Modifying node names, descriptions, and appearances.
Changing icons and labels for nodes.
Understanding how appearance options (e.g., hiding labels or customizing tags) impact visual representation.
Connecting Nodes:
Techniques for connecting nodes via wires, including shortcuts for quick connections.
Understanding the impact of enabling/disabling individual nodes within a flow.
Navigating connections between nodes and interpreting deployment symbols.
Additional Features and Tips:
Managing hidden flows and their functionality even when not visible.
Ensuring proper configuration to avoid deployment issues.
Leveraging efficient methods to build and manage flows for clarity and functionality.
This chapter offers a comprehensive introduction to the foundational elements of working with flows, nodes, and connections in Node-RED, providing both technical details and practical tips for efficient use.
Introduction to Node Management:
Node-RED comes with a base set of nodes for common tasks.
Users can significantly expand Node-RED's capabilities by installing additional nodes from the Node-RED library, created by both the Node-RED team and the community. This allows integration with various services, protocols, and hardware.
Finding Nodes:
The official Node-RED website (nodered.org) provides a central repository for finding nodes, flows (pre-built examples of node configurations), and collections (bundles of related nodes and flows).
The "Flows" section is organized into categories to help users discover relevant resources.
Each node listing provides important information, including the command needed for command-line installation, the node's update history, the number of downloads, and documentation on its usage.
Installing Nodes:
Via the Node-RED Interface: The "Manage Palette" feature within the Node-RED editor provides a graphical way to browse and install nodes directly within the application.
Via the Command Line: Using the Node Package Manager (npm) in the terminal offers a more direct way to install nodes, especially useful for automation or server environments.
Restarting Node-RED:
Restarting Node-RED is crucial after installing, removing, or updating nodes to ensure the changes are loaded and applied correctly.
The restart method differs depending on how Node-RED was started:
If Node-RED is running as a system service, a systemctl command is used.
If Node-RED was started manually from the command line, the process needs to be stopped (Ctrl+C) and restarted.
Removing Nodes:
Nodes can be easily uninstalled through the "Manage Palette" interface, removing them from the Node-RED editor and system. This is important for maintaining a clean and efficient Node-RED environment.
Updating Nodes:
Keeping nodes up-to-date is essential for bug fixes, performance improvements, and security patches.
Via the Node-RED Interface: The "Manage Palette" interface shows available updates and provides a simple update button.
Via the Command Line: The npm outdated command lists nodes with available updates, and npm install with the @latest tag updates a specific node to the newest version.
Introduction to Flow Export and Import:
This feature allows users to easily replicate, back up, or share Node-RED flows.
It's useful for deploying flows across multiple devices (e.g., Raspberry Pis), sharing flows with the community, or creating backups in case of accidental changes or system failures.
The export format is JSON (JavaScript Object Notation), a standard text-based format for representing structured data.
Exporting a Single Node:
Users can select individual nodes within a flow and export only those selected nodes.
The exported data can be copied to the clipboard for immediate import elsewhere.
Importing from the Clipboard:
Node data copied to the clipboard can be pasted into the import dialog.
Users can choose to import the node(s) into the current flow or create a new flow for them.
If a node with the same ID already exists, Node-RED will import it as a copy to avoid conflicts.
Exporting an Entire Flow:
Without selecting any specific nodes, users can choose to export the entire current flow (all nodes and connections within that tab).
Similar to single node export, the data can be copied to the clipboard for import.
Exporting to a File (JSON):
Flows can also be exported to a JSON file, which can be saved and shared.
The export dialog offers options for formatting the JSON output:
"Compact" format creates a smaller file size, suitable for sharing online.
"Formatted" format adds indentation and whitespace, making the JSON easier to read and manually edit.
This file-based export is useful for backups, sharing flows via file transfer or version control systems, and manually editing flow configurations.
Importing from a File (JSON):
Exported JSON files can be imported back into Node-RED.
This allows users to restore flows from backups or import flows shared by others.
Importing from a file also allows for pre-editing flow configurations.
Manually Editing the JSON File:
The tutorial briefly covers the structure of the exported JSON file.
The file contains information about the flow itself (ID, label, description) and detailed information about each node (type, configuration, connections/wires).
Manually editing the JSON file allows for advanced configuration changes or troubleshooting, such as modifying node settings directly or removing corrupted node data. The tutorial showed a simple example of changing the flow's description.
Introduction to Node-RED Flows and Information Flow:
Review of how flows are constructed from interconnected nodes.
Introduction to the concept of how data or information is passed between these connected nodes.
The Message Object:
Explanation that information travels between nodes as a "message" entity.
Definition of the message as a standard JavaScript object, allowing for flexible data structures.
Using the Debug Node to Inspect Messages:
Introduction of the Debug node as a tool to inspect the contents of messages.
Explanation of the Debug node's default behavior (showing only the msg.payload).
How to configure the Debug node to display the "Complete Message Object," including all its properties.
Message Object Properties:
Explanation of key message object properties:
msg.id: A unique identifier for each message, useful for tracking messages as they move through the flow.
msg.payload: The main data carried by the message, which can be any valid JavaScript data type (string, number, boolean, object, buffer, etc.).
msg.topic: A string used to categorize or route messages.
Emphasis on the message object as the fundamental entity; without a message object, no data can be transmitted between nodes.
Data Types in Messages:
Demonstration of sending different data types (string, number, boolean, JSON object) as the msg.payload.
Highlighting the flexibility of the message object in handling various data formats.
Accessing Message Properties with Paths:
Explanation of how to access nested properties within the message object (e.g., msg.payload.CPU to access the "CPU" property within a payload object).
Demonstration of how to copy the path to a specific property from the debug message output.
Emphasis on the correct syntax for accessing nested properties in code (using dot notation: msg.payload.CPU).
Debug Node Display Options (Pin Open):
Explanation of the "pin open" feature in the Debug node's output.
How this feature allows for the automatic unfolding/expansion of objects within the debug output, making it easier to monitor changing values over time without manually expanding each object every time a new message arrives.
Importance of Understanding Message Flow:
Reinforcement of the importance of understanding how messages flow between nodes and how they can be manipulated using function nodes, etc.
Brief overview of how this knowledge can be applied to various Node-RED use cases, such as interacting with APIs, databases, charts, maps, and implementing automation logic.
Introduction to Message Manipulation:
Review of message basics from the previous lesson.
Introduction of two key nodes for manipulating messages: the Change node and the Function node.
The Change Node:
Description of the Change node as providing default actions for message manipulation (setting, changing, deleting, moving properties).
Brief comparison with the Function node, highlighting the Change node's ease of use for basic operations.
The Function Node (Brief Mention):
Acknowledgment of the Function node's greater flexibility for complex logic, but requiring JavaScript knowledge.
Recommendation to use the Change node whenever possible for simpler tasks.
Deferral of a detailed explanation of the Function node to a future lesson.
Using the Random Node (Installation):
Demonstration of installing a new node (the "random" node) from the Node-RED palette manager.
Explanation of how to access the palette manager and search for nodes.
Use of the random node to simulate sensor data (humidity in this example).
Setting the Payload with the Change Node:
Demonstration of the Change node's "set" operation to override the msg.payload with a fixed value.
Explanation of how this can be used to set a default value or force a specific value for testing purposes.
Setting the Topic with the Change Node:
Demonstration of using the Change node to set the msg.topic property.
Explanation of how this can be used to add metadata to messages for routing or identification.
Changing the Topic with the Change Node:
Demonstration of using the Change node's "change" operation to find and replace parts of the msg.topic.
Explanation of how this can be used to modify existing topic values based on specific criteria.
Deleting Properties with the Change Node:
Demonstration of using the Change node's "delete" operation to remove properties from the message object (e.g., deleting the msg.topic).
Moving Properties with the Change Node:
Demonstration of using the Change node's "move" operation to rename properties (e.g., moving msg.topic to msg.home).
Introduction to JSONata Expressions:
Introduction of JSONata as a powerful query and transformation language for JSON data.
Explanation that JSONata allows for complex filtering and manipulation of JSON structures within the Change node.
JSONata Example and Testing:
Demonstration of a simple JSONata query to extract specific data from a JSON object (extracting product names from an invoice).
Explanation of how to test JSONata expressions directly within the Change node's editor.
Demonstration of how to apply a JSONata expression to the msg.payload within the Change node.
Importance of JSONata:
Emphasis on the power and usefulness of JSONata for more advanced message manipulation.
Encouragement to explore JSONata further using the online documentation.
Recap of Message Flow and Node Functionality:
Brief recap of how messages travel between nodes and how their content can be modified by nodes like the Change node.
Mention of more advanced flow scenarios involving function nodes, APIs, databases, and other integrations.
Introduction to the Function Node:
The Function node executes JavaScript code against incoming messages.
Messages received by the node are processed and outputted, potentially with modifications.
Output can be directed to one or more outputs on the node.
The node's behavior depends on the incoming message type and source.
Function Node Configuration (Setup Tab):
The "Setup" tab allows configuring the number of outputs for the node.
By default, a Function node has one output, but more can be added.
Future lessons will cover adding external modules (libraries).
On Start Tab:
Code placed in the "On Start" tab executes only once when the node starts.
This occurs when the "Deploy" button is clicked and the flow begins running.
Useful for initializing the flow or sending an initial event.
On Stop Tab:
Code in the "On Stop" tab executes when the flow is stopped (e.g., before a new deployment).
This allows for actions like completing pending operations or closing open connections.
The "On Stop" code runs before the "On Start" code during a redeploy.
On Message Tab:
The "On Message" tab contains the core JavaScript code that processes each incoming message.
This code is executed every time a message is received by the node.
A simple example using WARN() to output text to the debug log as a warning is shown.
Basic Message Processing Example:
A simple flow with an Inject node, Function node, and Debug node is created.
Initially, the Function node simply returns the incoming message (return msg;), demonstrating a pass-through behavior.
Modifying the Message Payload:
An example is shown where a string ("hello") is injected.
JavaScript code within the Function node converts the payload to uppercase.
The use of let to declare a variable to hold the modified payload is demonstrated.
The importance of returning a message object is emphasized. Returning just a string will cause an error. The correct way to modify the payload is to modify the msg object and return it: msg.payload = msg.payload.toUpperCase(); return msg;
Appending the transformed value to the message as a new property (msg.upper) is shown as a best practice.
Using Multiple Outputs:
The Function node is configured with two outputs.
An example demonstrates how to send different messages to different outputs based on a condition (uppercase vs. lowercase).
Returning an array in the return statement is explained as the mechanism for directing messages to different outputs. The first element of the array goes to the first output, the second to the second output, and so on.
The importance of returning message objects (not just strings or variables) in the array is reiterated to avoid errors.
Sending Multiple Messages to the Same Output:
An example shows how to send multiple messages to the same output by using nested arrays within the return array. This allows one output to receive multiple messages sequentially.
Routing Messages Based on Topic:
An example demonstrates routing messages based on the msg.topic property.
An if/else statement is used to conditionally send messages to different outputs based on the topic value.
Key Takeaways:
The Function node is a powerful tool for message manipulation using JavaScript.
Understanding JavaScript is beneficial but not strictly required for basic operations.
Returning message objects is crucial.
Multiple outputs and sending multiple messages to the same output are possible using arrays in the return statement.
Message routing can be implemented using conditional logic based on message properties like msg.topic.
Introduction to Context Variables:
Context variables are mechanisms for sharing values between nodes.
There are three levels of scope: node, flow, and global.
This tutorial focuses on flow and global scope, as node-level context is rarely used.
Flow Context Variables:
Flow context variables are accessible by all nodes within the same flow.
Example: flow.x = 1 makes the variable x with the value 1 available to all nodes in that specific flow.
Variables set in one flow are not accessible in other flows.
Global Context Variables:
Global context variables are accessible by all nodes across all flows.
Example: global.x = 1 makes the variable x with the value 1 available to all nodes in any flow.
Useful for sharing data between different parts of a Node-RED application.
Using the Change Node to Set Context Variables:
The Change node can be used to set both flow and global context variables.
In the Change node configuration:
Set the "Set" operation.
For flow context: Select "flow" and enter the variable name (e.g., x).
For global context: Select "global" and enter the variable name (e.g., x_global).
Set the value using an expression, such as msg.payload to use the incoming message payload.
Viewing Context Data:
Context data can be viewed in the Node-RED editor by clicking the sidebar (usually on the right) and selecting the "Context Data" tab.
This shows the current values of flow and global context variables.
Example: Setting and Reading Flow Context:
An Inject node sends a number (e.g., 1, 6).
A Change node sets the flow context variable x to the incoming payload.
Another Change node reads the value of flow.x and sets it as the new msg.payload.
A Debug node displays the resulting payload.
Demonstrates that reading a non-existent flow context variable results in an undefined value (or the Change node omitting the payload entirely).
Demonstrating Flow Context Isolation:
A second flow is created.
A Change node attempting to read flow.x in the second flow will not find the variable (unless it has been set in that flow). This highlights the flow-specific scope.
Example: Using Global Context to Share Data Between Flows:
The first Change node is modified to set a global context variable named x_global.
The second flow's Change node is modified to read global.x_global.
This demonstrates that changes to x_global in the first flow are immediately reflected in the second flow.
Key Use Case:
Global context is useful when data generated in one flow needs to be processed or used in another flow.
Further Information:
More advanced information is available on the Node-RED website.
Summary:
Context variables (flow and global) provide a way to share data between nodes in Node-RED.
Flow context is limited to the current flow, while global context is accessible across all flows.
The Change node provides a convenient way to set and read context variables.
Understanding context is essential for building more complex Node-RED applications.
Review of Change Node Context Usage:
The tutorial begins by briefly recapping how to set and retrieve flow and global context variables using the Change node.
It emphasizes the straightforward nature of using the "flow" and "global" options in the Change node configuration.
Introduction to Function Node Context Usage:
The focus shifts to using the Function node to work with context variables.
This provides greater flexibility, especially when needing to manipulate values within JavaScript code.
The tutorial highlights the importance of this method for retrieving values within function blocks.
Context Object in Function Node:
Inside the Function node's JavaScript code, context variables are accessed through the context object.
The context object has methods for setting (set) and getting (get) context variables.
The different scopes (node, flow, global) are accessed as context.node, context.flow, and context.global, respectively.
Node Context Example:
An example demonstrates setting a node context variable: context.set("test", 5);
This sets a variable named "test" with the value 5 within the node's context.
Retrieving this value is done with context.get("test");
The tutorial briefly explains how to use the recovered value in the message payload (msg.payload = recoveredValue;).
Flow and Global Context in Function Node:
The tutorial moves on to the more commonly used flow and global contexts.
Setting a flow context variable: context.flow.set("function_flow_variable", {temp: 21}); (This example sets an object as the value).
Setting a global context variable: context.global.set("function_global_variable", 24);
The tutorial mentions that retrieving these values is done similarly using context.flow.get() and context.global.get().
Example: Setting in One Flow, Retrieving in Another (Global Context):
A practical example demonstrates setting a global variable in one flow and retrieving it in another.
Setting (Flow 1): context.global.set("global", 9);
Retrieving (Flow 2):
let recovered = context.global.get("global");
msg.payload = recovered;
The tutorial emphasizes that the variable name in get() must be a string (using double quotes: "global"). An attempt to use global without quotes results in an error.
Setting Multiple Variables at Once:
The tutorial shows how to set multiple context variables simultaneously using arrays:
context.global.set(["var1", "var2", "var3"], [1, 2, 3]);
This sets global.var1 to 1, global.var2 to 2, and global.var3 to 3.
Retrieving Multiple Variables at Once:
Multiple variables can also be retrieved at once using an array in get():
let recovered = context.global.get(["var1", "var2", "var3"]);
This returns an array containing the values of the requested variables.
Accessing Specific Array Elements:
The tutorial explains how to access individual elements of the returned array using array indexing (e.g., recovered[0] for the first element, recovered[2] for the third).
It points out that array indices start at 0 and that accessing an out-of-bounds index results in undefined.
Key Takeaways:
The Function node provides flexible control over context variables using the context object.
context.set() and context.get() are used to set and retrieve values, respectively.
context.flow and context.global specify the scope.
Setting and retrieving multiple variables at once is possible using arrays.
Variable names in get() must be strings.
Array indexing can be used to access specific elements when retrieving multiple variables.
Flow context variables are accessible only within the flow they're defined in, while global context variables are accessible across all flows.
Default Context Behavior (In-Memory):
By default, Node-RED stores context variables in memory.
This means that if Node-RED restarts (due to a crash, intentional restart, etc.), all context variables are lost.
A demonstration shows this by setting a global variable, verifying it in the context data view, stopping and restarting Node-RED, and showing that the variable is no longer present.
Making Context Persistent:
The tutorial explains how to make context variables persistent across restarts by saving them to the local file system.
It references the Node-RED documentation: "docs user guide context" and "context stores."
Node-RED provides two built-in storage modules: "memory" (default) and "local file system" ("file").
Custom storage plugins can be created for other storage mechanisms (e.g., databases), but this is an advanced topic not covered in the tutorial.
Modifying the settings.js File:
The key to enabling persistent context is modifying the settings.js file.
This file is typically located in the user's .node-red directory (a hidden directory).
The tutorial recommends making a copy of settings.js before modifying it as a backup.
The contextStorage property in settings.js needs to be configured. By default, it's commented out.
Configuring contextStorage for Memory and File:
The tutorial provides the correct configuration for using both memory and file storage:
JavaScript
contextStorage: {
memoryOnly: { module: 'memory' },
file: { module: 'localfilesystem' }
},
This allows choosing between "memory" and "file" for individual context variables.
A common mistake of missing a comma in the settings file is shown, and the solution is provided.
Applying the Changes and Restarting Node-RED:
After modifying settings.js, Node-RED must be restarted for the changes to take effect.
The tutorial demonstrates how to stop and start Node-RED from the terminal.
Using the Context Data View and the Storage Options:
After restarting, a small database icon appears next to global and flow variables in the context data view.
This icon indicates that you can now choose the storage mechanism for that specific variable.
By default, it uses memory storage.
Clicking the icon allows switching to file storage.
File Storage Behavior (Delay):
Node-RED does not immediately write changes to the file system. It has a delay (approximately 30 seconds by default) before saving context data to the global folder (and respective json files).
This delay is configurable (though the tutorial doesn't go into details), but reducing it too much could impact performance, especially with many context variables.
The tutorial demonstrates waiting for the delay, after which a global folder containing global.json appears within the .node-red directory. The global.json file contains the saved global context variables in JSON format.
Setting Persistent Context with a Function Node:
The tutorial shows how to set persistent context using a Function node:
JavaScript
context.global.set("global_context", 5, "file");
The third argument, "file", specifies that this variable should be stored in the file system. If omitted, it defaults to memory.
Key Points:
Context variables are stored in memory by default and are lost on restart.
Modifying settings.js is required to enable persistent storage.
The contextStorage property allows configuring different storage modules.
Changes to settings.js require a Node-RED restart.
A delay exists before data is written to the file system.
The Function node can be used to set context variables and explicitly specify their storage mechanism.
This allows saving application state between restarts, which is crucial for many applications.
This summary provides a structured overview of the tutorial, including the key steps, code examples, and important considerations for using persistent context in Node-RED.
Introduction and Motivation:
I address implementing POST requests in Node-RED, based on user feedback.
POST requests serve to send data to an endpoint.
As I don't use an external service, I create two endpoints within the same Node-RED instance to demonstrate the process.
Basic POST Request (Without Authentication):
Nodes I use:
Inject Node: To create the payload to be sent.
HTTP Request Node (POST): To send the POST request.
HTTP In Node (POST): To create the receiving endpoint.
Function Node: To capture the received payload.
HTTP Response Node: To send a response back to the request.
Debug Nodes: To display the request and response data.
HTTP Request Node Configuration:
Method: POST
URL: The URL and endpoint of the receiving service (e.g., http://<server_ip>:<port>/test_POST_endpoint or http://localhost:1880/test_POST_endpoint).
HTTP In Node Configuration:
Method: POST
URL: The endpoint name (must match the URL in the HTTP Request node).
Flow:
The Inject node sends a payload.
The HTTP Request node sends a POST request to the defined endpoint.
The HTTP In node listens for POST requests on that endpoint.
The Function node captures the received payload.
The HTTP Response node sends a 200 OK response with a message.
Debug nodes display the request and response data.
I explain that the HTTP In node captures the payload and responds with a 200 OK status code and a message ("Your payload was received successfully").
I emphasize that in a real-world scenario, I would process the received payload further.
POST Request with Basic Authentication:
Nodes I use: Same as the basic example, with the addition of a Switch node.
HTTP Request Node Configuration:
Headers: I add an "Authorization" header with a token (e.g., "Bearer your_token").
URL: I use a different endpoint (e.g., /test_POST_endpoint_encrypted).
Switch Node: To evaluate the authorization token.
Flow:
The Inject node sends a payload.
The HTTP Request node sends a POST request with the Authorization header.
The HTTP In node receives the request.
The Switch node checks the msg.req.headers.authorization to validate the token.
If the token is valid: The flow continues to the Function node (payload capture) and the HTTP Response node (200 OK).
If the token is invalid: The flow goes to a separate HTTP Response node (401 Unauthorized) with an "Unauthorized" message.
I explain how to access the authorization token using msg.req.headers.authorization in the Switch node. I also show how to find the token in the debug output of the HTTP In node under msg.req.rawHeaders (it's often at index 3).
I demonstrate sending requests with both valid and invalid tokens, showing the different responses and status codes (200 and 401).
Further Improvements and Considerations:
I suggest adding more robust validation, such as checking the format of the incoming data or requiring specific fields in the payload.
I mention using different HTTP status codes for different error conditions (e.g., 400 Bad Request).
I emphasize the importance of security and recommend using at least basic authentication in real-world applications.
Course Description:
Are you ready to unlock the limitless potential of IoT and automation using Node-RED?
Welcome to the Node-RED Field Guide : your comprehensive guide to mastering this user-friendly, flow-based development tool.
Node-RED has become the go-to platform for IoT enthusiasts, developers, and engineers worldwide. In this course, I take you on a journey through both the fundamentals and advanced aspects of Node-RED, ensuring you gain the expertise to create powerful flows and automations effortlessly.
This course is intended to be practical but also serves as a reference course ( When you want to know how something is done just access the course and double check, hence the field guide title :P )
What You'll Learn:
1. Master the Basics: We begin with the essentials, ensuring you have a solid foundation. You'll explore the Node-RED interface, understand the core concepts of nodes, and learn how to create your first flows. Whether you're a complete beginner or just looking to refresh your knowledge, I've got you covered.
2. Advanced Techniques: As you progress, we dive into advanced techniques, such as debugging, error handling, and best practices. You'll be equipped with the tools to streamline your workflow, troubleshoot effectively, and build robust applications.
3. Automation Made Easy: Node-RED's intuitive interface allows you to automate tasks like never before. Discover how to create powerful automations that can simplify your daily life, from home automation to data analysis.
4. API Integration: Node-RED's versatility extends to integration with other services and platforms. You'll delve into connecting with external APIs, data exchange, and leveraging external data sources to enhance your projects.
5. Real-World Projects: My course goes beyond theory. Theory is good but practice is better. The idea is to add new projects to the couse as i finish them, so expect updates from time to time with new integrations an automations.
Who Should Enroll:
Beginners who want to kickstart their IoT journey.
Developers looking to expand their skill set and dive into IoT & automation.
Engineers and hobbyists interested in simplifying and automating processes.
Anyone who desires hands-on experience with practical automation projects.
Why Choose This Course:
My approach is hands-on and practical.
I focus on equipping you with the skills you need to start creating IoT solutions immediately. By the end of this course, you'll have the confidence and knowledge to tackle both simple and complex Node-RED projects.
Join me and unlock the power of Node-RED to build the IoT projects you've been dreaming of or just automate that tedious task you don't want to repeat all the time.
Enroll today and take the first step towards becoming a Node-RED pro!