# How to detect change in a flow or global variable

**URL:** <https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454>\
**Category:** Developing Nodes\
**Created:** [3 December 2018 21:43 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454 "2018-12-03T21:43:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [3 December 2018 21:43 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/1 "2018-12-03T21:43:30Z")

</div>

Is it possible for a custom node to react immediately to a change in a flow/global variable? Is there an event emitted when a `function` node executes something like `global.set("global","value")` or when a `change` node modifies a variable?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2018 22:15 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/2 "2018-12-03T22:15:18Z")

</div>

If you want notification when something like that changes you may be better off using MQTT rather than global variables.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 December 2018 22:30 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/3 "2018-12-03T22:30:58Z")

</div>

... or also wire an output from the function node to the custom node. If it's on a different tab use a pair of link nodes. That is what the wires are for.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 December 2018 22:33 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/4 "2018-12-03T22:33:40Z")

</div>

> [@drmibell](#):
>
> react immediately

No, there is no _event_ that is triggered when a variable changes. Also, just because a function node rewrites a variable doesn't mean that it has changed! So this partly depends on what you are trying to achieve.

Generally, if I want to check if something has been updated (not necessarily changed), I will add a timestamp when it is written. Then I can check the timestamp - but that still means that I have to read it again.

MQTT would work but doesn't really help unless you make sure that you don't update the data unless it has actually changed. If you do that, then MQTT is the best approach.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [3 December 2018 22:51 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/5 "2018-12-03T22:51:49Z")

</div>

@TotallyInformation, I would settle for rewrite, changed or not.

@Colin and @dceejay, yes of course, I'm using both mqtt and ordinary message passing, but I have a rather special (peculiar?) situation where a large of number of nodes might need to be aware of a change in a variable, and it gets to be messy to wire up dozens of `link` or `mqtt` nodes. If the answer is that no such event is available, I can live with that.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 December 2018 22:57 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/6 "2018-12-03T22:57:14Z")

</div>

well you can have one link out node and loads of link in nodes connected to it to help keep the mess under control

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [3 December 2018 23:00 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/7 "2018-12-03T23:00:02Z")

</div>

Or one publisher and loads of subscribers 🙂. Got it.

---

<div class="post-metadata">

**Author:** ![santaklon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/santaklon/32/16737_2.png) [@santaklon](https://discourse.nodered.org/u/santaklon)\
**Post date:** [28 May 2020 18:12 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/8 "2020-05-28T18:12:46Z")

</div>

Hello good people,

some years later and i'm having the same problem but cant find any other thread on this. Is there any developement in this? Is it still impossible to broadcast / subscribe to commands withing NR without going through an external MQTT broker or making a mess with "invisible" wires aka link-nodes? This seems like the most basic function, I cant believe it doesn't exist!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 May 2020 18:25 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/9 "2020-05-28T18:25:17Z")

</div>

> [@santaklon](#):
>
> without going through an external MQTT broker

You can run an MQTT broker within Node-RED itself nowadays using

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3eb07a4751468716626a9e509711b633e6db3edd.png)

> **[node-red-contrib-aedes](https://flows.nodered.org/node/node-red-contrib-aedes)**
>
> Node Red MQTT broker node based on aedes.js

---

<div class="post-metadata">

**Author:** ![santaklon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/santaklon/32/16737_2.png) [@santaklon](https://discourse.nodered.org/u/santaklon)\
**Post date:** [28 May 2020 18:29 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/10 "2020-05-28T18:29:24Z")

</div>

Thank you for this information, very interesting. A lot easier for sure than using an external broker, but still unnecessarily complicated it seems - any idea why this is not built into NR as a basic function?

The "Action" Nodes do something so very similar, yet not quite that.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [28 May 2020 18:33 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/11 "2020-05-28T18:33:52Z")

</div>

I would assume it's because NR is based on flow based programming. Stuff triggering in the background without no indicating wires would go against the principle.

I've wished a feature like this on some occasions so I understand the "need" but I also understand the principle why something like that doesn't exist. With that said I would likely use them if some "context watcher" nodes existed.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [28 May 2020 18:35 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/12 "2020-05-28T18:35:16Z")

</div>

> [@santaklon](#):
>
> any idea why this is not built into NR as a basic function

I'd suspect that its not been asked for very much - also the workarounds are easy - also state changes being sent from one node to another node without something in-between, is pretty much against the whole Node-RED philosophy

(Last bit just my opinion) 🙂

[edit] But I note that @ristomatti seems to agree with me[/edit]

---

<div class="post-metadata">

**Author:** ![santaklon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/santaklon/32/16737_2.png) [@santaklon](https://discourse.nodered.org/u/santaklon)\
**Post date:** [28 May 2020 18:41 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/13 "2020-05-28T18:41:03Z")

</div>

The thing about Node-RED Philosophy is indeed a good point here i guess. However variables and the linking of action-nodes are also not quite "visible", so one could ague NR has kinda left the realm of purely visible connections a while ago anyway 😉

Workarounds are actually not quite as easy it seems to me - but i'll give Aedes a try!

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [28 May 2020 18:45 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/14 "2020-05-28T18:45:11Z")

</div>

One workaround could be simply polling for changes. A parameterized subflow could wrap a function node that would periodically check if the given flow/global property has changed, and send the new value if it has.

I don't like polling approach as an idea but it could work if thought pragmatically.

---

<div class="post-metadata">

**Author:** ![santaklon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/santaklon/32/16737_2.png) [@santaklon](https://discourse.nodered.org/u/santaklon)\
**Post date:** [28 May 2020 18:48 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/15 "2020-05-28T18:48:59Z")

</div>

Yeah i have thought about this too and even tried it in one instance - it however generates a lot of unnecessary "background noise" processing-wise so i never did it on a larger scale.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [28 May 2020 18:52 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/16 "2020-05-28T18:52:28Z")

</div>

I doubt any performance hit could be noticed if polled on a reasonable interval. Can't say what reasonable would be but maybe something between 100-1000ms. I suppose for most purposes that latency would not be an issue.

---

<div class="post-metadata">

**Author:** ![santaklon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/santaklon/32/16737_2.png) [@santaklon](https://discourse.nodered.org/u/santaklon)\
**Post date:** [28 May 2020 18:56 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/17 "2020-05-28T18:56:45Z")

</div>

Hm. Yeah maybe you are right, i just didnt like the nervousness of the debug log, but it probably doesnt stress out an Rpi4 as much. Might give it another go, but since i just managed to get Aedes MQTT up and running within less than 5 minutes i guess MQTT is indeed the easiest option!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 May 2020 20:57 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/18 "2020-05-28T20:57:29Z")

</div>

> [@santaklon](#):
>
> since i just managed to get Aedes MQTT up and running within less than 5 minutes i guess MQTT is indeed the easiest option

Installing mosquitto is just as quick.  
`sudo apt install mosquitto`  
and you are up and running.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [28 May 2020 21:17 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/19 "2020-05-28T21:17:14Z")

</div>

> [@Colin](#):
>
> Installing mosquitto is just as quick.

Another advantage of `mosquito` is that if you keep it running in the background it will always be ready when Node-RED starts (or restarts). If you use a broker implemented as a contributed node, there can be timing issues that prevent new or retained messages from being sent when you need them. Of course, there are workarounds, but why bother?

---

<div class="post-metadata">

**Author:** ![santaklon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/santaklon/32/16737_2.png) [@santaklon](https://discourse.nodered.org/u/santaklon)\
**Post date:** [28 May 2020 21:19 UTC](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454/20 "2020-05-28T21:19:06Z")

</div>

I see, thank you for the clarification. I will switch to mosquitto then!

[Next page](https://discourse.nodered.org/t/how-to-detect-change-in-a-flow-or-global-variable/5454.md?page=2)
