# Accumulates data in a variable

**URL:** <https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157>\
**Category:** General\
**Created:** [14 April 2021 11:45 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157 "2021-04-14T11:45:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [14 April 2021 11:45 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/1 "2021-04-14T11:45:19Z")

</div>

hello everyone , i want ask if you have any idea about accumulates data in a variable  
i have this flow to accumulate the downtime but the result is NaN  
have a nice day ...  
 ![NaN](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5931134e415ba221f6899ede48864dfdd9b4b682.png)

```auto
[{"id":"760419e7.c6a2d8","type":"inject","z":"f320c581.d9d3d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":190,"y":660,"wires":[["cd36a3a4.f762a"]]},{"id":"8bd3662.3529298","type":"inject","z":"f320c581.d9d3d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":190,"y":760,"wires":[["cd36a3a4.f762a"]]},{"id":"cd1e796d.159688","type":"debug","z":"f320c581.d9d3d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":720,"wires":[]},{"id":"bed8a9da.8bf5c8","type":"function","z":"f320c581.d9d3d8","name":"","func":"\n\nvar stopingT= flow.get(\"stopingT\") || 0;\nvar downtime1= flow.get(\"downtime1\") || 0;\nvar downtime= flow.get(\"downtime\") || 0;\n\nif (msg.payload) {\n // payload is true so save start time\n startTime = new Date()\n context.set(\"startTime\", startTime)\n msg = null // don't send a message\n} else {\n // payload is false so send start and end time\n startTime = context.get(\"startTime\") || 0\n if ( startTime === 0) {\n // no start time recorded so must be first time through so ignore it\n msg = null\n } else {\n var stopTime =new Date()\n var stoping = (stopTime - startTime)/60000\n stoping =Math.round(stoping * 100) / 100\n \n flow.set(\"downtime1\",stoping);\n // to store the data\n if(flow.get(\"downtime1\")!== 0)\n { var a\n \n a = flow.get(\"downtime\") + flow.get(\"downtime1\");\n \n flow.set(\"downtime\",a)\n a=0;\n \n \n }\n stoping = stoping.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})\n // stoping =parseFloat(\"stoping\")\n flow.set(\"stopingT\",stoping);\n msg.payload = startTime.toLocaleString(\"nl-BE\") +\"#\"+stopTime.toLocaleString(\"nl-BE\")+\"#\"+stoping \n // clear start time\n context.set(\"startTime\", 0)\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":720,"wires":[["cd1e796d.159688"]]},{"id":"a18c495e.b03a18","type":"rbe","z":"f320c581.d9d3d8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":530,"y":720,"wires":[["bed8a9da.8bf5c8"]]},{"id":"cd36a3a4.f762a","type":"function","z":"f320c581.d9d3d8","name":"","func":"if(msg.payload === true)\nmsg.payload = false;\nelse \nmsg.payload = true;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":720,"wires":[["a18c495e.b03a18"]]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 April 2021 11:58 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/2 "2021-04-14T11:58:52Z")

</div>

So here is how I suggest you go about debugging this issue.  
In your second function node, after the first three lines add:

```auto
node.warn("1 stopingT=" + stopingT)
node.warn("2 downtime1=" + downtime1)
node.warn("3 downtime=" + downtime)

```

and then add more `node.warn()` statements thru the function so display what the data is at different parts. With this information you should be able to solve or at least narrow down what is happening.

(Also whhen you add a flow, the three back-tics need to be on a seperate line or it won't import. I've editid your post so it will import)

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [14 April 2021 12:08 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/3 "2021-04-14T12:08:47Z")

</div>

thank you for your Time  
i have this output after adding the node.warn

 ![noNaN](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10a808aaf64ae51a29e4e0ac0753a5309cdee2b7.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 April 2021 12:29 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/4 "2021-04-14T12:29:02Z")

</div>

As I said, sprinkle more node.warns in the flow so you can see the past thru the code. This will help `you` to solve your problem.

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [14 April 2021 12:37 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/5 "2021-04-14T12:37:33Z")

</div>

yeah i get that the a variable that get the flows data send a undefined value  
the a variable take the sum like a concatenation of to string

```auto
 a = flow.get("downtime") + flow.get("downtime1");

```

i use the the parseFloat() function but the resultat is the same

```auto
a = parseFloat(flow.get("downtime")) + parseFloat(flow.get("downtime1")); 
a always is undefined
```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 April 2021 13:25 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/6 "2021-04-14T13:25:47Z")

</div>

So where/when do you set the flow variable?

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [14 April 2021 13:29 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/7 "2021-04-14T13:29:03Z")

</div>

the flow variables are setting after the calculation in the same function and the data it's generated manually

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 April 2021 13:42 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/8 "2021-04-14T13:42:43Z")

</div>

Why are you getting the flow variable a second time? You get it at the start then get it again in that calculation?

Has the value changed or is it still undefined? And if it is still undefined, how is the computation going work?

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [14 April 2021 13:47 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/9 "2021-04-14T13:47:38Z")

</div>

in this flow i try to accumulate the down time so i create a variable to help me to store data in a flow variable downtime1 is the difference between tow dates is change after a true and false inject and the down time is the accumulate that i wan't to be changed

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 April 2021 13:56 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/10 "2021-04-14T13:56:04Z")

</div>

if flow.get("downtime") returns a undefined, how do you think parseFloat will work on it?  
What do you do in the very begining of your function when you get it???(hint hint hint)

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [14 April 2021 14:07 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/11 "2021-04-14T14:07:14Z")

</div>

because i set the calculation in the downtime flow variable  
i sheared the flow if you take a 1 min to take a look on it please

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 April 2021 14:14 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/12 "2021-04-14T14:14:57Z")

</div>

I’ll be out for about an hour

In the calculation why don’t you use the variable ‘downtime’ since you have already retrieved it?

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [15 April 2021 08:43 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/13 "2021-04-15T08:43:27Z")

</div>

because it will be change as i try to do

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 April 2021 09:21 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/14 "2021-04-15T09:21:11Z")

</div>

Can you show me where in your `function` node that 'downtime' is changing?

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [15 April 2021 09:30 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/15 "2021-04-15T09:30:05Z")

</div>

this is the last update that i made to the function i have an issue in the first inject the downtime give a NaN .

```auto
var c;
var stopingT= flow.get("stopingT") || 0;
var downtime1= flow.get("downtime1") || 0;
var downtime= flow.get("downtime") || 0;
var a= flow.get("a") || 0;

//flow.set("a",0);
node.warn("1 c=" + c)
node.warn("2 downtime1=" + downtime1)
node.warn("3 downtime=" + downtime)

if (msg.payload) {
    // payload is true so save start time
    startTime = new Date()
    context.set("startTime", startTime)
    msg = null // don't send a message
} else {
    // payload is false so send start and end time
    startTime = context.get("startTime") || 0
    if ( startTime === 0) {
        // no start time recorded so must be first time through so ignore it
        msg = null
    } else {
        var stopTime =new Date()
        var stoping = (stopTime - startTime)/60000
        stoping =Math.round(stoping * 100) / 100
        
         
        var b =stoping; 
        var d = flow.get("downtime");
        c =(b+d)/1;
        flow.set("downtime",c);
        
          stoping = stoping.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})
       // stoping =parseFloat("stoping")
        flow.set("stopingT",stoping);
        msg.payload = startTime.toLocaleString("nl-BE") +"#"+stopTime.toLocaleString("nl-BE")+"#"+stoping  
        // clear start time
        context.set("startTime", 0)

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 April 2021 09:45 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/16 "2021-04-15T09:45:46Z")

