# Detect immediately when a global variable becomes true

**URL:** <https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876>\
**Category:** General\
**Created:** [9 July 2022 14:52 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876 "2022-07-09T14:52:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)\
**Post date:** [9 July 2022 14:52 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/1 "2022-07-09T14:52:30Z")

</div>

I would like detect immediately when a global variable beomes true...a sort of Listening of a variable.  
this is possible?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 July 2022 15:23 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/2 "2022-07-09T15:23:30Z")

</div>

Possible  
read [here](https://discourse.nodered.org/t/lighting-controller-ideas/50876/18)

Or you could check where ever you set the global to true in the flow.

---

<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:** [9 July 2022 19:19 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/3 "2022-07-09T19:19:52Z")

</div>

If you can't just wire it through as suggested by @E1cid then if you are using MQTT (or even if you aren't) then you can use a Retained topic in MQTT instead of (or possibly in addition to) the global variable.

---

<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:** [9 July 2022 19:43 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/4 "2022-07-09T19:43:28Z")

</div>

+1 on Colin's suggestion if you run your own reliable MQTT broker.

Especially if your global vars are global across all your NR instances

---

<div class="post-metadata">

**Author:** ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)\
**Post date:** [10 July 2022 15:38 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/5 "2022-07-10T15:38:48Z")

</div>

> [@cymplecy](#):
>
> reliable MQTT br

Many many thanks for your help. I'm trying found example to detect global var changes with mqtt online but I don't find nothing. Do you have some examples?

---

<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:** [10 July 2022 21:30 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/6 "2022-07-10T21:30:31Z")

</div>

It isn't a matter of detecting global changes with MQTT, it is a matter of using MQTT instead of globals (or you can keep the global too if you want to, but the primary mechanism is MQTT. To learn more about MQTT then see this tutorial [MQTT Essentials - All Core Concepts explained](https://www.hivemq.com/mqtt-essentials/).

---

<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:** [12 July 2022 06:53 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/7 "2022-07-12T06:53:48Z")

</div>

Do you use MQTT at the moment?

Do you have your own local reliable broker?

---

<div class="post-metadata">

**Author:** ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)\
**Post date:** [12 July 2022 08:37 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/8 "2022-07-12T08:37:12Z")

</div>

Hi many thanks, I never used mqtt before and I have many difficulties to learn use it to detect global variable change in real time. I had to find many tutorials or example to well understand how to use it

I think it could be very usefull  
many thanks

---

<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:** [12 July 2022 08:59 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/9 "2022-07-12T08:59:21Z")

</div>

The basic concept is to publish a message to a topic,using an MQTT out node, whenever you change a global context value and then use an MQTT in node to receive that value

e.g.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/60e2bf557356880a2df1ba97f89e9909568f5cc0.png)

```auto
[{"id":"4a2cc3f1baa8df94","type":"change","z":"ccf2657f077b6c31","name":"","rules":[{"t":"set","p":"testValue","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":140,"wires":[["c4699f429030d529","ac7c74ce39283628"]]},{"id":"e4524d59a5b1483f","type":"mqtt out","z":"ccf2657f077b6c31","name":"","topic":"202207120948/globals/testValue","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"376110f78a48cb31","x":790,"y":220,"wires":[]},{"id":"999f9b304a713a8e","type":"inject","z":"ccf2657f077b6c31","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bar","payloadType":"str","x":150,"y":140,"wires":[["4a2cc3f1baa8df94"]]},{"id":"c4699f429030d529","type":"function","z":"ccf2657f077b6c31","name":"rest of flow","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":140,"wires":[["61afc5811e9bf0e9"]]},{"id":"ac7c74ce39283628","type":"change","z":"ccf2657f077b6c31","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"testValue","tot":"global","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":220,"wires":[["e4524d59a5b1483f"]]},{"id":"61afc5811e9bf0e9","type":"function","z":"ccf2657f077b6c31","name":"rest of flow","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":140,"wires":[["0e0988696b1c8cd4"]]},{"id":"0e0988696b1c8cd4","type":"function","z":"ccf2657f077b6c31","name":"rest of flow","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":140,"wires":[[]]},{"id":"8bc4e0cf17fb6890","type":"inject","z":"ccf2657f077b6c31","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"foo","payloadType":"str","x":150,"y":80,"wires":[["4a2cc3f1baa8df94"]]},{"id":"4e49ebf4c9bdcdc9","type":"group","z":"ccf2657f077b6c31","name":"Use MQTT in node to get change in global.testValue","style":{"label":true},"nodes":["862e553dfb1c6a4d","32530adc96e9664a"],"x":394,"y":319,"w":512,"h":82},{"id":"862e553dfb1c6a4d","type":"mqtt in","z":"ccf2657f077b6c31","g":"4e49ebf4c9bdcdc9","name":"","topic":"202207120948/globals/testValue","qos":"2","datatype":"auto-detect","broker":"376110f78a48cb31","nl":false,"rap":true,"rh":0,"inputs":0,"x":550,"y":360,"wires":[["32530adc96e9664a"]]},{"id":"32530adc96e9664a","type":"debug","z":"ccf2657f077b6c31","g":"4e49ebf4c9bdcdc9","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":360,"wires":[]},{"id":"376110f78a48cb31","type":"mqtt-broker","name":"","broker":"test.mosquitto.org","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":{},"userProps":"","sessionExpiry":""}]

```

_Note: only use this technique with your own reliable MQTT broker as you can't rely on a public broker for reliable MQTT messaging_

---

<div class="post-metadata">

**Author:** ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)\
**Post date:** [12 July 2022 14:29 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/10 "2022-07-12T14:29:58Z")

</div>

> [@cymplecy](#):
>
> `:`

just one last question. If I would like create many connections how can I do? I try configure new server as [alarm.mosquito.org](http://alarm.mosquito.org) on port 1884 and I change topic in alarmValue but it not connect. and with your example some times connect other times not.

thank you really much

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 September 2022 14:30 UTC](https://discourse.nodered.org/t/detect-immediately-when-a-global-variable-becomes-true/64876/11 "2022-09-10T14:30:49Z")

</div>

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