如何比较节点红色中的函数节点中的数值?

时间:2017-02-27 16:11:15

标签: function math compare node-red

我从功能节点开始尝试进行简单的数字比较,但我真的不知道它为什么不起作用。我有一个函数节点,它接受两个值。我甚至将它从对象转换为数字但仍然比较不起作用。请在此处找到完整的流程:

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

请告诉我我的错误在哪里。非常感谢你。

2 个答案:

答案 0 :(得分:1)

问题是每个输入消息都作为函数节点中的独立事件处理,因此每次消息到达时,您只能有1个值进行比较。

您需要做的是使用 context 在每条消息之间存储值。像这样:

//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];
  }
}

答案 1 :(得分:0)

Picture of diagram with and-gate operator (block) 我将全局变量保存到不同传感器的起始温度和湿度数据中。在一个块中,我检查这些变量是否为"," nan"然后条件..也许它会有所帮助。