Trying to add 3 incoming msg.payload.value - and failing

Looks so easy but failing to do it.
I have 3 nodes sending values, all I am trying to do is adding them up so I end up with a payload of the sum of the 3 msg.payload.value(s)
I thought it was easiest with a function node... problem is the function node sends 3 messages. As I am not sending asyncroneous, I thought it would wait until it has all 3 values... obviously that is not the case
I am also most likely overcomplicating things..

var boiler = context.get('boiler') || 0;
var sink = context.get('sink') || 0;
var micro = context.get('micro') || 0;


var nodeName;
nodeName = msg.nodeName

if (nodeName == "Watts RGBW boiler") {
    boiler = msg.payload.value
}

if (nodeName == "Watts RGBW sink") {
    sink = msg.payload.value
}

if (nodeName == "Watts RGBW micro") {
    micro = msg.payload.value
}

msg.payload.value = sink + boiler + micro

context.set("boiler", 0)
context.set("micro", 0)
context.set("sink", 0)

return msg;

One of the most important premises in node-red is that msgs from different wires travel individually (asynchronously) meaning they will NEVER arrive at the function node at the same time.

You have 2 choices here...

  1. use a join node (details below)
  2. put your nodes in series and move the payload to other parts of the msg object

Option 1:

See this article in the cookbook for an example of how to join messages into one object.


Option 2:

inject -> boiler -> change node (move msg.payload to msg.boiler) -->
sink -> change node (move msg.payload to msg.sink) -->
micro -> change node (move msg.payload to msg.micro) --> Function node
(you can now get msg.boiler and msg.sink and msg.micro in the function node)

OK I thought that might be the case. So if I go the join node way I would have a change node after each input to give them a topic boiler, topic micro etc. then join, then going to the function node.
Or put them all in line with change nodes which seems the easier way. I'll give that a try

If you want to do it in a function something like this should work.

let sync = context.get('sync') || {};
const nodeName = msg.nodeName;
sync[nodeName] = msg.payload.value;
const sync_array = Object.values(sync);

if (sync_array.length >= 3) {
    msg.payload =  sync_array.reduce((acc, num) => acc+num, 0)
    sync = undefined;
}else{
    msg = null;
}

context.set("sync", sync)
return msg;

e.g.

