Node Red Course:
Why NodeRed and not Homeassistant for making your automations?
Hassistant seems to have everything included. But still, Node Red seems to be a lot.
more intuitive. It is easy to modify and evolve.
Why install Home Assistant? It's because it is invaluable as a system to use it as
automator with sensors and all that. The Home Assistant dashboards are
better.
In Node-RED, we have a set of input and output blocks.
We also have debug blocks to visualize what we have in the output.
In the message sending configuration block, we can combine a message.
complete.
What is a JSON object? : it is a key-value dictionary.
To correctly nest a json, it is done through:
{
value
“dic1”:{
valor_int1
value_int2
}
}
Where can Node-RED be applied:
• Industrial Automation.
• Home Automation/Building Automation
• Internet of Things
• Maker
• IIoT
• Industry 4.0
• Artificial Intelligence.
What is Node-RED and how does it work:
It is a visual programming tool. Similar to Visual Basic.
Created in 2013. It was released on GitHub in that same year of creation.
Seek to deliver hardware with other services.
It is created from nodejs. It is accessible through a webpage and doesn't need
of the IDE.
Its minimum structure consists of palettes called nodes.
Nodered connects to third-party applications and can use protocols of
communication through settings.
Where can we install nodered?
WINDOWS7-10-ETC.
Debian systems.
Based on RPM.
Prerequisites
Compatible version of Node.js
Hardware: Raspberry Pi and BeagleBone Black.
npm installation.
Docker installation.
Snap installation.
The nodes execute a function specified by the user:
There are cord nodes: they are pre-installed. They come with the package:
Exit nodes.
Input nodes.
-Input/Output nodes: Function.
Output nodes:
inject:
They only provide data after receiving it through different methods, for example:
a mqtt message
errors thrown by the nodes
or like the case of the inject node, it injects a message into a flow manually or to
regular intervals.
Input nodes:
They only accept input data to send it to a site
A database
an indicator on a dashboard
Or like the case of the debug node, it receives the data and displays it in the window of
debug properties. By default.
Input/output nodes:
They allow the input of data and then offer one or several outputs, for example:
We can read the temperature value in degrees Fahrenheit and convert it to degrees.
celsius.
We can evaluate the input value and compare it with a value.
default (switch/case).
Or like the function node, which allows us to enter JavaScript code to be executed
against the messages received and return an object from the message (or several from
message).
In Node-Red, executions run in parallel unlike Arduino or
from a microcontroller where a task is executed and
after the following.
In the function block we can enter JavaScript code, but if we enter something
incorrect, this is going to generate an error.
Messages can be placed in the nodes; when developing a project, they are generated.
comments and indicate what the node does in our project.
MQTT blocks:
MQTT node in:
You configure the server, that is, where the broker works.
In the configuration section, we can observe which nodes have that type of
configuration.
The context tab exists.
There are 3 types of variables to use in node-red:
Local variables.
-Variables Flow:
Global Variables.
Do you remember when in other programming languages you used
a function to reuse code every time you called it. Well in node-red it
they use what we call subflows to carry this out.
Context of access to information about variables:
Variable Node on Scope:
When creating a variable of the node or local type, we can only access it through
from the same node. It cannot be from a different node.
FLOW type variables: They are accessible through any node that belongs to
that flow. But they do not have access to that variable from other flows.
Global variables. They are accessible in any of the nodes that belong to it.
flow.
Introduction to messages and data types:
The flow in nodered works by passing messages between the nodes, the messages are
JavaScript objects. That have key-value pairs. Messages have property
payload. This works on most nodes.
The switch node is similar to an IF in C. It allows us to identify the property of
message.
the property of msg.payload is what determines that the inject sends its values.
How to control the speed of messages flowing through a flow:
These nodes are:
Delay and Trigger.
The delay applies a delay to the messages that pass through it.
And the trigger, when activated, can send a message and optionally a second one.
message if they extend and restore messages.
The delay:
Important terms or concepts in Node-RED:
Parts or components of the editor:
Node palette: All nodes to use.
Workspace: Flows or project tabs.
Header
Flow or Tab Flow: It indicates that there is a set of nodes
interconnected.
NodeRedDebbuger: Tool.
It is a complement for nodered2.0, allows setting breakpoints.
Allows you to manually pause the execution time.
Pause the flow, also inspect the messages.
It basically works like the debugger of any code IDE.
Split and Join Nodes:
The behavior is based on the type of msg.payload.
The split node separates the messages you send according to how you configure it.
The join node works together with the split node, according to how you configured it.
Split node, combine the message.
The join node, when working without the split node, must be configured in manual mode.
Nodes that allow us to work with the JSON format:
Theory: What is JSON?: Data exchange is fundamental. For this we use
JSON (JavaScript Object Notation). It is a text format independent of
language.
Being a format independent of any programming language is that
services that share information do not necessarily have to be the same
"LANGUAGE" for example a receiver can be from python, while the one that sends
information can be from JAVA.
Common format for serializing and deserializing objects in most languages.
It has 2 structures, it is a name/value pair, it is similar to a dictionary.
JSON is represented by plain text.
A JSON node converts plain text into JSON format.
What is the object in Node-RED used for? To access values or keys of
our object.
Concepts of the function node:
The code entered in the function node represents the body of the function.
The simplest thing to do is to return the message as it came.
The second example is if the function returns null, it returns nothing.
Third example, the function must always return a msg object.
Fourth example, delete the properties of the existing message or do not forward them to
next node.
Node link:
Having a large number of flows.
We have virtual connections. I mean virtual cables.
NODE RED IS BASED ON THE MQTT PROTOCOL
Basically, you already know the above, except for some additional information. But
most importantly comes from the topics, the wildcards for subscribing, which
only the subscriber uses them.
Single-level wildcard:
(“+”)
for example the subscription to the topic:
myhouse/ groundfloor / + / temperature, you are going to subscribe to:
my house
myhouse/ groundfloor / bedroom / temperature.
myhouse/ groundfloor / patio / temperature.
my house
Wild card of various levels:
“#”.
It must be the last character of the topic.
Which you are going to subscribe to:
myhouse/ groundfloor / #:
myhouse/ groundfloor / kitchen / temperature.
my house
my house/ ground floor / patio / current.
my house/ ground floor/ dining room/ pressure.
Best practices for using topics:
Never use a backslash (\).
So it can be used, but what it generates is that it introduces an unnecessary level of topic.
with a zero character at the beginning. It does not provide benefits and often generates
confusion.
Never use spaces in the topics:
They cause difficulties during the debugging of the completed project.
Use brief and concise topics.
Well, because it consumes bytes and that memory is necessary.
Use only ASCII characters, avoiding non-printable characters.
What is the payload?: from English cargo.
Set of transmitted data that is actually the value of the variable that sends the
client to broker.
Other types of messages used in MQTT:
Message sent about a topic to indicate that the client has established a
successful connection.
It can be configured or not.
Another is the close, it is when the client disconnects from our mqtt network.
Another type of message is the last will and testament: This is sent when it has been
our client disconnected unexpectedly.
A retained message is a normal mqtt message with the flag set to true.
Storing the last retained message and the quality of service, which the customer
Subscribed to that held message will be received immediately upon resubscribing.
The broker only stores one retained message per topic.
The held messages are for customers to receive a status update.
when subscribing to a topic.
Service quality (you already know this). The higher the service quality, the lower it will be.
performance.
Persistent sessions are the information stored by the broker, this is convenient.
for clients with limited resources such as microcontrollers. Because if
the connection to the broker subscription is lost, this can cause problems for them
them.
What does it store? *Existence of a session.
All customer subscriptions.
All messages in a flow (QoS). 1 or 2 that the client has not confirmed.
All new QoS 1 or 2 messages that the client lost offline.
All QoS messages received from the client that have not yet been fully acknowledged.
Just as the broker stores information, the client also stores it.
next information:
All messages in a QoS 1 or 2 stream that have not been acknowledged by the
broker.
All QoS 2 messages received from the broker that are not yet acknowledged by
complete.
Good practices of the permanent session:
The client must receive messages from a specified topic while outside of
line. Wishing that the broker queues the messages for the client and the
deliver as soon as the client is back online.
The client has limited resources. Where the broker stores the information of
customer subscription and quickly restore interrupted communication.
The client must resend all QoS 1 and 2 publish messages after
a reconnection.
Clean session:
The client only publishes messages to topics, the client does not need to subscribe to topics.
Does not want the broker to store session information or try again.
transmission of QoS 1 and 2 messages.
The client needs to receive messages that are lost when they are offline.
The broker is the server (you already know this, dude!!)
MQTT node properties: