00:03
One of the best things about Node-RED is
that it has a node designed specifically
00:07
for every task. And then there are external
packages available to install even
00:12
more nodes when you're missing something
that you need. But having so many tools
00:16
at hand can be overwhelming. So in this
video I'm going to show you the top five
00:21
core Nodes that come pre-installed with
Node-RED.
00:28
Now there are some really important core
nodes that I won't be going over in this
00:32
video in particular the inject and debug
nodes which give you the ability to
00:37
control and observe the operations of
Node-RED. if you're unfamiliar with
00:40
those nodes check out the earlier videos in this series. In this video I'll be
00:45
using this groov Edge appliance. It's an
industrially hardened IOT device but has
00:49
Node-RED right out of the box. So let's get
right into it.
00:51
To login I just go to HTTP colon slash
slash my host name and then port 1880.
01:02
since the groov Box uses HTTPS I'll need
to allow the insecure connection and
01:07
then go ahead and log in with my groov
admin credentials. So here we are. Right
01:14
now the only thing in my flow is this
small little example output that I have
01:18
which basically just outputs hello world
automatically once every second. Before I
01:28
start working with this data let's see
the top five core nodes this video will
01:35
focus on. The function, switch, change,
report by exception, and finally the
01:48
email output node. Rather than just
explaining what each of these nodes do
01:53
and why they're useful I'll spend the
rest of this video building up a flow
01:57
that uses all five of them to complete a
relatively simple task. I have this hello
02:01
world data and message.payload that
I want to email to myself once a day. But
02:06
only on weekdays when I'm actually at
work and not on the weekends. Now
02:11
because I only want a message once
everyday I could edit this inject node
02:15
and just change the interval. But you may
not always have control over what your
02:19
input poll rate is. So instead I'm just
going to leave it and try to deal with
02:23
it as is. Firstly I'll need to find out
what day of the week it is. So to do that
02:28
I'll use the first node for my top five:
the function node.
02:35
The function node exposes a block of
code. Here you can enter whatever you
02:40
want as long as it's within the
constraints of JavaScript. If you're
02:44
unfamiliar with that language, there are
plenty of resources online. For example I
02:48
want to know what the JavaScript date
methods are. Here the top result is w3 schools
02:56
they are great resource. So
I'll go ahead and check that out. Here I
03:00
can see there is a get day function that
gives me the weekday as a number at 0
03:04
through 6 which is perfect. To find out
which day is which number I'll just go
03:09
down and read some of the details. Here I
can see that day 0 is Sunday. So I know
03:16
that my weekdays are the numbers 1
through 5. Now we'll head back to Node-RED
03:21
and create a new date object that I
can perform my function on. I'll save the
03:27
result in a new variable called day of
week which will be = d.get.day
03:33
I'll now assign this variable to be
held in the message property message day.
03:40
And then I can go ahead and return the
message. You should note though that any
03:47
JavaScript here is valid so instead of
having to use an intermediate variable
03:51
for things like objects and numbers I
can just go ahead and assign the message
03:55
property message.day to be that new
date object. And then call getDay
04:01
directly. Now when I return the original
message it will still have message.payload
04:07
holding hello world. And then
message.day will hold the number 0
04:11
through 6. Either way of coding the
JavaScript is perfectly fine here. You
04:16
should do whichever you're most
comfortable with
04:18
based on your experience level. Just know
that the function block is extremely
04:22
flexible and powerful.
04:26
Now if I wire this into the debug and
maybe give it an appropriate name. When I
04:38
deploy and enable the debug node, I'll
see hello world coming through every
04:46
single second. But now it also has an
additional property "day" holding the
04:50
number 5 which it should since it's
currently a Friday.
04:56
This is great, but you'll notice to stop
the flow of messages appearing in the
05:00
debug pane I had to go ahead and disable
the debug node. However this function
05:06
node is still churning through that
function call every single second that
05:10
this comes through. I don't really want
the rest of my flow to be running every
05:13
single second, and I certainly don't want
to be emailed that often. So instead what
05:17
I'm going to do is I'm going to use my
next node the report by exception or RBE
05:21
node.The report by exception or RBE node
by default does have the setting I'm
05:30
looking for. But if you can click it you
can see that there are other options to
05:34
ignore the initial value or block it if
it changes too much or too little. You
05:38
can either use direct changes in the
exact value or percent change. Also note
05:45
the "ignore initial value" basically says
that the first value to come in will be
05:49
used to compare to the next value rather
than considered a new value to begin
05:53
with. This is great if you don't want the
code to execute when you first deploy.
05:58
I'll leave it on the first setting but
considering that my payload will always
06:02
be hello world I'll need to be checking
the day property so that I update my
06:06
email when that number changes that will
be the exception that I report.
06:21
Now if I connect this to the debug node
so if I clear the debug pane, enable the
06:31
debug node, then when I deploy only one
message comes through. And the report by
06:38
exception node has dropped all of the
other messages that are coming through
06:41
every second. Now I'm ready to make the
decision as to whether or not this
06:46
number means it's the weekend or not. To
do that I'll use my next node the switch
06:50
node. The switch node routes messages
based on their property values or
07:05
If you double-click the node and select
the comparator drop-down you can see
07:10
that there are conditional operators and
endurance string checks, boolean logic,
07:15
even regex. Below that are some options
for splitting out sequence messages
07:20
based on the index of each individual
message with the option to regroup
07:25
message sequences using some maximum
buffer length. This node has a lot of
07:29
potential for different objects but
right now I'm just checking the integer
07:33
and saving if it's between the numbers
one for Monday and five for Friday. If so
07:40
then it must be a weekday if I wanted I
could also add an additional rule
07:47
to handle the otherwise case because
otherwise it's a weekend and then I
07:53
could have this second output handle the
weekend in a different case the day that
08:00
I'm checking however is not in message.payload. It is in message.day
08:06
I'll give the node an appropriate name
like "weekend" and then I can give the two
08:14
outputs
appropriate labels. Output one is for
08:19
days one through five, Monday through
Friday, the weekdays.And then zero and
08:24
six will go on the second output for the
weekend. If i hook this up to two
08:34
separate debug nodes one for each output
weekday and weekend then when I go into
08:44
debug and deploy I can see that this is
coming out of the top debug node which
08:52
must mean it's a weekday. So everything's
working correctly. Eventually I want the
08:58
endpoint for this data to be my email
account so that I can receive it
09:02
anywhere and it's safe persistently in
the cloud. So for that I have the next
09:06
node: the email output node. Let's check
out the info tab to see how it works.
09:17
It basically says I'll need to assign
the payload to hold the message body
09:21
topic to hold the message subject and at
least message.to to hold one or more
09:27
recipients. Here you can see I could also
use Carbon Copy or Blind Carbon Copy
09:31
recipients. But in this case I won't be.
Another awesome additional feature of
09:36
the email node is its ability to take
attachments. It can even take attachments
09:41
in the form of binary buffers that make
up files. This way you can take things in
09:46
Node-RED and export them off your
device for email storage. Even things
09:50
like CSV files and even images as well. You
may prefer a database like MySQL or
09:56
artificial intelligence software like
Azure for handling your data in an
09:59
industrial application having a core
node that's capable of offloading and
10:03
saving data is absolutely invaluable and
a great way to learn the process. When
10:10
using this node we recommend that you
have a separate email account since in
10:14
the case of Gmail you'll need to go to a
different tab and open
10:16
accounts.google.com
10:21
Make sure you're in the correct account
and then go into your account
10:24
preferences. On the left side you'll see
an option for your apps and you'll need
10:30
to make sure that allow less secure apps
is enabled this is required for Node-RED.
10:35
To have email read and write access for
your gmail account with this option
10:39
enabled head back to Node-RED with that
email and password. Leave secure
10:46
connection and the default port as they
are and you're good to go.
10:50
Also note that you can set a static
email recipient in the email node
10:55
directly but I'll be setting it
dynamically with the message property.
10:59
Now this node is all set up. The one
thing I'll need to do before I can send
11:04
the email is package up that "payload
topic" and "to field" so that the email is
11:08
ready to go. To change the message into
what I need I'll use my final node the
11:15
change node. The change node does exactly
what it sounds like it is used to set
11:27
change delete and move different
properties of a message rather than
11:34
working on the wire logic of the flow
like the switch node the change node
11:38
works on the message.property aspect of
the nodes. This way I can set message.payload
11:46
to be a string like email body but
that's not very interesting or
11:51
descriptive so instead I'm going to use
something called JSON odda
11:56
which is basically query language for
JSON objects. Here I could put string
12:01
literals directly in something like "here
is the report" and then have it attached
12:09
on the message.payload data. You'll note
though that this is also setting the
12:17
message.payload property which is
going to overwrite that hello world that
12:20
I originally started with. Let's say that
that value is a little bit more
12:25
important than just a sentence and I
want to be able to use it on the other
12:28
side after all this is completed and in
order. To do that I'll add another
12:32
property and before I override the
payload I'm going to set a new message
12:37
property "message.backup" and it's
going to hold that message.payload
12:43
"value". Now this change node will back up
message.payload into message.backup
12:50
and then it will copy message to
payload hello world to the back of this
12:55
string here. Here is the report using
JSON ata and put it in the payload
12:59
overriding hello world but that's fine
because it's safe up here. And back up
13:05
the next rule is going to set the email
subject in message.topic because I'm
13:11
going to be getting five of these emails
every week I'll once again use some JSONata
13:14
just to make it a little bit more
readable. Here I can say something like
13:18
daily report number and then I can
attach on the string value by calling a
13:26
small function within JSONata and give
it that message a property you can click.
13:33
These three dots here to get more
editing options
13:37
here you even have the ability to test
your output to make sure it's doing
13:41
everything you expect. You'll note that
it already has a very similar hello
13:46
world package as the payload by default
in this little test section but it
13:50
doesn't have the days so I can go ahead
and add that in here. Now I'll see that
13:55
it's taking daily report and then the
number symbol and assigning the string
13:59
value of the day so I get daily report
number 5 as my message subject. Now the
14:07
last thing I need to do is set the
recipient message.to which will be
14:11
myself opto.dev you should know that
it's perfectly okay to send emails to
14:19
your own address from your address. With
that out of the way I'll hit done, wire
14:26
this in, grab a debug node.
14:39
Now when I hit deploy see the email show
up in the debug pane right here
14:45
and then there we go. It shows up right
on my SmartWatch.And those are the top
14:50
5 core nodes in action. For more check
out opto22.com Thanks