title: How to do Sync in Node-RED?
body: <p>In my current project, we are trying to develop a simple application "Notify me when my room temperature goes beyond the certain limit". The flow diagram of this application as follow: <a href="https://i.stack.imgur.com/PgH4s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PgH4s.png" alt="enter image description here"></a></p>
  
  <p>Here, temperature sensor is producing temperature data <strong>periodically</strong> and the sensed data is send to the <code>CalculateAvgTemp</code> component. The <code>CalculateAvgTemp</code> component waits for 5 samples and then pass the calculated value to the <code>DisplayTempController</code>. The code of <code>CalculateAvgTemp</code> component, written in <strong>node.js</strong> is as follows.</p>
  
  <pre><code>var mqtt=require('mqtt');
  var client=mqtt.connect('mqtt://test.mosquitto.org:1883');
  var NUM_SAMPLE_FOR_AVG=5;
  var numSample=0;
  var tempCelcius=0;
  var currentAvg=0;
  client.subscribe('sensorMeasurement');
  client.on('message',function(topic,payload){
  
  sensorMeasurement=JSON.parse(payload);
  console.log(sensorMeasurement);
  
  if(numSample &lt;= NUM_SAMPLE_FOR_AVG){
       numSample = numSample + 1;
  
      if(sensorMeasurement.unitofMeasurement=='F'){
          tempCelcius=((sensorMeasurement.tempValue-32)*(5/9));       
      }
      else{
          tempCelcius=sensorMeasurement.tempValue;
  
      }       
  
      currentAvg=parseFloat(currentAvg)+parseFloat(tempCelcius);
  
      if(numSample==NUM_SAMPLE_FOR_AVG){
          //console.log(currentAvg);
          currentAvg=currentAvg/NUM_SAMPLE_FOR_AVG;
          var avgTemp={"avgTemp":parseFloat(currentAvg),"unitofMeasurement":sensorMeasurement.unitofMeasurement};
          client.publish('roomAvgTemp',JSON.stringify(avgTemp));
           numSample =0;
           currentAvg=0;
      }
  }       
  </code></pre>
  
  <p>});</p>
  
  <p>We are trying to implement the <code>calculateAvgTemp</code> component in <strong>Node-RED</strong>. Node-RED provide <strong>function node</strong> that allows developers to write custom function. Now, the real problem starts here --- Node-RED custom function does not implement the <code>calculateAvgTemp</code> functionality correctly. It does not wait for 5 temperature values and fires whenever the temperature arrives. My question is that - Is this Node-RED limitation OR should we implement the <code>calcuateAvgTemp</code> functionality differently in Node-RED, if yes then - how can we implement the functionality in Node-RED?
  The code of <code>CalculateAvgTemp</code>written in <strong>Node-RED</strong> is as follows:</p>
  
  <pre><code>context.temps = context.temps || [];
  sensorMeasurement=JSON.parse(msg.payload);
  var tempValue=parseFloat(sensorMeasurement.tempValue);
  context.temps.push(tempValue);
  if (context.temps.length &gt; 4) {
  if (context.temps.length == 6) {
  //throw last value away
  context.temps.shift();
  }
  var avg=0;
  for (var i=0; i&lt;=context.temps.length; i++) {
  avg += context.temps[i];
  }
  avg = avg/5;
  msg.payload = parseFloat(avg);
  return msg;
  } else {
  return null;
  }
  </code></pre>