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
  • ХоббиХобби

Комментарии • 49

  • @willo300
    @willo300 Год назад

    I'm a beginner to Node Red and wondered how variables are implemented. Great guide and extremely useful 👍

  • @the_tux
    @the_tux Год назад

    Great explenation. Simple, to the point and no yakety-yaking along. Somewhere YT still seems to have good content...

  • @d_sellers1
    @d_sellers1 3 года назад

    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.

  • @inbarajananbu8649
    @inbarajananbu8649 5 лет назад

    Well explained.. First time am seeing someone explaining about the scope in detail.. Very useful 👍

  • @erikjorpes1325
    @erikjorpes1325 6 лет назад

    Useful. Thanks for your guides related Node Red, Mosquitto and others.

  • @jibinjosemathewjibinjosema7617
    @jibinjosemathewjibinjosema7617 4 года назад +1

    I am extremely thankful to you Sir, you explained everything very well and in detail...

  • @stsmith1975
    @stsmith1975 4 года назад

    Excellent videos Steve. Many thanks

  • @lfmtube
    @lfmtube 4 года назад

    Thank you very much. Great video and very instructive!

  • @alainjacob925
    @alainjacob925 4 года назад

    Hi Steve, many thanks for your explanation.

  • @doubando
    @doubando 2 года назад

    Thank you sir, very informative and exactly what Ive been looking for

  • @TheHellis
    @TheHellis 3 года назад

    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.

  • @timfries398
    @timfries398 5 лет назад

    very well done, thanks!

  • @douglas491
    @douglas491 4 года назад

    Amazing explanation. Thank you.

  • @agsonethetube
    @agsonethetube 3 года назад

    Your videos are a great help

  • @tarunarya1780
    @tarunarya1780 2 года назад

    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.

  • @leonsteiner8412
    @leonsteiner8412 Год назад

    ne explained , better than my teacher

  • @enricorighetto3302
    @enricorighetto3302 5 лет назад

    Grazie molto chiaro, very well explained.

  • @uweri_3851
    @uweri_3851 6 лет назад +2

    very well explained. Hope we get much more videos from you.

  • @rjh8888
    @rjh8888 4 года назад

    Thank you.

  • @marceloap140
    @marceloap140 4 года назад

    THANKS.

  • @francisco-chaves
    @francisco-chaves 5 лет назад

    very good

  • @frankrubben8206
    @frankrubben8206 6 лет назад

    tnx

  • @arifkarisman1016
    @arifkarisman1016 4 года назад +1

    Thank you mr.. soory mr.. how but method reset user object , with flow and global reset ???..

  • @succubiuseisspin3707
    @succubiuseisspin3707 3 года назад

    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 ? :-)

  • @humbertolagomarsino8107
    @humbertolagomarsino8107 4 года назад

    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)

  • @johnwilson3918
    @johnwilson3918 3 года назад

    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).

    • @johnwilson3918
      @johnwilson3918 3 года назад

      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.

  • @michaelmcnaughton1535
    @michaelmcnaughton1535 6 лет назад

    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.

    • @stevecope
      @stevecope  6 лет назад +1

      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

    • @stevecope
      @stevecope  6 лет назад +1

      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

    • @michaelmcnaughton1535
      @michaelmcnaughton1535 6 лет назад

      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.

  • @guicmbh
    @guicmbh 5 лет назад

    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

    • @stevecope
      @stevecope  5 лет назад

      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?

    • @guicmbh
      @guicmbh 5 лет назад

      @@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?

    • @stevecope
      @stevecope  5 лет назад

      @@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?

  • @TheJrahhal
    @TheJrahhal 4 года назад

    I was wondering if you show how the reset function works?

    • @stevecope
      @stevecope  4 года назад +1

      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

    • @TheJrahhal
      @TheJrahhal 4 года назад

      @@stevecope awesome, thank you!

  • @stevmoon
    @stevmoon 6 лет назад

    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.

    • @stevecope
      @stevecope  6 лет назад +1

      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

    • @stevmoon
      @stevmoon 6 лет назад

      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)

    • @stevecope
      @stevecope  6 лет назад

      Not to worry. I've had problems with even fewer lines of code.

  • @gnakfaskdm
    @gnakfaskdm 4 года назад

    Is there a way to store arrays in this way?

    • @stevecope
      @stevecope  4 года назад

      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.

  • @BenDover-pq8xj
    @BenDover-pq8xj 5 лет назад +1

    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.

  • @timjansen7349
    @timjansen7349 4 года назад

    How to store data in a variable that is also kept after a node red restart?

    • @stevecope
      @stevecope  4 года назад +1

      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

    • @timjansen7349
      @timjansen7349 4 года назад

      @@stevecope thanks it worked :D

  • @neal1236
    @neal1236 5 лет назад

    ‘ ’ means char; " " means string, count is a string ,why you use 'count'? Sorry,my English is bad.