Storing data into variables | Node-RED Essentials | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to talk about an essential part of node-red and that's using variables if you're new to node-red then check out some of my other guides like my beginner's guide to node red but otherwise stay tuned so we're going to talk about how to store data into variables how to retrieve data from variables and also how to persist that data over node red restarts it's also going to include different contacts so whether that's local or node context whether that's flow context or the global context so let's get started [Applause] there are two main ways of using variables in node-red and that's with the change node and the function node so firstly we're going to take a look at the change node okay so we're in node-red and I've created a new flow tab called variables so now what we're going to do is we're going to add an inject node and then if we double click into that we can see here we can set message.payload to anything so at the moment it's set to timestamp by default but we've got flow and Global and they're the variables that we're interested in so basically when you define a flow variable that means it's available with anything in this tab in this flow and if we do Global then it's available through any of the types Within node-red now let's add a change node and now we're going to add a debug node so the first thing I'm going to do is I'm going to set a variable to have a certain value so if we connect all these up and if we go into this change node you can see that you can set any variables so we want to set a flow variable so if we call it flow DOT test and we're going to set the value just as a string at the moment and say yes and then into the debug now we want to see the whole message so we change this to complete message so now that I've deployed this we'll press this button and you can see a message has come through with payload of time but that's not what we're interested in we're interested in this flow.test variable so what we can do is if we go up to the menu up here and you see this drop down there's context data and then this will show you everything that's in the variables so if I refresh this flow you can see this variable called test and yes and here what you can do is you can press refresh to see the latest value or you can press delete on the right hand side or you can copy the value now to prove that that variable has actually been stored correctly we're going to do another part of the flow we're going to copy this inject node and reuse that and we're going to reuse another debug node and then if we double click into the inject node instead of setting message.payload to a timestamp we're going to set it to flow.test which is our variable then in theory this should be usable now within this flow so if I connect this up to the debug node debug 3 then we should see that it contains the text yes so let's give that a go we're going to go up here to debug messages clear these messages out deploy and then press this and you can see here that it's got yes as the payload so it's successfully stored it now that we've covered the basics with the change node saving string data we're now going to look at dictionaries where you can save multiple pieces of data into one object this is useful because it means that you don't have to have lots of different variables for related things so to save things into a dictionary is very similar so let's open up the change node and make a couple of changes so if you click here this is the data type of the thing that you want to store in the variables at the moment it's set to string but you can set it to number Boolean so true or false or you can set it to Json so this is what we're going to do we're going to have it as a Json object where we can store multiple things in one variable so this is how you structure addiction read to store multiple pieces of information so we've got text one which is storing hello world and text 2 which you're storing hello subscribers you can also press format Json here and it will format it in the way that looks a bit nicer let's press done done again and now let's try that so now if we press that to save it and go back to context data and then let's refresh this variable you can see here now that it's got an object and it's got two pieces of information text one and text two that you can select separately so we can use those separately in this other part of the flow here so instead of flow DOT test we could do flow.test.txt1 so by putting the dot notation here means that you can access things within the object so let's try that and see if it just extracts hello world let's go back to the debug messages deploy and here we go we can see that the payload has got hello world in it only so we've managed to save multiple things within an object but then extract certain pieces out now that we've gone through a change load example I could take you through a function node example but I think instead it's worth going through the persistent storage and that's because you might never have a need to use a function node whereas storing variable data across different restarts of node-red is probably going to be quite useful we just need to make a couple of changes in the config file and restart node-red okay great so we're storing some data into variables and then we're accessing it later on in the flow what happens if node red restarts then we're going to lose all that data under context data this will all get blanked out so what we need to do is is we need to actually Amanda configuration file of node red itself now I'm using the node red add-on for home assistant so I can easily access the configuration file it's called settings.js if you've got a separate node-red instance and you'll have to find that yourself so I'm using visual studio code to access the file and you can see here settings.js in home assistance instance it's under the config folder so you've got config node-red settings.js and then what you need to do is you either need to find a section called context storage and you might need to add to it or if it doesn't exist at all you need to add to it so I'll add in the description exactly what you add but it's fairly simple you can see it's just this here so this is basically creating a context and saying I want to store it on the local file system rather than just in memory so once you've saved this file you are going to need to restart node-red and then after that you should be able to save data and it will persist across restarts one thing to note is that these changes are not stored automatically unfortunately they're only stored to disk every 30 seconds so if you are changing variables quite often then just note that the latest data in those variables might not be accurate during a restart if you're changing them a lot whereas if you're just saving them every now and again then it's likely that this is going to be very useful to you so assuming that you can now persist data across restarts of node-red you're possibly going to need to sometimes delete stuff out those variables so what we can do is is we can use this change node for that as well so instead of set you can actually use delete so if I copy this node here you can see at the moment we've still got data in here let's just refresh that and make sure yeah and copy these now what I'm going to do is I'm going to change this to delete and then say delete flow.test so it should delete everything about those variables let's deploy and press this and then refresh over here and you can see test is now undefined so there's nothing stored in that variable anymore so now let's put together what we've learned so far so what I've done here is I've created another flow and I've also created a dummy entity in home assistant just so that we can toggle this switch on and off and then in node-red it's going to look for this state change so we're using the state change node here and all it simply does is we've got a change node and it sets global.homeassistancewitch to the payload and the payload in this state will be the state of the switch so every time the switch state gets changed in home assistant then it should update This Global variable with the state of the switch and note that this time we've used Global rather than flow because what I'm going to do is once we've saved it in this variable is we're going to access it from this other flow that we had before so what I've created here is is I've got an inject node but with an inject node you can set it to run at an interval so every 10 seconds and then I've got a debug node which is going to Output the result every 10 seconds so you can see in this inject node I've set the payload to be global.home assistance switch which is the variable from the other tab now all we need to do is enable this debug here by pressing that and then it should start putting messages in here so there you go you can see at the moment it's showing payload is off and the switch is off if I change this to on and then we wait a few seconds and then it should send a message of payload on there we go so hopefully now you know how to use variables across the different flows so now we're going to take a look at the function node we can store data into variables and retrieve it from variables just like the change node but instead we use some JavaScript to do it I personally don't use this very often because I like using change nodes because it makes more sense in the flow and it's easier to see what's going on but there's certainly a lot of situations where it might make more sense to store it in a function node so let's take a look okay so now on to the function example so let's drag in the function node and we want an inject node again just to kick it off and a debug node link them all up and then let's add some information to here so if we take our changelode example where we set flow DOT test then we can do the same in the function node and it's quite simple to do so we just need to do flow dot set and then the name of the variable so in this case test and then the value we want to store now that's all we're going to add and we're going to run it and see if it stores it into that variable now if we go back to the context data again and refresh this and you can see test has got hello world in it let's just change that and prove that it is working and there we go so to demonstrate retrieving data from variables using the function node we're going to copy and paste this function node and add it as the next node open it up and instead of set we're going to do flow dot get now of course we need to do something with this data that we've retrieved from this variable so what I'm going to do is I'm going to store it into a local variable within this function so what I've done here is I've retrieved information from this variable I've then saved it to message.t and that means then in the message there should be a variable called T which will have that information in so let's deploy this run it go back to the debug messages and you can now see within the message object we've got a key value power of T and the hello world from the test variable you can of course do the same with global variables as well so instead of flow we just change it to Global and now you can see here we've also got a global variable called test with that datron as well and that's separate to the flow variable so the final thing to talk about is What's called the local or the node context so I've done an example here already so at the top here you can see that under node there's no variables and what I've done is there's another similar flow with the function node now if I double click into this you can see I've defined a variable called Al and I'm getting a context variable called local VAR so a context variable is only available within this node if the context variable hasn't been set then it sets it to a hello so that's what these two pipes are for here and then the next line says append something to the L variable so store whatever's in now already and then append hello again at the end and then we're going to set the context variable local VAR to whatever is in L so now if I press this inject and then refresh this we need to select the node first then you can see it's put hello hello again and then if I press it again so we run the flow again basically then you should see that it updates and adds another hello again and that will keep doing that there's obviously more useful use cases for this I to be honest it's another thing I don't really use very often myself but there's definitely use cases where this could be very handy there is an example in the documentation where it shows using this as a counter to know how many times this function node has been run well hopefully this video has given you all the information you need for storing data and retrieving data from node-red and if you want to learn more about node-red then check out some of my other videos I've got a node red playlist well that's it for today so please consider subscribing and liking the video If you enjoyed it and thanks until next time [Applause] [Music] thank you [Music]
Info
Channel: Let's Automate
Views: 3,350
Rating: undefined out of 5
Keywords: node red, nodered, variables, node red variables, node red context
Id: IpwihQf6Kqo
Channel Id: undefined
Length: 13min 19sec (799 seconds)
Published: Sat Aug 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.