Accumulates data in a variable

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

[{"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"]]}]

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

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)

1 Like

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

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.

1 Like

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

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

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

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

So where/when do you set the flow variable?

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

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?

1 Like

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

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)

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

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?

because it will be change as i try to do

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

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

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)

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.

1 Like

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

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

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