Inject and MQTT input topics - using variables

#1

This is most likely a stupid question, but I have a number of devices, identical in all but name which receive input from injects and from MQTT input. Is it possible to use, say, flow variables for topics in these two, rather than fixed strings?

1 Like

#2

I don't believe so, no.

What I do is make sure that all similar data uses a parent topic. So perhaps all the data from physical devices uses DEVICES/<deviceid> for example. Then I can simply listen for DEVICES/+ and get all of the direct descendent data on that master topic. I can then sort out different paths for different devices in Node-RED. You could go another layer deep if, for example, you needed to split by device hub and then by id.

Does that make sense?

0 Likes

#3

Devices/# I think.

0 Likes

#4

It would make sense but for the way I use my topics - devicex/fromesp - and on mqtt outputs I send topic devicex/toesp

Do I don't have a way to use descendent data/

0 Likes

#5

That will give you a full path so you need to take care with that.

DEVICE/# would give you

DEVICE/thing1
and
DEVICE/thing1/something

DEVICE/+

would only give you

DEVICE/thing1

Sometimes quite useful.

https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/

0 Likes

#6

I think you can set the mqtt topic string as an ENV variable... so the same flow could run on multiple servers setup with different environments.

But if you need to listen for different device topics within a single node-red instance, use the wildcard topic string # and then use a switch node to route to named devices.

0 Likes

#7

Sorry, I can't think of another way.

0 Likes

#8

Exactly, you said "listen for DEVICES/+ and get all of the direct descendent data on that master topic" which is what the # gives. DEVICE/thing1/something is a direct descendent of DEVICE isn't it? Though perhaps by 'direct' you meant only one level, which I suppose it could mean, perhaps you are right. Anyway the important thing is that the operation has been clarified.

0 Likes

#9

Urm, no, that's an indirect descendent. :slight_smile: Sorry for not being clear.

0 Likes

#10

I don't think so. In family relationships I believe an indirect descendant would be via a cousin or marriage or similar

0 Likes

#11

Haha, I bow to your superior search skills :slight_smile:

Well, hopefully everyone now understands what I meant anyway.

0 Likes