Accumulates data in a variable

So show me where in your code AND how many times do you get that flow variable.

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

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

get the last downtime value stored
i think generally

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

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

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

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

It seems to me you need to take a tutorial about javascript - I suggest looking at JavaScript Tutorial

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

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:

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

3 Likes

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

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