# How to hold data in node function

**URL:** <https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027>\
**Category:** General\
**Created:** [5 July 2021 02:23 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027 "2021-07-05T02:23:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PNWw](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@PNWw](https://discourse.nodered.org/u/PNWw)\
**Post date:** [5 July 2021 02:23 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/1 "2021-07-05T02:23:04Z")

</div>

Hi  
In my project, I have sensor that can send 2 data. Height and Profile number  
height when have object it have value but don't have object it = 0  
Profile number it counted values indefinitely when start sensor.  
I want to hold profile number first value when height \> 0 and last value when height = 0.

I'm sorry for my beginner English language.  
PNWw

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 July 2021 04:41 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/2 "2021-07-05T04:41:57Z")

</div>

You want to learn about `context`.

There are 3 _types_ of context:  
1 - node  
2 - flow  
3 - global

node is local to that node  
flow is local to that flow/tab  
global is visible to all nodes in all flows/tabs.

Here is a basic run down on how to use it:

Say you want to store a value coming in as the default `msg.payload`

```auto
let value = msg.payload;
context.set("name",value);

```

Now: value isn't a number. I am just not good at using/finding the right terms. But it _gets_ what is the `msg.payload`.

`"name"` - again: bad word - is what ever you want to call it.  
There are some _rules_ how to name them and which case they are.  
eg: `Name``name``'MyName` and `myname`  
All different. So be careful.

To get that value to use later on:

```auto
let something = context.get("name");

```

Now `something` is what the `msg.payload` was.

Hope that helps.

But reading what you say I am not sure this is going to help.

I read it as you get a message which has 2 parts: `Height` and `Profile number`  
So it may not be as simple as simply getting `msg.payload`. But that's a whole other story.

Good luck.

---

<div class="post-metadata">

**Author:** ![PNWw](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@PNWw](https://discourse.nodered.org/u/PNWw)\
**Post date:** [5 July 2021 04:57 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/3 "2021-07-05T04:57:17Z")

</div>

Thank you for reply.

 ![Screenshot (18)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34835f46c458dd5d0485b3df1ed546ab795823a7.png)

I don't know method. Array[0] is Height value and array[1] is Profile number when array[0] \> 0 I want hold or keep array[1] as default and when array[0] = 0 again I want hold or keep array[1] is the last value.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 July 2021 05:06 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/4 "2021-07-05T05:06:40Z")

</div>

Ok, _we_ are going to have to teach you how to paste things rather than screen shots.  
As good as they are, when it comes to picking apart data: not very good.

On the right side of the screen there is the `debug` window where you are seeing the data/outputs.

Each _part_ (line/section) has _options_.  
In the top right corner there is an option to copy the entire message to the clipboard.  
You will see this when you move the mouse over it you will see a small icon of a clipboard appear and a little pop up text something about "Copy to clipboard".

Click it then you can paste it here.

![Screenshot from 2021-07-05 15-07-06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b3ccc9bcba58b7f18afbfcd2455cd16cc5b1b3b0.png)

But it isn't \*_THAT_ simple.

Before you paste it, look at the top of the "window" where you will be typing and there are a few buttons.

One of them looks like this: `</>`

Click that button then paste the code where the text is.

That will preserve any special stuff which may be needed.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 July 2021 07:13 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/6 "2021-07-05T07:13:37Z")

</div>

Hi, it is not 100% clear what you mean by "I want to hold profile number first value when height \> 0 and last value when height = 0." but here is my best guess...

```javascript
let parts = msg.payload.split("\t");
if(parts.length >= 2) {
  let height = Number(parts[0]);
  let profileNo = Number(parts[1]);
  if(height == 0) {
    flow.set("height_0_profileNo", profileNo);
  }
  let height_not_0_first_time__profileNo = flow.get("height_not_0_first_time__profileNo");
  if(height > 0 && height_not_0_profileNo == null) {
    flow.set("height_not_0_first_time__profileNo", profileNo)
  }
}

```

Then you can access `flow.height_not_0_first_time__profileNo` and `flow.height_0_profileNo` using a change node or switch node or anywhere access to `flow` context is accessible.

---

<div class="post-metadata">

**Author:** ![PNWw](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@PNWw](https://discourse.nodered.org/u/PNWw)\
**Post date:** [5 July 2021 07:45 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/7 "2021-07-05T07:45:44Z")

</div>

{"payload":["0","628681",""],"fromip":"169.254.191.182:8000","ip":"169.254.191.182","port":8000,"\_msgid":"38885dfa.30e802"}

This is my value.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 July 2021 07:46 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/8 "2021-07-05T07:46:35Z")

</div>

Close

you need to use that button I mentioned.

The text will look different when you post it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 September 2021 07:47 UTC](https://discourse.nodered.org/t/how-to-hold-data-in-node-function/48027/9 "2021-09-03T07:47:00Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
