Top 5 Node-RED Core Nodes

Video Statistics and Information

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