Node-Red Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the function is a powerful node it allows you to write JavaScript functions so we're gonna go ahead and set this up here by default the function is set up to just pass the message through it so it's returning the message let's go ahead and view the entire message object now when we click you'll see the message object just goes right through let's go ahead and change up what we sinned let's go ahead and do a string we're going to read let's go ahead and do a couple of these so we have different messages coming into our function the first thing we want to do is get to the message dot payload so we're gonna do let's just make a variable called M and it's going to be MSG that payload and then if we want the topic we can also do make a variable we'll call it t equals message topic okay now what else should we do here to move things along I'm gonna go ahead and just paste in an if statement so you can see that we're gonna say if the message payload is read we're gonna return the actual topic that was passed to it and we changed the payload otherwise if it is black we're gonna go ahead and modify both of the topic in the payload and we can give this thing a name let's call it custom function deploy so if we put if we click blue nothing happens because we weren't gonna pass anything unless it was red or black if it's red you can see that we went ahead and passed the topic through as it was didn't modify it whatsoever but we didn't change the payload and then if it was black we modified both the topic and the payload let's do something different here I'm gonna add another function remember that the time stamp is a UNIX timestamp so when we click we see this a not a readable timestamp so in a function we can simply use JavaScript to go ahead and build a better timestamp so this is standard Java JavaScript stuff we are gonna go ahead and make a new date and then we're getting the month date year so forth we're joining them all together and then we're going to return the message and right here we're setting the message dot payload to this formatted date/time stamp let's go ahead and deploy end we have a nice date time stamp and then one more thing often in functions you're going to not only get to the message top payload but what if you need to get to different values in an array so let's go ahead and edit one of these and instead of just doing a single value we're gonna do a JSON string and inside of here let's do blue and then the value will be something and so if we look at the entire message object let's go ahead and debug this here and we see that inside of here we have an object and we have both blue and orange if you want to get to one of those you could simply come in here so let's go ahead and just get message that payload dot blue and we're going to return topic we're going to call it blue and the payload will be now when we click we can see that the topic was blue the payload was 34 we go ahead and switch this back to the message dot payload so it looks nicer okay when we click our inject node we see that the message object has blue and orange where are we just getting a blue out of here but what if we want to get both of them we can do that in a single function we're gonna do two outputs we're gonna go ahead and get the blue and the orange we go ahead and build two messages and then we turn we return both messages and they can be returned to different outputs deploy we see that we have orange and blue I'll separate it out there are some lots of other things a function can do but this kind of gets you started
Info
Channel: FreeWave Technologies
Views: 42,696
Rating: undefined out of 5
Keywords: Free-Wave, functions, tutorial
Id: 7BxkS5iSE0Q
Channel Id: undefined
Length: 6min 55sec (415 seconds)
Published: Fri Oct 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.