Create a Node that can create a flow

Hi everyone,
I have a question and couldn't find any answers on the forum : is there a way to create a flow automaticly, using a node ?
I explain a bit more :
I use Telegram-bot to get informations (ID and password) from a user when he types /start on a conversation. When it is trigerred, I'd like to create a MQTT node and configure it with these 2 informations (for exemple, a topic with the ID in it). Is there a function to do that ?

Sorry it's difficult to explain by text and I'm not fluent in English..
THank you very much !

You can add/remove dynamic MQTT subscriptions.

So yes is the answer.

See this thread for an example of performing dynamic MQTT subscriptions.

In simple terms, you would simply need to make the telegram message do what the inject node does.

1 Like

Did you mean this thread? Dynamic MQTT node's "actions->connect" guidance needed

1 Like

Thanks Colin. Forgot to update the link.

This is, I think, the fourth time dynamic mqtt has come up in a couple of days. Very odd.

thank you so much ! I'll try this :slight_smile:

@Steve-Mcl actually, i'm not sure of what I want to do (^^) but I think this is more than "replace the inject node with a telegram message". Actually I have devices that send their messages (LoRa protocol) to a server called The Things Network. With Node-RED I create a MQTT link to retrieve the data, to transform it and to store it in a database. My flow looks like this :

Each device has its own ID and password so as you can see, for each device I create manually a new "line" ( a MQTT node and a link to the database).

What I'd like to do is to automaticly create a new line when I trigger a bot telegram : a new MQTT node configured with the infos given in telegram, linked to the database.

I will try to do this with the dynamic MQTT infos you gave me, thank you again guys !

Seems at first sight that you really don't need to have a line for each sensor. You are doing work that you can let the computer do.

If all of the sensors have an MQTT topic naming convention, you could use a single wildcard. Either way though, you could then feed the MQTT input to a single json node and on to a single function.

Inside the function, you can process any sensor since you will have a unique topic that represents its name. Process the topic string to get something you want to send to your db and use in your telegram bot (might be as simple as removing any static prefix string).

To handle new sensors, if the topic for each sensor is sensibly named, you shouldn't need to do anything! Since each topic will be unique, the above process will continue to work no matter how many sensors appear. If the topic strings aren't unique, there is a bit more work to do but presumably the sensor id would then appear in the data somewhere and you could pick that out in your function node.

Also, if you wanted to have human readable sensor names instead of whatever MQTT gives you, that is possible too in a single function. Simply create a lookup table and store it in a flow variable. When you get a sensor msg, do the lookup and apply the required name to the output msg topic.

Bottom line is that you should never need to keep repeating nodes to handle a group of sensors like this.

Here is an extreme example where I process ALL of the sensors in range of my 433MHz transciever. That has processed thousands of devices over the years since I'm in a dense urban area (currently lists 1,717 discovered devices though I only now operate around 6, mostly other people's smoke detectors :thinking:).

2 Likes

I agree. Note the words "in simple terms" and the wide open/overly simplified (but hinting there may be more to it) statement of " make the telegram message do what the inject node does" :smiley:

I guess you mean "Each device requires an individual broker connection with unique username and password"? Are you talking about Thingsboard? I've seen this comment more than once - and I dont understand the model - but it seems to make everything super complicated! Maybe it is my (mis)understanding or users (mis)understanding or (mis)configuration that results in this requirement? I would have thought 1 username/password/broker connection per project (e.g. all devices) would be possible?

Hmm, thingsboard MQTT connectivity seems a little off security wise. Device authentication options | ThingsBoard Community Edition
Normally, you would generate and use an access token for ease-of-use but they don't support TLS over that.

But after a very quick look, in general, I see nothing stopping a single client id connection from Node-RED nor a single broker for multiple devices. Though it was a very quick look.

In any case, even if you did need that, you could still feed all MQTT inputs to a single json and function node to process everything.

:wink: Of course ! I wanted just to precise it, just to be clear. And thank you for the work you do to explain in simple words to newbies like me ! :smile:

I'm not talking about Things board, I tried it and it looks like complicated.
I use The Things Network to declare gateways and devices. I then connect it in MQTT with my personnal server with Node-RED (for the moment one line per sensor but it will be soon changed, according to all the good advices you gave me ^^).

And I want users (farmers) to easily declare a new device, so I put a telegram bot. My goal is to do everything (declaration, check status, data from the device) with telegram. There's still a long road to do so but I will do it !

Hi @TotallyInformation @Steve-Mcl and thank you for all the good advices !

Here is a version of my flow with dynamic MQTT :). I can read infos from a .json file, connect to a MQTT broker and get the msg from my devices !
I also added something to append a new object and push it to a new file.

Still a lot of things to do but it's leaning well ! It put it here just in case someone wants more infos. And if you have any advise to do it better, don't hesitate !

Thank you again !

