# Timingproblem in Calculation?

**URL:** <https://discourse.nodered.org/t/timingproblem-in-calculation/36504>\
**Category:** Dashboard\
**Created:** [25 November 2020 09:33 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504 "2020-11-25T09:33:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [25 November 2020 09:33 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/1 "2020-11-25T09:33:33Z")

</div>

Hello,

I have something like this:

 ![timing](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8d324ae9e39c7af5fc44476dacc4711eca7c13d.png)

Its a simple addtion every step.

But if i use the flow like in the screen, only every second step has an addition.

I tried to give y back as global and wired.

If i use a function to simulate the Databaseoutput with an incrementing number, i get the addition every step.

Is it possible that there is a timing problem with that circle ?

Greetings  
Chorum

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 November 2020 09:36 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/2 "2020-11-25T09:36:53Z")

</div>

It's hard to say for sure without more specific details on what each step is doing - such as whether it is asynchronous or synchronous etc.

Have you tried adding any debug to the flow to see what is happening at each step?

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [25 November 2020 09:49 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/3 "2020-11-25T09:49:41Z")

</div>

yes, i followed the value the whole way.

in the function node with the calculation i see it very clearly. i setup a template node to watch the added values and the result in live.

the second thing what can happen is that i get alternating values on the result. like that:

10  
8  
11  
9  
12  
10

its only a sum from 2 values. one from DB & one from flow.

if i exchange the DB value with an incrementing number made in the same funktion node, everything works fine. only one sum and that every step.

so i think the problem isnt my calculation.

i think i will build up a minimal flow to check this out...

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [25 November 2020 10:54 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/4 "2020-11-25T10:54:58Z")

</div>

Ok i set up a minimal example.

only a function node working with globals.

Code:

```auto
var summand1 = global.get('W16Zaehlerstandout'); // comes from Database
var summand2 = global.get('W16Steptime');
var result;

result = (summand1 + (summand2/1000)).toFixed(0);
global.set('Bhmini',result); //goes in to database

msg.payload.summand1 = summand1;
msg.payload.summand2 = summand2;
msg.payload.result = result;
return msg;

```

the problem stays... only every second step a addition to see in result

a second try:

```auto
var summand1 = global.get('W16Zaehlerstandout'); // comes from Database
var summand2 = global.get('W16Steptime');
var result;

var z1 = context.get('z1')||0;
var z1alt = context.get('z1alt')||0;

if (z1 == z1alt){
    z1++;
    z1alt = z1;
}else{
    

}

context.set("z1",z1) ;
context.set("z1alt",z1alt) ;

result = (z1 + (summand2/1000)).toFixed(0);
global.set('Bhmini',result); //goes in to database

msg.payload.summand1 = summand1;
msg.payload.summand2 = summand2;
msg.payload.result = result;
return msg;

```

z1 instead summand1 in the addition.

everything works fine...

so it seems that the way to the database and back has a problem...

do i have to think about node runtimes ?

---

<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:** [25 November 2020 11:04 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/5 "2020-11-25T11:04:12Z")

</div>

> [@Chorum](#):
>
> so it seems that the way to the database and back has a problem

What database node are you using? I know MSSQL-PLUS gives you a result you can use to check how many rows were modified.

are you certain database is actually getting updated?  
are you certain database is actually getting re-read properly?

Also, it may be a flaw or bug in your real flow (you havent shared the actual flow only psuedo flows so we cant check)

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [25 November 2020 11:17 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/6 "2020-11-25T11:17:47Z")

</div>

the test flow is only a the function node with that code in it.  
i exchanged summand1 with a fixed "1", so i only have summand2 as a dynamic value.

i use a sql Database

What i can see in the values is, that i send the correct value with the query but the value from the database is falling one step back.

maybe i cant write and read at the same time.  
i will try 2 database nodes parallel.

---

<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:** [25 November 2020 11:21 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/7 "2020-11-25T11:21:05Z")

</div>

> [@Chorum](#):
>
> i use a sql Database

That doesnt really help! There are LOTS of SQL databases and even more SQL nodes!

  
  

> [@Chorum](#):
>
> maybe i cant write and read at the same time.  
> i will try 2 database nodes parallel.

That info would have been useful upfront. Thats why i said ...

> [@Steve-Mcl](#):
>
> Also, it may be a flaw or bug in your real flow (you havent shared the actual flow only psuedo flows so we cant check)

  
  

> [@Chorum](#):
>
> i will try 2 database nodes parallel.

You mean in series right? as in `[prepare data]--> [WRITE SQL] --> [READ SQL] --> [next node]`

---

<div class="post-metadata">

**Author:** ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)\
**Post date:** [25 November 2020 12:22 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/8 "2020-11-25T12:22:55Z")

</div>

I only know its a mysql database...

The node to connect to the database is: node-red-node-mysql

The flow is simply to big to post it, so i use small examples.

the read and the write query come at the same moment (parallel) to 1 sql node

[prepare data WRITE] --\>[WRITE SQL]  
[prepare data READ] --\>[READ SQL]--\> [Data to prepare data WRITE]

mhmmm ok i try to make that in series....

---

<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:** [25 December 2020 12:23 UTC](https://discourse.nodered.org/t/timingproblem-in-calculation/36504/9 "2020-12-25T12:23:05Z")

</div>

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