Counting messages per main topic


Looking for advice/solution for the following problem:

A got an mqtt broker with lots of topics and subtopics.
I wish to get a list of main topics, and check how many messages are published per each main topic, including ist subtopics.
Let's say this many messages were published to the broker:

Maintopic1/Subtopic1 - 2 messages
Maintopic1/Subtopic1/Subtopic - 2 messages
Maintopic1/Subtopic2 - 2 messages
Maintopic2 - 1 message
Maintopic2/Subtopic1 - 1 message

The result I would like to get automatically would look like this:
Maintopic1 - 6 messages
Maintopic2 - 2 messages

I have used 'node-red-contrib-msg-speed' to check how many messages are being published in a certain topic. That's basically what I want, but without have to give the topic. It should check all main topics.

Sorry if my explanation is not easy to understand....

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...



demo flow...

[{"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"]]}]

Hi Steve-Mci,

I am a beginner and impressed by your skills, to put out a solution just like that !
Works perfectly, giving a broker subscription with wildcard (#) to the input, the output gave me the list of main topics, and the total count of all messages within. Numbers constantly increasing of course, until variables are cleared.

I guess, to be able to do use the speed node (measuring how many messages were published withing given time), I would need to sort all messages per main topic ?

Can a function node have more outputs? (Like a switch node?)
All messages to maintopic1/# - output1, all messages to maintopic2/# to output 2, and so on ...

Yes: Writing Functions : Node-RED

I dont know that node. Probably best to start a new thread, stating this node name in the topic so that it catches the attention of someone who knows it can assist.

PS: Dont forget to mark the post you believe to be the solution.

As I do to all beginners, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Thank you very much for your help !

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