Define Constant variables


How can I define Constant variables which can be used anywhere in flow, and also saves the same in DB using GET FLOW api.

Example : To define Topic , constant variables can be defined to make flow dynamic, as declare same variables at multiple nodes is error prone.

HI @shershah

You can use the Change node to set a flow or global variable.
or do so in a function node.



If you want something that cannot be amended in a flow, then you can set global variables in settings.js.

As per my understading, this variables can not be used in topic names. To receive data from cloud, I have to define mqtt topic? Using mix of static and dynamic variables in topic name is not possible in Nodered

It is not possible in MQTT. A topic must be subscribed to in the remote broker.

But you can:

  • use wild card topics
  • environment variables
    • these env vars can be system defined or group/tab/subflow/global node-red defined env vars
  • subscribe dynamically (where the topic can be generated in a function node or change node from constants/env vars/values from wherever you please.

The point is the subscription cannot change reactively because it has to be registered to a broker external to node-red. Having a variable topic makes little sense.

Though we do now have this don't we? Since we can use a msg to change the subscriptions?

Kinda. But the point I was attempting to clarify was it cannot be some variable in the topic specifically - there is an overall unsubscribe/subscribe procedure (and a whole bunch of goings on in the client and node-red) meaning that a topic cannot be some automagically changing variable (probably still not :100: clear)

1 Like

Hi Steve,

I can not use function in case where I am subscribing to the data, yes it can be used when I am publishing the data.

Consider a topic
Instead of hardcoding and define at multiple places, I want to define this variables at one place, and use it everywhere in flow. Is it possible to do so?


You could setup all your constants as env vars (in one tab or group or globally or system defined if desired) or use dynamic subscriptions. You could compute all your topics etc in a function node and store them in context too.

As I said before

NOTE: To clarify.

Each group, tab, subflow and global node-red settings can have env vars defined. Anywhere there is a text input (like topic) you can enter ${myEnvVar}

Read up here

Maybe do your subscriptions once, output to a link node and you can then consume the data wherever you want? You can use the dynamic subscription mode and use an inject or some other node to deliver the subscriptions.


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