Could I use a Variable?

#1

Hi,

I have 20 lots of mqtt information coming in and each has a countdown timer so that if nothing comes in within a certain time a zero will be sent to the gauges, when I have a problem I have to reduce the time of the twenty timers from 20 mins to something workable like 5 mins, this is time consuming and i wondered if I could have a variable in all the timer nodes and then just change it once for all of them somewhere ?

thanks in advance
Stuart

#2

could you use an environment variable using the $(env_var_name) syntax ?
(which timer node are you using ?)

#3

Hi, Dceejay,

I am using the timeouttrigger from the node red library, I have been doing a bit of digging and as far as I am aware Variables disappear on reboot so I would have to back them up somehow.

thanks
Stuart

#4

One approach is to use a global context incl. contextStorage set in settings.js

  1. set the global variable.
  2. use a function with setTimeout()

example:

[{"id":"a45da4bb.5ab008","type":"function","z":"895bbff8.0e6c4","name":"watchdog","func":"var delay = (global.get('delay') || 2) * 1000;\nvar timeout = context.get('timeout') || null;\n\nif (timeout) {\n    clearTimeout(timeout);\n}\n\nvar timeout = setTimeout(function() {\n    node.send(msg);\n}, delay);\n\ncontext.set('timeout', timeout);\n\nreturn null;","outputs":1,"noerr":0,"x":280,"y":4220,"wires":[["28d88d3d.0c1a12"]]},{"id":"28d88d3d.0c1a12","type":"debug","z":"895bbff8.0e6c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":4220,"wires":[]},{"id":"a1c461ee.db5fe","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"timeout","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":4220,"wires":[["a45da4bb.5ab008"]]},{"id":"5c2960e6.0e869","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":4120,"wires":[["6d532299.17d1cc"]]},{"id":"6d532299.17d1cc","type":"change","z":"895bbff8.0e6c4","name":"","rules":[{"t":"set","p":"delay","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":4120,"wires":[[]]},{"id":"a9e85147.9b828","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":4160,"wires":[["6d532299.17d1cc"]]}]
#5

Environment variables exist outside of Node-RED so Dave's suggestion is a little different. You can set an Environment variable before starting Node-RED. So you might need to adjust the way that you start Node-RED slightly. This only really works if the variable is actually a constant as far as Node-RED is concerned.

If this doesn't work, there are other timer nodes that might do what you want.

#6

Another simple way is to use a subflow.

Put your trigger node into a subflow and replace all your trigger nodes by the subflow.
The time can than be modified in the subflow once.

1 Like