How to Store Data In Node Red Variables
HTML-код
- Опубликовано: 16 июл 2024
- www.steves-internet-guide.com /node-red-variables It is often necessary to store data in a flow. Node-red provides several mechanisms for storing data. In this video tutorial I will show you how to use the context, flow and global objects to store data in node-red flows.
Tutorial for this video
stevesnoderedguide.com/node-r...
Flows in used in video
stevesnoderedguide.com/downlo...
Related videos
Node-Red Variables (Context data)- Part 2
Covers changes in version 0.19 which lets you store variables in the file system
• Node-Red Variables (Co...
The Node-Red Message Object for Beginners
• The Node-Red Message O...
------------
Create a Node Red Basic Flow - MQTT Subscribe Example
• Quick Introduction to ...
---------------------------------
How to Create a Node-Red MQTT Dashboard
• How to Create a Node-R...
----------
MQTT Publish and Subscribe Using Node Red
• MQTT Publish and Subsc...
Have a question Use the comments or if you want help then use
stevesnoderedguide.com/ask-st...
If you find these videos useful then you might want to consider buying me a coffee
www.paypal.me/StepenCope Хобби
I'm a beginner to Node Red and wondered how variables are implemented. Great guide and extremely useful 👍
Great explenation. Simple, to the point and no yakety-yaking along. Somewhere YT still seems to have good content...
Very well made video. I found your webpage first and read over it. This video did an excellent job of demonstrating everything you mentioned in the text. With the information you gave, I should be able to improve my automations.
Well explained.. First time am seeing someone explaining about the scope in detail.. Very useful 👍
Useful. Thanks for your guides related Node Red, Mosquitto and others.
I am extremely thankful to you Sir, you explained everything very well and in detail...
Excellent videos Steve. Many thanks
Thank you very much. Great video and very instructive!
Hi Steve, many thanks for your explanation.
Thank you sir, very informative and exactly what Ive been looking for
This makes it so much easier to set all variables that is used in one flow.
You just set everything in a function node and with an inject.
Then just trigger a read to keep everything in memory.
Wow.. This really made my flows much easier to maintain.
very well done, thanks!
Amazing explanation. Thank you.
Your videos are a great help
Thanks. I was going to use files to store data from another of your videos till I saw this. As a newbie I had to figure out how to get the debug to show the msg.payload.
ne explained , better than my teacher
Grazie molto chiaro, very well explained.
very well explained. Hope we get much more videos from you.
Thank you.
THANKS.
very good
tnx
Thank you mr.. soory mr.. how but method reset user object , with flow and global reset ???..
Hi! Thanks a lot for the video!
I am trying to store an array of objects in a global variable with
„var myarray=global.get('myarray') || [];“
and then push objects into it
„myarray.push({name:msg.deviceName,status:msg.payload});“
Afterwarts write the array back with „global.set('myarray',myarray);“
But as soon as the first object gets into the array the push() methods seems to stop working. No new elements are added.
Any idea by chance ? :-)
Hi, i need your help PLEASE.
i want to define a flow node to add some html code inside it and then use in a template node. I have this but isnt working
--o--
flow.set("flowVariable","text");
var obtainData= flow.get("flowVariable");
msg.payload = obtainData;
return msg;
{{{flow.flowVariable}}}
(this work but only with a tiny html code, i want to add a large html code, like and so on)
Thank you. Is there any 'init' process (perhaps by way of using an 'init-flow' javascript node) which is automatically executed/triggered, allowing me to initialise flow variables?
I wanted to set up a clean way of initialising flow constants (like HTTP URLs).
Ah! This is embarrassing. I've just watched your initialising node-red video. I can see you can use an auto 'inject' node to set flow variables.
Is there a method of programmaticaly changing the target file name of a file node. I would like to use a flag in my incoming data stream to trigger the name change. When the flag indicates, I would like the flow to produce a new target file name and supply it to the File node for future data streaming.
I haven't found it. It is on my list to try and modify the node so that you can do that as I've a need for that as well.
Rgds
Steve
Correction I just found an answer on aGoogle group.
Leave the filename in the file node empty
The in the function feeding the file node use
msg.filename=""your filename";
I tried it and it works
Steve Cope Thanks for the excellent advice. I put in a simple user function node to build the file name and put it into a flow context. Elementary from there.
Thanks Steve, i don't understand this line of code: context.set('count',count); Why do you have to type Count twice? What is the difference between one and the other? When doing context.get you only did count one in between ' ', which in my understanding means a counter is natively available in the context object. Then you created a variable count, and that's what you also declared in the context.set, but why? Thanks
with the get we used var count=context.get('count')
the count in quotes is the count stored in the context object.
When we store it back ''count' is the value in the context object and count is the variable we are using in the function node.
Perhaps if I had used
var c=context.get('count')
context.set('count',c)
it would make more sense
Is that OK?
@@stevecope Thanks Steve, i understand one is the variable and the 'count' in quotes is the actual counter natively part of the context object. But why did you have to include the variable in the context.set in line 4 is my doubt.
Line 1 - you create a variable from getting the counter of the context object
Line 2 - you increment that variable with 1 unit
Line 3 - you modify the msg.payload
Line 4 - You store the counter back to the 'count' of the context object, but why also need to add the variable? I have tested without the variable and it does not work, but i don't understand this. Is it because you are setting the 'count' to have the same value as the variable? Is it mandatory to indicate where the value to be set in 'count' should be coming from? And that is how you did it? Could you also simply put a literal for testing purpose?
@@guicmbh Because teh variable had or could have changed.
You could also do
context.set('count',1)
to store 1 in the context variable count.
think of it as the same as
var count=1
Does that make sense?
I was wondering if you show how the reset function works?
Hi
The reset functions are there to reset the flow and global counters to 0 or in the case of objects to {}.They aren't reset on a redeploy
@@stevecope awesome, thank you!
I'm having trouble. I'm not sure if its sytax or what but I can't seem to get it to work.
I have a function to grab a numeric from the dashboard:
var timer = msg.topic *1000;
global.set('ontime', timer);
return msg;
An another to add the it to the msg object msg.delay:
msg.delay = global.get('ontime'); //msg.delay = global.get('ontime') || 0;
return msg;
As is I get undefined in my msg.delay debug and if I use the commented version I get "0".
It seem straight forward, but wont work for me... I even try the old method with no luck.
Hi
Use the ask-steve page on the site and send me the code for both functions and I'll take a look
www.steves-internet-guide.com/ask-steve
I was confused by the first line
var timer = msg.topic *1000;
I would have expected msg.payload
Thanks for the quick response. lol YES that was the problem. Such a simple error, of coarse my output was undefined, msg.topic is a string. This was the most trouble I've ever had with 5 lines of code, and after finding my error I feel embarrassed for my post.(first time using global/flow/context/ variables in Node Red)
Not to worry. I've had problems with even fewer lines of code.
Is there a way to store arrays in this way?
Yes.
var data=[1,2];
flow.set("data".data);
same with objects. I almost always uses objects when there is more than one variable too store.
6:28 in Totally confused - where is 'data' defined ? - 'local' is created by the var and then trying to assign 'data' to it but 'data not already defined anywhere, well, that I can see ?????
also, for the very old time 'C' programmers, well hobbyist bitkickers from the early days, is a Node-Red "context" var the equiv to a 'C' "static" var ????, also are 'context' vars only relavent/usable in a function node ?.
I have used your tutorial for Node Red Steve, as just over a year ago I desperately needed to get websockets going in a project, what a task that was but if it was not for one of your tutorial then I would have never done it, so many thanks for that one and keep up the good work, judging by all the other comments here it seems everyone else has got it so for sure it must be me, but I just cant get what I'm missing. Thks.
How to store data in a variable that is also kept after a node red restart?
Tim is is possible since a release 0.19. They are stored in a variable and copied to the disc every 30secs see www.steves-internet-guide.com/node-red-variables/ (at end) for more details
@@stevecope thanks it worked :D
‘ ’ means char; " " means string, count is a string ,why you use 'count'? Sorry,my English is bad.