Node-Red 函数等待 2 个输入
Node-Red Function Wait for 2 Inputs
我正在尝试为我的气象站创建一个流,它在构建 SQL 语句并将数据插入数据库之前等待来自 MQTT 的大量输入。
我似乎无法让它在触发之前等待所有输入都到达。
我尝试创建一个简单的流程,它使用 2 个具有不同主题的注入节点和一个调试节点来报告输出。
但是,一旦它收到一条消息,功能节点就会输出...
这是函数节点中的代码:
context.data = context.data || {};
switch (msg.topic) {
case "WeatherStation1/BME280/Trev1":
context.data.insidetemperature = msg.payload;
msg = null;
break;
case "WeatherStation1/BME280/Trev2":
context.data.insidehumidity = msg.payload;
msg = null;
break;
default:
msg = null;
break;
}
if(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {
msg2 = new Object();
msg2 = context.data;
context.data = null;
return msg2;
}
这是节点的代码:
[{"id":"e61a1536.2ea238","type":"inject","z":"fa655376.2f5ea","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":540,"wires":[["e57cbc71.f5cce"]]},{"id":"e57cbc71.f5cce","type":"function","z":"fa655376.2f5ea","name":"Wait for Trev Sensors","func":"context.data = context.data || {};\n\nswitch (msg.topic) {\n case \"WeatherStation1/BME280/Trev1\":\n context.data.insidetemperature = msg.payload;\n msg = null;\n break;\n case \"WeatherStation1/BME280/Trev2\":\n context.data.insidehumidity = msg.payload;\n msg = null;\n break;\n default:\n msg = null;\n \tbreak;\n}\n\nif(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {\n\tmsg2 = new Object();\n msg2 = context.data;\n context.data = null;\n\treturn msg2;\n}","outputs":1,"noerr":0,"x":480,"y":580,"wires":[["1f0b9ac2.57f2a5"]]},{"id":"fc5b276d.4f9b18","type":"inject","z":"fa655376.2f5ea","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":620,"wires":[["e57cbc71.f5cce"]]},{"id":"1f0b9ac2.57f2a5","type":"debug","z":"fa655376.2f5ea","name":"Trev Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":720,"y":580,"wires":[]}]
我看不出我做错了什么。抱歉,如果很明显:)
在这种情况下,使用如下配置的连接节点更简单:
测试附加的流程以查看其工作原理:
[{"id":"bd70b6f3.d327c8","type":"tab","label":"Join Node","disabled":false,"info":""},{"id":"caa58f4e.316c8","type":"join","z":"bd70b6f3.d327c8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":200,"wires":[["b82136ef.53d8e8"]]},{"id":"85340c81.7988b","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":120,"wires":[["caa58f4e.316c8"]]},{"id":"fccfc295.2527a","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":240,"wires":[["caa58f4e.316c8"]]},{"id":"b82136ef.53d8e8","type":"debug","z":"bd70b6f3.d327c8","name":"Trev Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":620,"y":200,"wires":[]},{"id":"92af39.d98c10c8","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":160,"wires":[["caa58f4e.316c8"]]},{"id":"21b2e29d.fac10e","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["caa58f4e.316c8"]]}]
检查你的 if 子句 -
if(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {
...
如果它进入该逻辑,则两者都已设置,您很可能缺少重置它们的逻辑。尽管看到 context.data
我有点惊讶,因为我通常希望看到类似
的东西
context.get(..)
或 context.global.get(...)
或 global.get(..)
或 flow.get(..)
我正在尝试为我的气象站创建一个流,它在构建 SQL 语句并将数据插入数据库之前等待来自 MQTT 的大量输入。
我似乎无法让它在触发之前等待所有输入都到达。
我尝试创建一个简单的流程,它使用 2 个具有不同主题的注入节点和一个调试节点来报告输出。
但是,一旦它收到一条消息,功能节点就会输出...
这是函数节点中的代码:
context.data = context.data || {};
switch (msg.topic) {
case "WeatherStation1/BME280/Trev1":
context.data.insidetemperature = msg.payload;
msg = null;
break;
case "WeatherStation1/BME280/Trev2":
context.data.insidehumidity = msg.payload;
msg = null;
break;
default:
msg = null;
break;
}
if(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {
msg2 = new Object();
msg2 = context.data;
context.data = null;
return msg2;
}
这是节点的代码:
[{"id":"e61a1536.2ea238","type":"inject","z":"fa655376.2f5ea","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":540,"wires":[["e57cbc71.f5cce"]]},{"id":"e57cbc71.f5cce","type":"function","z":"fa655376.2f5ea","name":"Wait for Trev Sensors","func":"context.data = context.data || {};\n\nswitch (msg.topic) {\n case \"WeatherStation1/BME280/Trev1\":\n context.data.insidetemperature = msg.payload;\n msg = null;\n break;\n case \"WeatherStation1/BME280/Trev2\":\n context.data.insidehumidity = msg.payload;\n msg = null;\n break;\n default:\n msg = null;\n \tbreak;\n}\n\nif(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {\n\tmsg2 = new Object();\n msg2 = context.data;\n context.data = null;\n\treturn msg2;\n}","outputs":1,"noerr":0,"x":480,"y":580,"wires":[["1f0b9ac2.57f2a5"]]},{"id":"fc5b276d.4f9b18","type":"inject","z":"fa655376.2f5ea","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":620,"wires":[["e57cbc71.f5cce"]]},{"id":"1f0b9ac2.57f2a5","type":"debug","z":"fa655376.2f5ea","name":"Trev Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":720,"y":580,"wires":[]}]
我看不出我做错了什么。抱歉,如果很明显:)
在这种情况下,使用如下配置的连接节点更简单:
测试附加的流程以查看其工作原理:
[{"id":"bd70b6f3.d327c8","type":"tab","label":"Join Node","disabled":false,"info":""},{"id":"caa58f4e.316c8","type":"join","z":"bd70b6f3.d327c8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":200,"wires":[["b82136ef.53d8e8"]]},{"id":"85340c81.7988b","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":120,"wires":[["caa58f4e.316c8"]]},{"id":"fccfc295.2527a","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":240,"wires":[["caa58f4e.316c8"]]},{"id":"b82136ef.53d8e8","type":"debug","z":"bd70b6f3.d327c8","name":"Trev Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":620,"y":200,"wires":[]},{"id":"92af39.d98c10c8","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":160,"wires":[["caa58f4e.316c8"]]},{"id":"21b2e29d.fac10e","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["caa58f4e.316c8"]]}]
检查你的 if 子句 -
if(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {
...
如果它进入该逻辑,则两者都已设置,您很可能缺少重置它们的逻辑。尽管看到 context.data
我有点惊讶,因为我通常希望看到类似
context.get(..)
或 context.global.get(...)
或 global.get(..)
或 flow.get(..)