# Require X number of same messages before sending along

**URL:** https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969
**Category:** General
**Created:** [30 December 2022 21:23 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969 "2022-12-30T21:23:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dth/32/72723_2.png) [@dth](https://discourse.nodered.org/u/dth)
#### Post date: [30 December 2022 21:23 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969/1 "2022-12-30T21:23:14Z")

</div>

I have been unable to find an elegant and concise way of collecting multiple messages with the same value into one. I have a nagging sense that I am missing something obvious 🙂

For example a node that evaluates to either true or false, and then another node that only passes on a "true" message after receiving for example 10 "true" messages in a row. One "false" message will reset the count.

Thanks 🙂

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 December 2022 21:46 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969/2 "2022-12-30T21:46:17Z")

</div>

Not sure one standard node would do this, there maybe a contrib node.

You can do ut with 3 standard nodes.  
e.g.

```auto
[{"id":"f823969bdae166b3","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":150,"y":1940,"wires":[["0dde7af39bb21306"]]},{"id":"0dde7af39bb21306","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"$$.payload = false ? true","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":1900,"wires":[["badfca9947d35894"]]},{"id":"e1e8d7cc1dc239af","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":150,"y":1900,"wires":[["0dde7af39bb21306"]]},{"id":"badfca9947d35894","type":"join","z":"da8a6ef0b3c9a5c8","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"10","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":1900,"wires":[["5052c5d60c998a0a"]]},{"id":"5052c5d60c998a0a","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":1900,"wires":[["0b2c5a93a5c3bbf5"]]},{"id":"0b2c5a93a5c3bbf5","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 218","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":1800,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [30 December 2022 22:11 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969/3 "2022-12-30T22:11:15Z")

</div>

Strangely I had the same requirement a few days ago and came up with a function node since I couldn't see another way either.  
note: everyone in here and my wife all agree I'm not a good coder so take this with great skepticism

```auto
let Z = msg.payload;

//if good
if (Z != false) {
    context.set("CountBad",0);
    node.status({ fill: "blue", shape: "dot", text: "good" });
}
//if bad
if (Z == false){
    let W = context.get("CountBad") ?? 0;
    W = W + 1;
    context.set("CountBad", W);
    node.status({ fill: "red", shape: "ring", text: "bad " + W });
    if (W > 10) {
        flow.set("Alert",true);
    }
}

return msg;

```

I put this in a function node and if the message is not false it sets a counter to zero. If the message is false for ten messages it then sets an alert. Any not false message before it gets to ten resets the counter. Adjust code as needed. This is not the node-red way but sometimes you gotta do what you gotta do.

---

<div class="post-metadata">

### Author: ![dth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dth/32/72723_2.png) [@dth](https://discourse.nodered.org/u/dth)
#### Post date: [31 December 2022 10:05 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969/4 "2022-12-31T10:05:03Z")

</div>

Thanks guys 🙂

Since I am already making the true/false distinction in a javascript function node, I will just use the counter approach. It seems much simpler 🙂

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [31 December 2022 21:36 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969/5 "2022-12-31T21:36:20Z")

</div>

For future reference, I think the `join` node can be configured to do what you want. Here, it is set to wait for 3 messages, rather than 10, to simplify testing.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/2539084ea77afc34fdba613b6e7fa9a10fc6471f.png)

```auto
[{"id":"efcf54024f5b1558","type":"inject","z":"c3b4e23e95193f6b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":110,"y":360,"wires":[["898355a08940da70"]]},{"id":"898355a08940da70","type":"join","z":"c3b4e23e95193f6b","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":430,"y":400,"wires":[["4c8f73321c998977"]]},{"id":"b42c3dc9636fa253","type":"inject","z":"c3b4e23e95193f6b","name":"complete ","props":[{"p":"complete","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":440,"wires":[["898355a08940da70"]]},{"id":"2b9b9c11733cec31","type":"change","z":"c3b4e23e95193f6b","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":400,"wires":[["898355a08940da70"]]},{"id":"4c8f73321c998977","type":"change","z":"c3b4e23e95193f6b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":400,"wires":[["c6ae5dd7cfd03d2e"]]},{"id":"886e094a462d70ef","type":"inject","z":"c3b4e23e95193f6b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":110,"y":400,"wires":[["2b9b9c11733cec31"]]},{"id":"c6ae5dd7cfd03d2e","type":"debug","z":"c3b4e23e95193f6b","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":400,"wires":[]}]

```

The two `change` nodes do housekeeping that could be done in other ways, but the `join` node is key. The `inject` node with `msg.complete` has also been added for testing purposes.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 March 2023 21:36 UTC](https://discourse.nodered.org/t/require-x-number-of-same-messages-before-sending-along/72969/6 "2023-03-01T21:36:33Z")

</div>

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