Seems to me that @kersing already told you how to limit the number of output nodes?
Also, I’ve not used the TTN Node-RED library, but beware it’s been marked “archived”. I’ve no idea why, but that makes me wonder if the future V3 will be supported. Many SDKs have been archived too, and explicitly marked deprecated.
So, I’d simply use a standard MQTT Input node in Node-RED, which when using MQTT wildcards allows you to subscribe to all devices in an application. (I guess the TTN library allows for that too.) And a single MQTT Output node (probably using the very same MQTT Config node) could then be used for the downlinks.
Of course, if the downlink is always a function of the uplink, then there’s no need for a downlink at all. The device could determine the downlink’s payload without any server side code then.
Also, using JSON.stringify(msg.payload)
followed by some substring
looks weird and will fail when the format changes. I’d use attributes of the JavaScript object msg.payload
, rather than using a string representation of that object.
(Please see How do I format my forum post? [HowTo] And totally aside: your above database code would be very unsafe when you cannot be sure that the payload is always trusted.)