[{"id":"9d658e1986a3ca06","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":1,\"HourDescription\":\"12 am - 1 am\",\"Value\":-0.11487,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":1,\"HourDescription\":\"12 am - 1 am\",\"Value\":16.101,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":2,\"HourDescription\":\"1 am - 2 am\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":2,\"HourDescription\":\"1 am - 2 am\",\"Value\":10.175,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":3,\"HourDescription\":\"2 am - 3 am\",\"Value\":-0.15443,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":3,\"HourDescription\":\"2 am - 3 am\",\"Value\":13.202,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":4,\"HourDescription\":\"3 am - 4 am\",\"Value\":-0.130146,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":4,\"HourDescription\":\"3 am - 4 am\",\"Value\":13.289,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":5,\"HourDescription\":\"4 am - 5 am\",\"Value\":-0.125505,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":5,\"HourDescription\":\"4 am - 5 am\",\"Value\":11.953,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":6,\"HourDescription\":\"5 am - 6 am\",\"Value\":-0.109888,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":6,\"HourDescription\":\"5 am - 6 am\",\"Value\":11.8,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":7,\"HourDescription\":\"6 am - 7 am\",\"Value\":-0.105976,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":7,\"HourDescription\":\"6 am - 7 am\",\"Value\":11.441,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":8,\"HourDescription\":\"7 am - 8 am\",\"Value\":-0.109031,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":8,\"HourDescription\":\"7 am - 8 am\",\"Value\":14.554,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":9,\"HourDescription\":\"8 am - 9 am\",\"Value\":-0.093543,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":9,\"HourDescription\":\"8 am - 9 am\",\"Value\":14.643,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":10,\"HourDescription\":\"9 am - 10 am\",\"Value\":-0.1052,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":10,\"HourDescription\":\"9 am - 10 am\",\"Value\":13.027,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":11,\"HourDescription\":\"10 am - 11 am\",\"Value\":-0.115724,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":11,\"HourDescription\":\"10 am - 11 am\",\"Value\":13.855,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":12,\"HourDescription\":\"11 am - 12 pm\",\"Value\":-0.108498,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":12,\"HourDescription\":\"11 am - 12 pm\",\"Value\":11.367,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":13,\"HourDescription\":\"12 pm - 1 pm\",\"Value\":-0.097682,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":13,\"HourDescription\":\"12 pm - 1 pm\",\"Value\":11.767,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":14,\"HourDescription\":\"1 pm - 2 pm\",\"Value\":-0.094643,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":14,\"HourDescription\":\"1 pm - 2 pm\",\"Value\":12.608,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":15,\"HourDescription\":\"2 pm - 3 pm\",\"Value\":-0.095467,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":15,\"HourDescription\":\"2 pm - 3 pm\",\"Value\":12.206,\"AirQualityCategory\":\"GOOD\",\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":16,\"HourDescription\":\"3 pm - 4 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":16,\"HourDescription\":\"3 pm - 4 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":17,\"HourDescription\":\"4 pm - 5 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":17,\"HourDescription\":\"4 pm - 5 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":18,\"HourDescription\":\"5 pm - 6 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":18,\"HourDescription\":\"5 pm - 6 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":19,\"HourDescription\":\"6 pm - 7 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":19,\"HourDescription\":\"6 pm - 7 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":20,\"HourDescription\":\"7 pm - 8 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":20,\"HourDescription\":\"7 pm - 8 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":21,\"HourDescription\":\"8 pm - 9 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":21,\"HourDescription\":\"8 pm - 9 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":22,\"HourDescription\":\"9 pm - 10 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":22,\"HourDescription\":\"9 pm - 10 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":23,\"HourDescription\":\"10 pm - 11 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":23,\"HourDescription\":\"10 pm - 11 pm\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"CO\",\"ParameterDescription\":\"Carbon monoxide\",\"Units\":\"ppm\",\"UnitsDescription\":\"parts per million\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":24,\"HourDescription\":\"11 pm - 12 am\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null},{\"Site_Id\":1001,\"Parameter\":{\"ParameterCode\":\"PM10\",\"ParameterDescription\":\"PM10\",\"Units\":\"µg/m³\",\"UnitsDescription\":\"microgram per cubic meter\",\"Category\":\"Averages\",\"SubCategory\":\"Hourly\",\"Frequency\":\"Hourly average\"},\"Date\":\"2022-12-01\",\"Hour\":24,\"HourDescription\":\"11 pm - 12 am\",\"Value\":null,\"AirQualityCategory\":null,\"DeterminingPollutant\":null}]","payloadType":"json","x":110,"y":3920,"wires":[["6f3229e0ec292e99","2b3a0fb24ed8d1d9","dc3f93e6dc631268"]]},{"id":"6f3229e0ec292e99","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"2","tot":"num"},{"t":"set","p":"nodeName","pt":"msg","to":"Watts RGBW boiler","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":3860,"wires":[["5801347964bfdbb2"]]},{"id":"2b3a0fb24ed8d1d9","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"2","tot":"num"},{"t":"set","p":"nodeName","pt":"msg","to":"Watts RGBW sink","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":3920,"wires":[["5801347964bfdbb2"]]},{"id":"dc3f93e6dc631268","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"2","tot":"num"},{"t":"set","p":"nodeName","pt":"msg","to":"Watts RGBW micro","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":3980,"wires":[["5801347964bfdbb2"]]},{"id":"5801347964bfdbb2","type":"function","z":"452103ea51141731","name":"function 18","func":"let sync = context.get('sync') || {};\nconst nodeName = msg.nodeName;\nsync[nodeName] = msg.payload.value;\nconst sync_array = Object.values(sync);\n\nif (sync_array.length >= 3) {\n    msg.payload =  sync_array.reduce((acc, num) => acc+num, 0)\n    sync = undefined;\n}else{\n    msg = null;\n}\n\ncontext.set(\"sync\", sync)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":3920,"wires":[["265550587332c873"]]},{"id":"265550587332c873","type":"debug","z":"452103ea51141731","name":"debug 111","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":3920,"wires":[]}]

Thats not working. This is my flow now

with the function node

var boiler = context.get('boiler') || 0;
var sink = context.get('sink') || 0;
var micro = context.get('micro') || 0;

var nodeName;
nodeName = msg.nodeName

if (nodeName == "Watts RGBW boiler") {
    boiler = msg.boiler
}

if (nodeName == "Watts RGBW sink") {
    sink = msg.sink
}

if (nodeName == "Watts RGBW micro") {
    micro = msg.micro
}

msg = sink + boiler + micro

context.set("boiler", 0)
context.set("micro", 0)
context.set("sink", 0)

return msg;

If you moved the props to where I suggested then it should be...

msg.payload = msg.sink + msg.boiler + msg.micro
return msg

I tried this, I left the change nodes as the nodeName is coming from my 'input' nodes anyway. Its now sending the sum of all the payload values as I wanted.
Now I have to do some reading to understand what it does. I think I understand most, what I dont get (yet) is

msg.payload =  sync_array.reduce((acc, num) => acc+num, 0)

@ Steve-Mcl

I will still try your way too

Reduce iterates over an array and adds the element value (num) to acc, at end acc is sent to msg.payload. The 0 is the initial value of acc

OK that makes sense now. So the => is part of the .reduce function syntax, I thought is was greater or equal

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.