State of global variable in dashboard

I have a global variable which changes state around all my flows. As I need to show the state of the variable in the dashboard, is there any way smarter than to poll the variable each second? (I need a "live" display of the state).

You could use a 'Complete' node, select the nodes which can change your variable; this will then trigger when the specified nodes have completed their actions.

1 Like

If you already use MQTT (or even if you don't) then you could use a Retained topic in MQTT rather than global. Then feed that into all the nodes that need it and also into the dashboard node. Then any time it changes all nodes that need it will be updated.

1 Like

Yes, this is a good idea by the "complete" node can only select nodes in the same flow, while I have several into which the variable is spread.
Thank you.

I am not using MQTT in this project. Do you mean that it is worth to setup mosquitto anyway to keep track of few variables? I will have a thought on it.

I very rarely use global variables but use MQTT instead. I find it generally gives rise to simpler flows and has the advantage that all nodes get informed when it changes. Also it means the values can be global across multiple instances of node red which is particularly useful for variables that map real world values into node red, such as whether a light is on.

how do you enable the variabile to talk with mosquitto and publish its state?

I don't have the global variable at all, at points where the flow writes to the variable I publish it to MQTT instead.

yes, I got it soon after having sent my question to you! Thanks

One last question: with a global variable I can query its state with a switch node. In case of MQTT, how can I do in a flow when I need to know its current state? Generating an MQTT query seems not to be the best thing to do.

You can use a Join node to combine the MQTT value into the message, then you can switch on it. This is the one area where globals appear to be convenient. Sometimes though refactoring the flow can avoid the need for that. Is it possible to give a real world example of where you need to do this?

Yes, I have many.

For example:

When John comes back at home, he:

  • switches off the burglar alarm, which in turn:
    • opens the garage door
    • checks if the global variable "twilight switch" is true (night)
      • if true, switch on the living room lights
      • if false, do other actions.

Anoher one:

  • once the irrigation system is deployed and programmed, it always checks if the global variable "irrigation_enable" is set or not. This is to disable irrigation in winter.

OK, for the twilight switch state case I would use one of the boolean nodes, I like node-red-contrib-bool-gate, but there are others. Connect as inputs the MQTT states for burglar alarm on/off and twilight switch on/off. Then each time either the burglar alarm state or the twilight switch changes state you will get an output from the boolean node which then outputs the living room lights mqtt topic (which drives the lights via another flow somewhere) or triggers the other actions.

The second example is another one where a boolean node does the job, if I understand it. So the condition for 'irrigation on' is that the programme says switch it on AND irrigation enable is set. Once again each time either of those changes the appropriate action will be taken.

1 Like

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