How to use context storage in Node-RED to save data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a node red flow each node can add to modify and even delete your data in very dynamic ways you can easily start to flow with just a timestamp in your message and end up with a very complex collection of objects but what if you need a snapshot of your message from a specific part of the flow you may want to use that data in an entirely separate flow page compare it to another value prevent something from being overwritten or save it to reference sometime in the future one fast easy way to store your data more persistently is using the context stores which i'm going to focus on in this video [Music] to start here's a very simple example to show you what i'm talking about here i can inject my timestamp on the left it appears in my debug pane of course but that's it there's no way for me to reference this value again anytime in the future if i inject again right here on the left i get another time that's a completely different value so what happens if i actually need to save this let's start with the most simple way we can and that's using the pre-installed change node i'll just drag that in drop it onto my wire and change it so that i'm setting a flow variable well instead of setting messenger.payload i'll select this drop-down and i'll make sure i set flow dot some value so in this case let's call it flow.savedtime and what i want to set flow.save time to is that currentmessage.payload so now my payload will be saved under save time and i'll be able to reference it again sometime in the future now we'll need some way of getting that back let's create another debug node and this time we'll set it to inject that flow variable i'll just double click this and instead of getting my message.payload equal to the current timestamp i'm going to refer to that flow.savedtime now when i inject my message.payload it'll hold that new save time and i'll be able to reference it again over here let's make a copy of this debug node and wire that in now i'll be able to save my time at any point and reference it down here but let's first see if i just deploy and try to view my save time before i have actually even defined it if i inject you you can see that it knows it's undefined i've never said what flow.save time is so i don't really have anything to reference now i'll just inject my current timestamp set that flow.save time you can see that's the time that i should get 930 and of course if i inject here there we go now you can see i get this timestamp and it is in fact for 9 30 and 36 seconds this is because it's this exact same number that i'm now referring to in this bottom part of the flow i'm easily able to get it back and it's been saved for me more persistently so now what happens if we need to take this for example into another flow page if i come up here and click add flow at the top here i get this entire new tab in this entire new window here that's separate from flow one if i were to grab my save time and copy these nodes i'll paste them into this second flow tab here and we'll be able to inject and see if we can get our flow save time over here i'll go ahead and deploy but we'll see when i inject on flow save time here it's still undefined even though on floor 1 it actually is defined as that 9 30 and 36 seconds that's because this flow context only stores within this one main page here if i go to a different page i have a second instance of that flow save time so what happens if i want to reference one value across multiple flows across these multiple pages here let's see an example of that i'll make a copy of all of these nodes and we'll set up another example here this time though instead of flow.save time i'm going to set a global value and this one's going to be global.hello world here instead of injecting the timestamp i'll show you that you can also save a string so instead my timestamp being generated here i'm going to define my type as a string and i'll hand in the string hello world exclamation point now when i click done we'll inject hello world save that under global.hello world and i'll be able to read it back here using this global value here getting of course hello world this is going to be a separate value and we'll also make a copy of this on our second tab here so if i make a copy of those nodes come over here to my second tab and paste that in we'll be able to see the values across multiple flows now i'll go ahead and deploy you can see that yep our flow time was never defined and we haven't defined our global.hello world yet but if i come over here and define that value we can see yes hello world did come through to the debug pane and i can also confirm that both in this tab and over here in flow number two as well you can see even just by defining on that one page because it's global.hello world i'm able to view it from this flow as well so you can see how easy it is to define things either limited to just one flow page with this flow dot whatever you want to call it as well as globally with global dot for example hello world now this is just using the basic change node but a lot of the more advanced uses of storing this context data is being able to do more complex calculations let's have a look at a quick example of where you can find some different documentation about how to do this using javascript i'll go to nodered.org and i'll view their documentation site we'll have this linked in the description below there's a lot of good data on their documentation site especially under getting started and the user guide you can see here there's a lot of different pieces of information here especially using the function node and what's of a special interest to us today is this working with context right here you can see here it's going to show you all the different context scopes i you can save it to just a specific node to the flow or global that we've already seen and how to use the different pieces of code there's a lot more detail in here that we need to go into today but you can always come through and read it at your own pleasure i'm going to go ahead and go to using the function node and i wanted to see how to store data you can see that's over here on the left this shows me the basic code that i need so instead of using just the change node i can use the code flow.get and flow.set where i get the name of the value and when i set it i need to get the name of what exactly i'm setting and what value i'm handing in let's create a really basic counter and view that over in node-red i'll just go ahead and copy this whole object all this code so that i can get a really nice easy example right off the bat of course need an inject node to trigger this so i'll just drop one in really quickly that's going to inject a timestamp and that's going to activate this function node here if i double click this function node and paste the text in we can see how this is going to work we're going to get some count which is going to get the context.getcount and this context only exists within this one node so i won't be able to refer to it in any other part of the flow but we'll see an example of that in a second to make sure that we don't try to work with some counts that's undefined we use these two vertical bars to say or if it isn't defined go ahead and initialize it as zero then we just add one save it back under our account in our context store and we'll review it out on message.com so let's put in a debug node just make a copy of that one and we'll make sure we view the complete message object since we want to see message.count not just message.payload so we'll view the complete message object and wire that in we'll see if i clear up my debug pane and deploy every time i inject i'm going to be adding one to this count you can see that right here i'll pin that so we don't have to uh open it up every time now when i inject it again there we go count two and count three but you'll see if i try to inject and let's say i want to refer to that count here i can't inject a context dot count it doesn't exist outside of that node i would need to store it as either flow or global so let's make a change to that and see what that would look like now instead of doing context.get for just this node i want to do flow.get so that i can refer to it in a different part of this flow so flow.get and flow.set i'm saving the value back to the context after i increment it by one and i'll be able to view it down here with this inject node now i'll just inject message.payload equal to flow.count and that value will be able to be read in from other places as well let's again go ahead and make a copy of that debug node so that we can see this in the debug pane over on the right hand side i'll go ahead and deploy and we'll see that initially my flow.count is undefined but as soon as i define it as one and we can even increment it a few times 2 and 3. now i can get that value back 3 anytime as many times as i want it's now saved even though i didn't use a change node i used a function node just with these couple of simple lines of code let's take a look at one more complex example to show you that you can store pretty much anything besides just numbers strings and timestamps i'll go ahead and clear up my debug pane and we'll make a copy of this copy and paste now what i'm going to be doing is instead of injecting a timestamp i am going to inject a payload that's going to be an array so i'll just go into json and i'm going to add an array of strings here so i can have it say something like do comma or comma do not so now i have an array do or do not now i can inject that and i'll be able to save it using either my function node or a change node let's go ahead and use a change node i'll just drag one in from the debug pane over on the left and wired in here instead of setting message.payload i'm going to save something like flow.list so we'll have our flow.list get saved and that's going to be what comes in on our message.payload now this array comes in from message.payload and stored in flow.list and i'll be able to reference it in a different part of the array or a different part of the flow so let's go ahead and inject that flow.list and we'll be able to view it right here in the debug pane before i try to edit it with any functions let's just see and make sure it's actually working i'll go ahead and deploy we can see that the list is not yet defined until i inject here we've set it and there we go we've got this list here now if i inject i can refer to that list again but what's cool is now that i have this list saved i can also add to it so let's make a copy of these nodes and to see what that looks like i'll just go ahead and paste that in and we'll drag it in a debug node now what i'm going to do is i'm going to read in some data and modify it so let's start this one from scratch this time i'll start by doing my list equals two and i'll do flow.get list if it's not defined i will get an empty array and again can just use those two vertical bars as like a or if it's not defined go ahead and use this value in this case an empty array now let's add something to this let's say we have some extra string and it's equal to the value do or do not there is no try now i just want to add this extra string on the end of my list so i can just do my list dot push and i'm going to push the extra string onto the end now i of course need to make sure i save my list back again so that it actually persists this data so i'll do that flow.set flow.set i am handing it into that same saved list and what am i putting in there well i'm putting my list in there because it's the one i've modified we can also do message.payload equals this new list and make sure that we actually return at the end here now you can see i'm getting my saved list that should be do or do not then i'm going to add my extra string push it onto the end of my list and save it so that i can view it in the debug pane i'll go ahead and click done you can see that that's function is going to run and we'll be able to view our list right here so let's clear up the debug pane and deploy we'll try to view our list you can see here it's due or do not it has persistently saved that list so now i'm going to inject add to that list now you can see it says do or do not there is no try i've pushed on that extra item and i can view that and we get all five values that we expect you can store pretty much anything you need whether it's an array an object even a complex object like a weather object something simple like a count or a timestamp or whatever you need the main thing is is that you choose the appropriate context store scope for your needs whether it just needs to exist within one node for example the context within a node for something as simple as a counter or you can store it in flow or global if you need access to it in other parts of your node-red projects i hope you found this helpful and if you have any questions we'll have links for some extra resources down in the description below thanks for watching [Music]
Info
Channel: Opto Video
Views: 23,291
Rating: undefined out of 5
Keywords: automation, opto 22, opto, opto22, automatizacion y control industrial, groov, groov EPIC, groov RIO, opto 22 groov, IoT, IIoT, Opto programming, nodered, node-red, Node-RED, Node-RED Dashboard, dashboard, HMI, UI
Id: j5RqpRvlqsU
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Tue Mar 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.