</div>

So where in that code do you change the variable 'downtime'? I see you create it on line 4  
`var downtime= flow.get("downtime") || 0;`  
but where do you change it?

Just to let you know, I know why you are having this issue, but I am trying to lead you to discovering the cause yourself. That way you will learn some debugging and will (hopefully) in the future be able to solve problems on your own.

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [15 April 2021 09:52 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/17 "2021-04-15T09:52:50Z")

</div>

thank you for your time  
i load the c variable in this line  
i guess i have to initializes the downtime variable in the first

```
    flow.set("downtime",c);
```

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [15 April 2021 09:57 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/18 "2021-04-15T09:57:29Z")

</div>

i try to use this node but the problem is rest it to zero after one 1 hour for exemple  
`npm install node-red-contrib-totaliser`

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 April 2021 09:57 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/19 "2021-04-15T09:57:39Z")

</div>

> [@Khaledpro](#):
>
> `var downtime= flow.get("downtime") || 0;`

Do you understand what this line of code is doing and why it is good programming practice?

---

<div class="post-metadata">

**Author:** ![Khaledpro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khaledpro/32/38910_2.png) [@Khaledpro](https://discourse.nodered.org/u/Khaledpro)\
**Post date:** [15 April 2021 10:01 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/20 "2021-04-15T10:01:33Z")

</div>

> [@zenofmud](#):
>
> Do you understand what this line of code is doing and why it is good programming practice?

yes it's about create a flow variable and initializes the variable i guess !!!  
but when i store the first data it's give me NaN

[Next page](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157.md?page=2)
