# Could I use a Variable?

**URL:** <https://discourse.nodered.org/t/could-i-use-a-variable/2651>\
**Category:** General\
**Created:** [29 August 2018 07:24 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651 "2018-08-29T07:24:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![acestu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/acestu/32/90408_2.png) [@acestu](https://discourse.nodered.org/u/acestu)\
**Post date:** [29 August 2018 07:24 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651/1 "2018-08-29T07:24:58Z")

</div>

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

---

<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:** [29 August 2018 07:41 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651/2 "2018-08-29T07:41:31Z")

</div>

could you use an environment variable using the $(env\_var\_name) syntax ?  
(which timer node are you using ?)

---

<div class="post-metadata">

**Author:** ![acestu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/acestu/32/90408_2.png) [@acestu](https://discourse.nodered.org/u/acestu)\
**Post date:** [30 August 2018 10:21 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651/3 "2018-08-30T10:21:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [30 August 2018 12:15 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651/4 "2018-08-30T12:15:04Z")

</div>

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:

```auto
[{"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"]]}]

```

---

<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:** [30 August 2018 19:53 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651/5 "2018-08-30T19:53:26Z")

</div>

> [@acestu](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [31 August 2018 08:24 UTC](https://discourse.nodered.org/t/could-i-use-a-variable/2651/6 "2018-08-31T08:24:27Z")

</div>

> [@acestu](#):
>
> ... 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 ?

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.
