00:03
Alerts are a crucial part of automation
since you shouldn't have to be actively
00:07
watching your gauges and status lights
to know when something important is
00:10
happening. When you're right next to your
equipment you might have some flashing
00:13
lights or blaring alarms but if you get
too far away or get too distracted you
00:17
have no idea when big events are
happening. To make sure that you're
00:20
always notified about important digital
and analog conditions whether you're
00:25
outside the room the building or even
outside the country you can make a Node-RED
00:29
flow to send yourself emails that
you can see on your phone or SmartWatch
00:33
to know about a SNAP PAC system no
matter where in the world you are. In
00:37
this video I'll show you how to set up
the nodes you'll need to make that
00:46
For this workshop I'll be using a groov
AR1 and the SNAP PAC Learning Center.
00:51
it's basically a SNAP PAC system with a
controller several i/o units and a rack. We
00:57
use it for training here at Opto 22. This
panel simulates some typical i/o signals
01:02
like a meter, potentiometer, a temperature
sensor, LEDs, switches, and a buzzer. The
01:09
controller is running a
convenience-store strategy that's loaded
01:12
into its memory and the groov AR1
box is an industrially hardened
01:16
appliance that's running Node-RED
and groov. There are already some
01:19
special alerts setup; one for when the
emergency alarm is triggered and another
01:25
for when the fuel level is critically
low. Now you don't need these alerts
01:30
setup in PAC control to get Node-RED
alerts but you will need some tags for
01:34
Node-RED to read. So if you want the
convenience store example strategy and
01:38
your own Learning Center check out
workshops.opto22.com. Let's head over to
01:43
PAC Control and look at what the
convenience store strategy gives us. The
01:46
emergency alarm I just triggered is
under i/o units, the SNAP PAC i/o and the
01:52
points list. You can see here it's
emergency with a capital 'E'. I'll make note
01:57
of that and head over to Node-RED. The
first step is to regularly check the
02:02
level of the input so I'm going to drag in
an inject node. And, double click to
02:05
edit it. And, set the repeat interval to
be one second. Click done and drag in a
02:13
SNAP PAC read node. This is what it's
going to scan every second. Double click
02:20
it and hit the pencil icon to add your
new PAC device. If you are using HTTP you
02:27
will need extra instructions from
developer.opto22.com to set up the
02:31
SSL Certificates. I'm just using HTTP
though so I can just go ahead and enter
02:35
my PAC's address. And, then I will need an
API key set for the controller. Go ahead
02:41
and grab the IP address, copy it to your
clipboard and open a new tab.
02:47
Enter in that controller's IP/admin/keys to bring open the
02:53
authentication key management window.
Here you can make a new key for Node-RED
02:57
and give it some password you can
remember. Click Submit and once the
03:04
changes are saved you can go ahead and
enter this key set back into Node-RED.
03:08
Make sure to use the exact same
capitalization as you did in the
03:12
authentication key management window. Add
this controller and now you can select
03:18
your data type which for my emergency
switch is a digital input called
03:22
'Emergency' with capital 'E'. This data will
be read into the msg.payload
03:28
property. Now that I've clicked done I
want to make sure that I don't get an
03:32
email every single second that the level
is checked. So the next node I'm going
03:35
to use is down in the function section
called RBE for rate by exception. I'll
03:42
drag that in and double click to edit it.
And set it to block unless the value
03:47
changes where I ignore the initial value
so that it doesn't send me an email when
03:51
the flow is initially deployed. As well
as ignoring the initial value I'll also bring
03:56
in a switch node so that I can filter
out the full signal. To do this I'll make
04:02
sure that it only accepts the JavaScript
expression value 'true'. What this node
04:09
does now is essentially filters out the
full signal. Now I click done and see
04:17
that the flow only gets through when the
true value is passed otherwise I would
04:21
trigger another email when the switch is
turned off. You can leave out the switch
04:24
node if you do want both signals. To build
the actual email I'll need another node
04:29
from the function section. This one is a
JavaScript function node. So, I'll drag
04:33
that in and double-click it to edit it.
Here I'll create a new message object.
04:40
It's going to have the three properties
that make up the email;
04:45
the first is the payload, second is the
topic, and then after that you can enter
04:54
the recipient fields with to, CC, and BCC.
05:00
The payload is what holds the body of
the email. You can keep it nice and
05:05
generic for now, for example, 'the
emergency alarm has been triggered'. Make
05:11
sure that you've put a comma after each
property to avoid any JavaScript errors.
05:16
The topic is the subject of the email. It
can be a bit more brief so I'm just
05:20
going to title mine 'emergency'. Again make
sure that you have a comma after this
05:24
property. The next fields can take as
many addresses as you would like to
05:28
enter just make sure you separate
further addresses with commas inside the
05:33
quotation box. The CC and BCC fields will
act essentially the same as the to
05:40
field. That will just be sent under those
respective labels. Finally the object is
05:50
ended with a semicolon.
Note that the CC and BCC fields are
05:54
totally optional. In this case I'm
actually not even going to include them.
05:58
The to field doesn't need to be set in
the function node either. You can set the
06:02
main addresses right in the email node.
That's what I'm gonna do so I can erase
06:06
this line for now. Before I leave the
function node I can add a little bit
06:09
more detail to the email body. In my case
I'm going to add the current time string
06:14
so that I know exactly when the email is
sent even if I'm outside of Wi-Fi or
06:17
cell service. Finally I'll return the
message and I can close the node by
06:22
hitting done. Now I can go down to the
social section, drag in an email output
06:29
node and edit it to have the recipients
that I erased a minute ago. Here I'm
06:36
going to send it to myself so I'll use the
Opto alert email. If I wanted to I could
06:40
also add extra recipients here, again
separating addresses with a comma.
06:44
However, just one email will do for this
example.
06:49
The user ID and password field are for
the address that will actually be
06:53
sending the mail. We recommend that you
create a separate email address for
06:57
Node-RED to send from since you will
have to go and open a new tab, bring open
07:03
your account settings, slash less secure
apps, and make sure that 'allow less
07:12
secure apps' is enabled. Once you have
enabled that make sure that you have the
07:19
'use secure connection' box checked. Click
done and now you can wire together the
07:23
flow. Once every second the inject node
we'll check the emergency tag, use the
07:29
RBE node to see if it's changed, filter
out the full signal, and then if it's
07:35
been toggled to true it will write the
email, and finally, send it. If I wanted to
07:42
see the message in Node-RED I could go
back up to the output section, drag in a
07:47
debug node and wire it into the function
node. I'll make sure I double click the
07:55
debug node and set it to output the
'complete message object' to make sure I
08:00
can see the topic and email body. Hit done,
deploy the flow and flip the emergency
08:08
switch to see this in action.
08:12
Great it works! Now, let's move on to
something a little more complicated the
08:17
fuel level alert emails. First I'm gonna
rearrange the flow by just dragging the
08:22
nodes under one another. This will make
things a little bit more neat and
08:26
staggering them keeps the wiring from
getting too convoluted. While I'm moving
08:32
them I'll also take the chance to rename
the function node to say 'build email'.
08:36
That way I can keep track better. Since I
already have an inject node in place I
08:44
just need to know the tags from PAC
Control. So I'll bring open that window
08:47
and look at my other i/o points. Here I
have the fuel level tag that comes in
08:51
from the potentiometer. I can also read
variables into Node-RED so I'll grab the
08:56
numerical variable 'fuel low limit' as
well since that is what determines the
09:01
threshold of my alarm. Now that I know
these
09:03
two things I'll head back to Node-RED
and drag in a new SNAP PAC read node.
09:08
I'll double click this node and give it
the same controller that I got the other
09:12
variable from. In this case though I am
getting the analog input called 'fuel level'.
09:18
Because this is a separate flow path it
is fine for it to also go into msg.payload
09:22
just like emergency. Click
done and drag in another SNAP PAC read
09:27
node to read in the variable 'fuel low
limit'. Once again we'll use the same
09:34
controller only this time I am getting
the float variable 'fuel low limit'. Since
09:42
this is on the same path as fuel level
which has stored a msg.payload
09:45
I'll need a new message property so that
I don't override the fuel level I
09:49
already have stored. To keep things
consistent I'll call this one 'message.
09:54
fuel low limit' This leaves the payload
unaffected. Now I need to check to see if
10:02
the current level is below that limit. To
do that I'll go back down to the
10:08
function section and get another switch
node. Last time I used the switch node as
10:13
a digital filter, but this time I'm going
to use it for analog comparison so I'll
10:17
check to see if the msg.payload
is less than or equal to the
10:23
msg.fuel low limit. this will allow the
message payload through if and only if
10:31
the payload is less than the fuel low
limit. Now click done and you'll want to
10:36
make sure that you don't get an email
every single second that the fuel level
10:39
is low. To do this you can use a delay
node under the function section.
10:48
Double-click it and change it from
delaying each message to rate limit mode.
10:55
Using this you can delay all messages so
that only one message is sent every hour.
11:00
In this case you'll want to go ahead and
drop intermediate messages so that you
11:05
don't get any stale email sent. Click
done and now we can build a new email
11:09
for the fuel low limit. This one will
have essentially the same format as
11:19
before where you create a new message
object and give it the property's
11:22
payload and topic. In this case the email
subject is that the fuel level is low
11:30
and in the payload I can set the message
body to say exactly when the fuel is low.
11:36
And, then also enter what level it is.
11:55
Make sure that you end each property
with a comma and note that here the
11:59
current message payload is the fuel
level and we are repacking that into a
12:03
new payload that will hold the message
body and the message object with a
12:08
semicolon and return the entire object.
12:15
Now click done and see that the same
timestamp checks the current fuel level
12:21
and stores it in message payload, grabs
the fuel low limit and puts it in a new
12:26
message property, checks to see if the
current level is below the threshold,
12:32
limits the rate of the message, and
finally builds the email. Now I can copy
12:39
and paste the old email node using
control-c and control-v.
12:48
Note that when I do this it only retains
the email address I was going to send it
12:52
to and I have to enter my user ID and
password once again. Click done. We can
13:02
also copy over the debug node so that
once again I can see the messages appear
13:06
on the right-hand side. Now with
everything all set up and ready to go
13:15
I can hit deploy, crank down the fuel
level and just as the Learning Center
13:22
starts to chirp I see the email come in
and telling me it's exact level. However
13:26
this doesn't give me a ton of room to
react so I'll quickly go back to
13:30
Node-RED and add another condition to the
switch node. Here I can click the Add
13:36
button to give another condition that
instead of checking a message property
13:40
I'm actually going to check a JavaScript
expression once more. Here I'm going to
13:47
take the message fuel low limit to make
sure that an appropriate email is sent
13:53
out based on the conditions instead of
checking all the rules I'll stop after
13:57
the first match. To make sure that the
highest priority is on a critical email
14:04
I'll put it at the top of the list so
that if that matches firstThat is where
14:07
the email is sent. The next highest limit
is the fuel low limit so that will get
14:12
checked next, and then finally, the
highest limit is the warning limit of
14:15
double the low limit. To keep things
clear I'll also label the ports so the
14:22
first port of 500 gallons is the
critical warning. The second port that
14:29
checks of the low limit from the PAC
Controller is the low limit warning. I
14:34
multiply that number by 2 to give me an
early warning which goes into the third
14:38
port. Now I can click done and rearrange
the flow to make room for the new nodes.
14:48
The nodes I will need is another delay
node and another function node. So I can
14:52
just use control-c and control-v to copy
the ones I already have. Once you have
14:59
these new nodes you'll want to customize
each message body to reflect the level
15:03
of the alert. So on the first email which
is the critical warning I'll want to
15:07
make sure that it says the word 'critical'.
Here I can also set exactly what the
15:14
threshold is inside the email body as
well. So now this node builds my critical
15:25
alert email. I'll click done and do the
same to the next nodes. In the 'build low
15:34
alert function node' I will also include
the message property that tells me what
15:38
the limit is. That way if the limit
changes in PAC Control it shows up in
15:54
Now I click done and finally build the
warning email. Once again recall that the
16:04
warning is at doubled the fuel low
limit.
16:13
And I'll make sure to change the topic
field to put warning in the subject. Now
16:21
I can click done and wire all three
paths together. The critical path goes
16:26
into the first port that I labeled
'critical' and the warning path goes into
16:31
the third port that I labeled a 'warning'.
All three of these paths can go into
16:36
both the debug node and the email node.
16:52
Keep in mind that if you wanted each
message to go to a different address you
16:57
could empty out the two field in the
email node and use the fields in the
17:05
function node that I specified earlier
to, CC and BCC. That way the recipients
17:14
would be unique to which email you sent.
Instead of doing that you can do what
17:18
I'm going to do and just go ahead and
use the field in the email node. That way all
17:25
the emails go to the same place. Now
after I hit done, deploy, and slowly crack
17:34
down the fuel level, I can see all three
messages come in one at a time. Now if
17:42
this was a real-world tank and I was
away from the meter I would still be
17:45
fully aware if the fuel level got low. So
there you have it. Now you know how to
17:50
read various tags from a PAC controller,
make decisions based on either the
17:54
digital or analog values, build an email,
and get it sent out automatically. And, in