I want to create a mqtt node subscibe with custom topic that (I set up from another node that has function to add that node)

1 Answers

1
hardillb On

MQTT-in nodes do not support dynamically settings the topic.

You have a few options

  1. Set the topic in the MQTT-in node to a wildcard e.g. # and then use a switch node to filter incoming messages based on a comparing the topic of the incoming message to a context variable. If you have a high volume of messages this might cause performance problems

  2. Set the topic with an environment variable. You can use the ${ENV_VAR_NAME} to set configuration values at flow deployment time. This would mean knowing what topic you need before starting Node-RED

  3. Use the Node-RED Admin API to edit the current flow. While this is closest to the question you have asked, this is a very complex solution as it means crafting the correct payload to modify the currently running flow and then triggering a deploy.