I am trying to create a flow for my weather station where it waits for a number of inputs from MQTT before it builds a SQL statement and inserts the data into a database.
I just cannot seem to get it to wait for all the inputs to have arrived before it triggers.
I have tried to create a simple flow which uses 2 inject nodes with different topics with a debug node to report the output.
But, as soon as it has received one of the messages the function node outputs...
This is the code in the function node:
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;
}
This is the code for the nodes:
[{"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":[]}]
I cannot see what I am doing wrong. Apologies if it's obvious :)
In such case it is a way simpler just to use a join node configured as below:
Test the flow attached to see how it works:
[{"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"]]}]