Search code examples
functionmathcomparenode-red

How to compare numeric values in the function node in node-red?


I've been trying to do a simple numeric comparison since days in my function node but I really don't have any idea why it's not working. I have a function node which accepts two values. I've even converted it from object to Number but still the comparison won't work. Please find the full flow here:

[{"id":"39421a3d.5cda36","type":"function","z":"251d0ac6.958a36","name":"getL1MagneticCount","func":"msg.payload = {\"getCarCount1\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"x":586.6666259765625,"y":606.6666259765625,"wires":[["31136d74.228fb2"]]},{"id":"a171070a.1ba198","type":"function","z":"251d0ac6.958a36","name":"getL2MagneticCount","func":"msg.payload = {\"getCarCount2\":msg.payload.Car};\nreturn msg;","outputs":1,"noerr":0,"x":586.6666259765625,"y":719.9999732971191,"wires":[["31136d74.228fb2"]]},{"id":"31136d74.228fb2","type":"function","z":"251d0ac6.958a36","name":"comparison","func":"var count1 = Number(msg.payload.getCarCount1);\nvar count2 = Number(msg.payload.getCarCount2);\n\nif(count1 >= count2){\n    console.log(\"In\");\n    msg.payload = msg.payload.getCarCount1;\n    return [msg,null];\n    \n} else {\n    console.log(\"Out\");\n    msg.payload = msg.payload.getCarCount2;\n    return [null,msg];\n    \n}","outputs":"2","noerr":0,"x":824.4443950653076,"y":663.3333148956299,"wires":[["57c8e7b7.c948e8"],["10b4a39f.16338c"]]},{"id":"57c8e7b7.c948e8","type":"debug","z":"251d0ac6.958a36","name":"","active":true,"console":"false","complete":"payload","x":1025.5556182861328,"y":626.6666140556335,"wires":[]},{"id":"10b4a39f.16338c","type":"debug","z":"251d0ac6.958a36","name":"","active":true,"console":"false","complete":"false","x":1028.8889236450195,"y":709.9999084472656,"wires":[]},{"id":"1a6938ca.0d2bf7","type":"inject","z":"251d0ac6.958a36","name":"","topic":"","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"x":256.6666679382324,"y":605.555606842041,"wires":[["39421a3d.5cda36"]]},{"id":"d23e60e5.adb83","type":"inject","z":"251d0ac6.958a36","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"x":254.66665649414062,"y":719.5555419921875,"wires":[["a171070a.1ba198"]]}]

Please tell me where my mistake is. Thank you very much.


Solution

  • The problem is that each of the input messages are handled as independent events in the function node, so you only ever have 1 value to compare each time a message arrives.

    What you need to do is make use of the context to store values between each message. Something like this:

    //get stored values if present
    var count1 = context.get("count1");
    var count2 = context.get("count2");
    
    if (msg.payload.hasOwnProperty("getCarCount1")) {
      count1 = msg.payload.getCarCount1;
      context.set("count1", count1);
    }
    
    if (msg.payload.hasOwnProperty("getCarCount2")) {
      count2 = msg.payload.getCarCount2;
      context.set("count2", count2);
    }
    
    if (count1 != undefined && count2 != undefined) {
      if(count1 >= count2){
        console.log("In");
        msg.payload = count1;
        return [msg,null];
      } else {
        console.log("Out");
        msg.payload = count2;
        return [null,msg];
      }
    }