How to Store Data In Node Red Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I initially on to talk about working with node-red variables now in an earlier video we talked about the message object and we saw how it was used to pass information between between nodes the problem with the message object is that each time an event takes place you get a new message object and the old message object gets replaced so how do we store data in node read well node read provides three mechanisms one is the context object which stores data for a node so only a single node the flow object which stores data for the flow which is a collection of nodes and the global object which stores data for the canvas which is a collection of flows and also a collection of nodes and we're going to look at all three methods now the process of retrieving and storing data is to use the get method for retrieving the value and the set method for storing the value so here we're looking at the context object so to retrieve data from the context object we use the get method and to store data from the context of each of the context objects we use the set method and when we look at the flow object and we look at the global object they both have get methods and set methods now a variable stored for function one in the concept context object is non available function two and vice-versa and in our first low we can to to demonstrate that okay to illustrate the context object and the flow object and the global object I'm going to use a collection of flows and I've already created them here and the simple flows they consist of an inject node which is used to inject a message and we're going to inject the on message and that injects into a function node which modifies the message now what I'm going to get the function node to do is actually count the number of times messages actually been passed into this function so we're just going to implement a simple message counter throughout this illustration and then we're going to pass the message on into the debug node Omega displayed over here so we could see what's happened every to remember every time I press this button here it injects a message it's actually just on into this function is function modifies or does something on the message passes on to the debug node that we displayed over on the the right hand side and now what I want to illustrate first is the fact that the context object only stores the data for a function so when I actually create a storage counter in function 1 it's not available to function two and vice-versa so let's have a look at the the functions so this is function 1 and the first line of it in is common to most of the flops and most of all of the function not quite identical but very very similar it says take the variable count in Assen and assign it to the variable count that we've got stored in the context object so we're using the get method to get the variable now if this doesn't exist then we'll assign it the value of 0 that's all that's doing then we'll take the counter we'll increment it and then we create a message payload this is something to print out on the right hand side and we're going to include the count in as part of the payload so we can see the counter and then we're going to store the counter back into the context object and then we're going to return the message which will display it now function - its identical to function 1 except at this time I'm just going to put F 2 in there as opposed to F 1 so we can actually see which function is actually printing out and what I want to illustrate with the this first demo is that the data we save in the context of context object is only available to a function so that the counter here in function 1 is actually a different counter in a function - even though it's got the same name so if you just get back to function 2 we can see counter name is count and we're getting the count from this variable count stored in the context object so they're using identical variables so let's deploy it let's see what happens when I eat checked a message so press the button and out on this right-hand side f1 on message the counter is 1 press it again the character is to press it again the counter is 3 now let's go on to this one here press the inject node no function to counter is 1 no it's not 4 it's 1 press it again counter is 2 so you can clearly see here that the counter stored in function 1 is different than the kind of stored in function 2 even though it's using the same name okay that Illustrated using a single variable and I just go back to function 1 we can see here we had a variable count and we were storing it as a single variable count here now we can actually store multiple variables and to do that we just create a second one and we store it in a second variable name as part of the context object there and I've just modified this script here slightly serves again it's retrieving both variables it's storing both variables further down this time I'm sorry I'm incrementing count 2 by 2 rather than Y 1 otherwise it's the same so done and let's just click on there to inject and we see cat 1 is 1 can choose to do it again 2 and 4 as we'd expect now we can also store an object so this is useful if you want to store lots of variables rather than creating individual variable names we can store it as part of an object and this one does that the opening is a little bit different we don't set it to 0 this time we set it to an empty object so local here is not a variable simple variable it's actually an object variable and we test if the the count variable is part of all cat properties part of the local variable and it's not if it's not we set it to 0 other than that's very similar to the first script and we're just gonna print it out and we're gonna store it back in as part of the context object and we're done so so to test it we just click here and we get the counters one click it again and it's - and again an edge three which is what we expect so it works storing data in an object rather than a single variable so now let's move on to the flow object and the idea here now is that we're going to share a counter between two nodes and we can share the counter between any node within this this flow which is flow one so very similar to what we've seen before but this time I'm using the flow object here and again I'm using the same opening line here we retrieve the counter if it's there otherwise we set it to 0 and then we increment the counter we printed out and then we store the counter back again and the counter is in an object called or a variable called count that's that done and if we look at the other function this time we're incrementing it by two but we're using the same count count variable should I say and again we printing it out and we're storing it back again right let's reset it and let's try it so 5 is 1 F 5 is 2 there when we click on this one here with the f6 is 4 and if we click on this one here now F 5 is 5 so these are clearly using the same F 5 is 6 now if we click this one it's 8 these are clearly using the same counter so the cancer is being shared between these two nodes and it will be shared by other nodes within the same flow this will reset then again there so no what happens if we've got several flows we've got to flow here and we've got to flow here flow flow 1 and flow 2 so now I've been on the straight sharing variables between to flow different flows and we've gone here a global function and we can see here at this time we're using the global object we're also using the flow object because I'm going to show you that the flow object is a naturally taken across flows whereas the global object is so we do the same thing we create a counter in the global one and we create a counter in the flow one and we store them there and we print both of them out now let's just give it a try first click on here and then we can see the flow is set to nine now it's set to nine because we were just using it here if I just click on this one again you can see the flow one should and it's just flow six but if I go back to this one here you can see now it's 12 because remember the flow object is shared with all the between all the nodes in the flow in this counter here is part of the same flow okay our global counter is set to zero and if I show you the function again you can see that the we don't increment the counter there what I do is I increment the counter in the the second flow so if we go over here now and we look at the function the function looks the same as what we had before we got the flow we've got the global and this time I increment the global count I also increment the flow okay so remember a flow count was 12 and Oklahoma count was zero so let's click on that and you see a flow count is 1 and our global count is 5 flown count is 1 because it's a different flow beats flow 2 and not flow on and the flow object isn't carried across two flows and if we go back to flow 1 now remember the global is set to 5 if we click this one again we can see this time it's five and the flow object here is 13 it is not one and let's go back to flow two again and we do it again and this time we Global ten and flow two and go back to flew one and click it again and this time we got global ten and flow fourteen so you can see there the global counter is carried across flows whereas the flow counter isn't carried across flows it's only it's restricted to the nodes within the flow that's the context object the flow object and the global object being used to store variables within node-red then that brings us to the end of the video if you've got a comment there you can leave them below if you like the video then you can use the like button below and if you'd like to be notified when I publish more videos you can always subscribe to the channel the flows i'll make available on the downloads page of the website and i'll put a link to the written tutorial which cut with accompanies this on the website you can find other links to other related tutorials as well so until next time good bye
Info
Channel: Steve Cope
Views: 83,113
Rating: undefined out of 5
Keywords: stevesinternetguide, node-red, data, store, variables, global, flow, context, tutorial, how to
Id: kNfP1vw2Img
Channel Id: undefined
Length: 12min 23sec (743 seconds)
Published: Thu Mar 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.