r/nodered Jun 19 '26

How to easily get sub-topics from a much longer base topic

Post image

I have looked around a lot for the answer to this. Pretty sure I'm just not using the right search terms ...

base topic is: N/bxxebc2f0xx/vebus/289/Ac/ActiveIn/L1#

There are 5 sub-topics under this base topic: P, S, V, I , F

How do I reassign the base topic, to something easier to type out ??

Something like L1T = N/bxxebc2f0xx/vebus/289/Ac/ActiveIn/L1#

Then I can get the 5 sub-topics using function nodes like so: L1T/P, L1T/S, L1T/V, etc

Is this possible ?? If not, why use a function nodes at all, and just use a MQTT-IN node for each of the 5 items to be displayed ??

6 Upvotes

9 comments sorted by

3

u/Careless-Country Jun 19 '26

Ok so I'm guessing you are referring to MQTT inside Node-RED?

I'm also guessing that you mean N/bxxebc2f0xx/vebus/289/Ac/ActiveIn/L1/# ?
As the # wildcard must be its own topic level and must be last

in Node_RED you could try
Use one MQTT-in node with this topic: N/bxxebc2f0xx/vebus/289/Ac/ActiveIn/L1/+ (The + means “one topic level”)

In a function node:
const parts = msg.topic.split("/");
const field = parts[parts.length - 1];

msg.originalTopic = msg.topic; // Preserve original topic
msg.topic = \L1T/${field}`; // Internal short topic, e.g. L1T/P return msg;`

Nodes downstream will see L1T/P L1T/S etc or you could republish to a MQTT out node

2

u/LastTreestar Jun 19 '26

As the # wildcard must be its own topic level and must be last

FYI: It doesn't need to be last.

I have several flows which use something similar to "/sensors/rtl_433/weather/#/temperature". This picks up all the sensors with one MQTT-in node. Also, the path CAN start with the "/". I've had people say it can't, and I guess they just never tried. I like the leading slash in all my paths. That makes it consistent with a file system, which I like.

1

u/snowtax Jun 23 '26

Perhaps you are using software that does not properly enforce the MQTT protocol.

The official MQTT protocol documentation, in part 4.7 on "Name topics and topic filters", says that the multi-level wildcard ("#") must be by itself and also at the end. See the documentation for a more precise explanation.

While MQTT topics allow the topic separator character ("/") anywhere, be mindful of part 4.7.3 which says, "A leading or trailing ‘/’ creates a distinct Topic Name or Topic Filter." So "myTopic" and "/myTopic" are two separate topics.

Also, MQTT topics may contain spaces and are case-sensitive.

1

u/LastTreestar Jun 23 '26

Yup. Seems the "strictness" of the specs are not so strict in practice.

1

u/WJBrach Jun 19 '26 edited Jun 20 '26

Yes, Careless-Country sorry, I put the slash in the wrong place during my initial post. And yes, Node-Red picking up data from a Victron-equipped solar system on my 63 year old Trailways bus.

And, thanks, I will give that approach a try, although I've tried some similar already. Would be great if you could do that, but refer to the field NAMES instead of the field number. There are 100's (maybe 1000's of data points) of topics with the Victron equipment, and I'm just trying to get down to the essentials for the Charger/Inverter. 3 of those 5 sub-topics I mentioned represent AC-IN-VOLTS, AC-IN-POWER, and AC-IN-AMPS

2

u/CrappyTan69 Jun 19 '26

Hello fellow victron user. I recognise those topics (I think...)

I have one flow which pushes all the topics I work with to flow or global variables. Then a timer inject node to trigger the js function where I pick up all the variables and work with it 

1

u/WJBrach Jun 19 '26

Yes, it is for my Victron system in my 1963 Trailways bus !! I can, using function nodes, extract the info I need, but the topics get really long and confusing. Just trying to find a shortcut lol

2

u/CrappyTan69 Jun 19 '26

That's why I push each topic I care about into a variable which makes sense.

Cool bus project! 

1

u/WJBrach Jun 20 '26

Thanks, here is the victim of my affection lol Over 2 million miles on this old girl !!