Node-RED Tutorial: Email Alerts & Notifications

Video Statistics and Information

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