Detect immediately when a global variable becomes true

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

Possible
read here

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

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.

1 Like

+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

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?

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.

Do you use MQTT at the moment?

Do you have your own local reliable broker?

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

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.

[{"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

just one last question. If I would like create many connections how can I do? I try configure new server as 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

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