# 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:** 11\
**Page:** 2

<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 10:04 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/21 "2021-04-15T10:04:14Z")

</div>

So show me where in your code AND how many times do you get that 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:** [15 April 2021 10:09 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/22 "2021-04-15T10:09:44Z")

</div>

when i get the stopingtime i want to accumulate it in the down time variable so i get the last stopingtime and the last value of the down time  
that should be in the first time = a zero  
and sum them and restore it to downtime flow variable

> [@Khaledpro](#):
>
> ```auto
> var b =stoping; 
> var d = flow.get("downtime");
> c =(b+d)/1;
> flow.set("downtime",c);
> 
> ```

---

<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 10:12 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/23 "2021-04-15T10:12:22Z")

</div>

> [@Khaledpro](#):
>
> ```auto
> var b =stoping; 
> var d = flow.get("downtime"); <======== HERE
> c =(b+d)/1;
> flow.set("downtime",c);
> 
> ```

what does flow.get("downtime") contain at this (see arrow) point in the code?

---

<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:14 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/24 "2021-04-15T10:14:03Z")

</div>

get the last downtime value stored  
i think generally

---

<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 10:18 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/25 "2021-04-15T10:18:05Z")

</div>

but what and when have you stored a value???

---

<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:21 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/26 "2021-04-15T10:21:29Z")

</div>

this is the issue i want in the first time to store a 0 in the downtime and for the rest of the day accumulate the stooping time when i store a 0 in the begin the script always give the downtime 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 10:22 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/27 "2021-04-15T10:22:43Z")

</div>

What do you notice that is different in the two places you get downtime?

---

<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:26 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/28 "2021-04-15T10:26:27Z")

</div>

i don't know  
or i still don't understand the logic of javascript or if i need to use a while or do while

---

<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 10:48 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/29 "2021-04-15T10:48:07Z")

</div>

It seems to me you need to take a tutorial about javascript - I suggest looking at [JavaScript Tutorial](https://www.w3schools.com/js/DEFAULT.asp)

Lets look at the original version of the `function` node from your first post:

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

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
        
        flow.set("downtime1",stoping);
    // to store the data
        if(flow.get("downtime1")!== 0)
        { var a
        
           a = flow.get("downtime") + flow.get("downtime1");
           
            flow.set("downtime",a)
        a=0;
            
            
        }
        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)
    }
}
return msg;

```

In line 3 you have  
`var downtime= flow.get("downtime") || 0;`  
which says "if the flow variable "downtime' contains something, put it in the local variable 'downtime' otherwise put 0 in the local variable 'downtime'. So far so good!

Further down you have this code:

```auto
    // to store the data
        if(flow.get("downtime1")!== 0)
        { var a
           a = flow.get("downtime") + flow.get("downtime1");
           flow.set("downtime",a)
           a=0;
        }

```

So if flow.get("downtime1" is not zero, you execute the following code. Note: since you set a local variable `downtime1` in line 2 you should code this line as `if (downtime1 !== 0)'

So you create a variable 'a' and `try` to set it to the flow variables `downtime` and `downtime1` BUT if you have never set those variables, they might contain nothing and cause a NaN error to occur!

had you coded that line as: `a = downtime + downtime1;' using the two local variables you setup on lines 2 and 3, you would never have hit the NaN issue.

All this tells me you should stop using the `function` node until you have taken a Javascript tutorial.

---

<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 11:03 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/30 "2021-04-15T11:03:22Z")

</div>

@zenofmud thank you for your time SIR yes i will take the tutorial that you give me yes it's my first experience with javascript  
sorry .

---

<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:** [29 April 2021 11:03 UTC](https://discourse.nodered.org/t/accumulates-data-in-a-variable/44157/31 "2021-04-29T11:03:54Z")

</div>

Closed on 29 April 2021 11:03 UTC

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

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