# Counting messages per main topic

**URL:** <https://discourse.nodered.org/t/counting-messages-per-main-topic/56814>\
**Category:** General\
**Created:** [17 January 2022 16:47 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814 "2022-01-17T16:47:31Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 January 2022 17:07 UTC](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814/2 "2022-01-17T17:07:47Z")

</div>

First you would need to split the topic and grab the first part.

Then you could use a function node and an object stored in context to count them...

### example...

![chrome_ay2VTcASfH](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/6742a9f9475a68d3916cc1c8a8809fd07ed5693e.gif)

### demo flow...

```auto
[{"id":"8f7f9a59772e9c54","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main1/sub1","payload":"","payloadType":"date","x":1940,"y":800,"wires":[["4b61f945350d10d6"]]},{"id":"4b61f945350d10d6","type":"function","z":"a4633311d1bf470d","name":"topic counter","func":"let topic_counter = flow.get(\"topic_counter\") || {};\n\nif(msg.topic == \"CLEAR-STORE\") {\n topic_counter = {};\n} else {\n const main = msg.topic.split(\"/\")[0]\n if(topic_counter[main] == null) {\n topic_counter[main] = 0;\n }\n topic_counter[main]++;\n}\n\nflow.set(\"topic_counter\", topic_counter);\n\nmsg.payload = topic_counter;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2190,"y":860,"wires":[["159f9dcabd276f59"]]},{"id":"e6ce8b5ac7c69af1","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main1/sub2","payload":"","payloadType":"date","x":1940,"y":840,"wires":[["4b61f945350d10d6"]]},{"id":"43647647ced4c82c","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main2/sub1","payload":"","payloadType":"date","x":1940,"y":880,"wires":[["4b61f945350d10d6"]]},{"id":"34fb52f7e712aa72","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"main21/sub2","payload":"","payloadType":"date","x":1940,"y":920,"wires":[["4b61f945350d10d6"]]},{"id":"159f9dcabd276f59","type":"debug","z":"a4633311d1bf470d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2370,"y":860,"wires":[]},{"id":"db507ead0e26e1af","type":"inject","z":"a4633311d1bf470d","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"CLEAR-STORE","x":1960,"y":740,"wires":[["4b61f945350d10d6"]]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/counting-messages-per-main-topic/56814)._