[{"id":"affb109fcb6efdfe","type":"watch","z":"ce6f6a0bdd3686d4","name":"Watch ttn.json","files":"Documents/mobilab_ttn/hubis_test.json","recursive":"","x":490,"y":640,"wires":[["830c9826d1a493e6"]]},{"id":"830c9826d1a493e6","type":"file in","z":"ce6f6a0bdd3686d4","name":"Read ttn.json","filename":"Documents/mobilab_ttn/hubis.json","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":670,"y":640,"wires":[["e10b2c1981d2f8f3"]]},{"id":"d08b06d4144023bb","type":"inject","z":"ce6f6a0bdd3686d4","name":"Inject auto","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":435,"y":600,"wires":[["830c9826d1a493e6"]],"l":false},{"id":"e10b2c1981d2f8f3","type":"json","z":"ce6f6a0bdd3686d4","name":"","property":"payload","action":"","pretty":false,"x":810,"y":640,"wires":[["262de0462e7dfd34"]]},{"id":"262de0462e7dfd34","type":"function","z":"ce6f6a0bdd3686d4","name":"","func":"msg1={}\nmsgsend=[]\nfor (const i in msg.payload){\n    msg1[i]= {\n    \"topic\": 'v3/'+ msg.payload[i].appName +'@ttn/devices/'+msg.payload[i].devEUI+'/up',\n    \"action\":'subscribe',\n    };\n    msgsend.push(msg1[i])\n}\nreturn [msgsend];\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":640,"wires":[["b6353c010af20c61","7987b5b106616b2e"]]},{"id":"b6353c010af20c61","type":"debug","z":"ce6f6a0bdd3686d4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1150,"y":580,"wires":[]},{"id":"7987b5b106616b2e","type":"mqtt in","z":"ce6f6a0bdd3686d4","name":"MQTT merle app","topic":"","qos":"2","datatype":"json","broker":"c87222d8.d2c36","nl":false,"rap":true,"rh":0,"inputs":1,"x":1100,"y":640,"wires":[["334fb3b44fc9cd25","3acb65ecd036ce61"]]},{"id":"334fb3b44fc9cd25","type":"function","z":"ce6f6a0bdd3686d4","name":"","func":"var message = {\n    \"id\": msg.payload.end_device_ids.device_id,\n    \"name\":'3C1-WS(Aval)',\n    \"lat\":43.642874,\n    \"lon\":5.017567,\n    \"value\": 1-msg.payload.uplink_message.decoded_payload.bytes[0]\n};\n\nmsg.payload=message;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1260,"y":640,"wires":[["cabbfe16587345af"]]},{"id":"3acb65ecd036ce61","type":"debug","z":"ce6f6a0bdd3686d4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1350,"y":580,"wires":[]},{"id":"cabbfe16587345af","type":"debug","z":"ce6f6a0bdd3686d4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1410,"y":640,"wires":[]},{"id":"171a5c812c9e20e1","type":"inject","z":"ce6f6a0bdd3686d4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":395,"y":700,"wires":[["876fdf9ee096eaad"]],"l":false},{"id":"876fdf9ee096eaad","type":"file in","z":"ce6f6a0bdd3686d4","name":"Read ttn.json","filename":"Documents/mobilab_ttn/hubis_test.json","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":510,"y":740,"wires":[["feb033ff708c8de4","1721b699ad331b20"]]},{"id":"feb033ff708c8de4","type":"json","z":"ce6f6a0bdd3686d4","name":"","property":"payload","action":"","pretty":false,"x":670,"y":740,"wires":[["e06b8f7835cd6040"]]},{"id":"1721b699ad331b20","type":"debug","z":"ce6f6a0bdd3686d4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":800,"wires":[]},{"id":"e06b8f7835cd6040","type":"function","z":"ce6f6a0bdd3686d4","name":"","func":"\nvar myObj = {\n    \"devEUI\":\"eui-70b3d57ed004c18a\",\n    \"name\":\"3C3-WS(Aval)\",\n    \"appName\" : \"merle\"\n};\n//push the object to your array\nmsg.payload.push(myObj);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":740,"wires":[["a2215c2edddc202f"]]},{"id":"a2215c2edddc202f","type":"file","z":"ce6f6a0bdd3686d4","name":"hubis_test","filename":"Documents/mobilab_ttn/hubis_test.json","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":950,"y":740,"wires":[["9c12aa73277cbab3"]]},{"id":"9c12aa73277cbab3","type":"debug","z":"ce6f6a0bdd3686d4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":740,"wires":[]},{"id":"c87222d8.d2c36","type":"mqtt-broker","name":"merle","broker":"eu1.cloud.thethings.network","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

the file where the json is stored looks like this :

[{"devEUI":"eui-70b3d57ed004c188","devName":"3C1-WS(Aval)","appName":"merle"},{"devEUI":"eui-70b3d57ed004c189","devName":"3C2-WS(Aval)","appName":"merle"},{"devEUI":"eui-70b3d57ed004c18a","name":"3C3-WS(Aval)","appName":"merle"},{"devEUI":"eui-70b3d57ed004c18a","name":"3C3-WS(Aval)","appName":"merle"},{"devEUI":"eui-70b3d57ed004c18a","name":"3C3-WS(Aval)","appName":"merle"}]

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