# Trigger node and global variables

**URL:** <https://discourse.nodered.org/t/trigger-node-and-global-variables/10101>\
**Category:** General\
**Created:** [13 April 2019 11:43 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101 "2019-04-13T11:43:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 11:43 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/1 "2019-04-13T11:43:47Z")

</div>

Hi again folks.

I have a `trigger` node and I want it to wait a certain time.

So I say:

> Send: existing msg  
> Then: wait for .......

And I can only set milliseconds; seconds; minutes;hours; etc.

I want this value to be a global context value.

Possible?

Not a big deal if it isn't, but as I am working on a thing just now, that would be nicer than having a fixed value.

Alternative node?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 April 2019 12:07 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/2 "2019-04-13T12:07:38Z")

</div>

Can't you send msg.delay or something similar to the trigger node?

I don't use it but I'm certain I've read something somewhere.

Have you checked the info for trigger node on the side bar?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 12:14 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/3 "2019-04-13T12:14:31Z")

</div>

I think that is for the `delay` node rather than the `trigger` node.

I have kind of got it working, but with an external node called `mytimeout` which allows the trigger time to be set.

Thanks though for the reply.

Appreciated.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 April 2019 12:19 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/4 "2019-04-13T12:19:22Z")

</div>

How about this ...

```auto
[{"id":"5ff626e9.141ac8","type":"delay","z":"4ee0e132.e7df2","name":"variable delay","pauseType":"delayv","timeout":"0","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":440,"y":260,"wires":[["71243006.b8b14"]]},{"id":"31f462f.376889e","type":"inject","z":"4ee0e132.e7df2","name":"set 277ms delay","topic":"","payload":"277","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":40,"wires":[["a9095cd3.3bb64"]]},{"id":"4aa9a154.13781","type":"inject","z":"4ee0e132.e7df2","name":"set 5s delay","topic":"","payload":"5000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":80,"wires":[["a9095cd3.3bb64"]]},{"id":"71243006.b8b14","type":"debug","z":"4ee0e132.e7df2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":180,"wires":[]},{"id":"a9095cd3.3bb64","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"delay","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":40,"wires":[[]]},{"id":"d83ed7d3.dc4ed8","type":"inject","z":"4ee0e132.e7df2","name":"set 1m delay","topic":"","payload":"60000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":120,"wires":[["a9095cd3.3bb64"]]},{"id":"d7293d4a.22f75","type":"inject","z":"4ee0e132.e7df2","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":180,"wires":[["71243006.b8b14","ab715b96.396ba8"]]},{"id":"ab715b96.396ba8","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":260,"wires":[["5ff626e9.141ac8"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 12:29 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/5 "2019-04-13T12:29:19Z")

</div>

Nice.

But I think the meaning got lost in translation.

This sends out a _pulse train_ at a given interval.

I'm wanting a time out with a variable amount of time for the time out.

So:  
I set the global _time out_ to 20 seconds.  
I get a pulse message coming in every..... (when ever).  
If it is quicker than 20 seconds, the output is _OK_.

If the time goes beyond 20 seconds, the output is _FAIL_.

I can change the time form 20 seconds to 30, 15, 5, what ever.

The _idea_ is that really the time out and frequency are the same. So to avert any erroneous time outs, I get the time and (say) add half again.

So:  
Every 20 seconds _something_ happens. I need to see it has happened. (Get an _I am working_ signal.)

If something goes wrong, after _time + time/2_ the `trigger` isn't _reset_ and I get a _Something is wrong_ signal.

Clearer?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 12:37 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/6 "2019-04-13T12:37:31Z")

</div>

Indulging me with my flow:  
(You will need `mytimeout` to see it working.)

```auto
[{"id":"c85e8713.ea705","type":"mytimeout","z":"15af81c5.64999e","name":"","outtopic":"","outsafe":"on","outwarning":"","outunsafe":"off","warning":"","timer":"30","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":430,"y":390,"wires":[["6e063b00.75032c"],[]]},{"id":"b9a5395f.f8a57","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":120,"wires":[["75c50db2.3263f4"]]},{"id":"75c50db2.3263f4","type":"function","z":"15af81c5.64999e","name":"","func":"var t = msg.payload\nmsg.timeout = t;\nflow.set('timer',t);\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":240,"wires":[[]]},{"id":"c9086361.d7225","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":160,"wires":[["75c50db2.3263f4"]]},{"id":"35481fd9.3309a8","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":200,"wires":[["75c50db2.3263f4"]]},{"id":"1b9fd93e.e3cc07","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":240,"wires":[["75c50db2.3263f4"]]},{"id":"1d2a5070.ed4c28","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":390,"wires":[["dd4f1df6.497f2"]]},{"id":"6e063b00.75032c","type":"debug","z":"15af81c5.64999e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":320,"wires":[]},{"id":"dd4f1df6.497f2","type":"function","z":"15af81c5.64999e","name":"","func":"var t = flow.get('timer');\nt = t + (t/2);\nmsg.timeout = t;\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":390,"wires":[["c85e8713.ea705"]]}]

```

Oh, and you need to press one of the buttons at the top before you start.  
My bad.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 April 2019 12:38 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/7 "2019-04-13T12:38:30Z")

</div>

Clearer yes but if you'd said that in the first place 🙄

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 12:40 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/8 "2019-04-13T12:40:09Z")

</div>

Sorry, I thought I did.

I'm wanting a trigger node with a controllable time out value.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 April 2019 12:44 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/9 "2019-04-13T12:44:52Z")

</div>

What about, storing a time stamp every time a message comes. Then use a function node to see if the time from last timestamp to current time stamp is less than your global setting, send OK. If it's greater, send FAIL

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 12:47 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/10 "2019-04-13T12:47:27Z")

</div>

Yeah, that's possible.

Thanks.

There's always more ways to _skin the cat_ than are in the book.

😄

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 April 2019 12:53 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/11 "2019-04-13T12:53:28Z")

</div>

Here is an example...

```auto
[{"id":"31f462f.376889e","type":"inject","z":"4ee0e132.e7df2","name":"set 277ms delay","topic":"","payload":"277","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":40,"wires":[["a9095cd3.3bb64"]]},{"id":"4aa9a154.13781","type":"inject","z":"4ee0e132.e7df2","name":"set 5s delay","topic":"","payload":"5000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":80,"wires":[["a9095cd3.3bb64"]]},{"id":"71243006.b8b14","type":"debug","z":"4ee0e132.e7df2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":180,"wires":[]},{"id":"a9095cd3.3bb64","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"maxtime","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":40,"wires":[[]]},{"id":"d83ed7d3.dc4ed8","type":"inject","z":"4ee0e132.e7df2","name":"set 1m delay","topic":"","payload":"60000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":120,"wires":[["a9095cd3.3bb64"]]},{"id":"d7293d4a.22f75","type":"inject","z":"4ee0e132.e7df2","name":"fake pulse train","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":180,"wires":[["4f608851.67b078"]]},{"id":"4f608851.67b078","type":"function","z":"4ee0e132.e7df2","name":"on time?","func":"var maxtime = flow.get(\"maxtime\") || 20000;//default to 20s\nvar thisTimestamp = Date.now();\nvar lastTimestamp = flow.get(\"lastmsg\") || thisTimestamp;\nvar diffMS = thisTimestamp - lastTimestamp;\nflow.set(\"lastmsg\",thisTimestamp);\nif(diffMS < maxtime){\n msg.payload = \"OK\";\n} else {\n msg.payload = \"FAIL\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":180,"wires":[["71243006.b8b14"]]}]

```

you can adapt your solution from this.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 12:59 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/12 "2019-04-13T12:59:44Z")

</div>

**SO close**

I will try to work it out from that.  
(maybe not, though. The mechanics are not compatible. Read on please.)

The only problem is this:

Say I have it for 5 seconds.  
Pulse. (3 seconds) pulse (3 seconds) pulse (repeat)  
All is good.

But! (This is the kicker!)  
If I send a pulse, and then there is a 6 (or greater than the delay)........  
I don't get the "FAIL" until the next signal.

Given the _next_ signal didn't arrive because the sending thing failed...... It will never arrive and I will not get a FAIL message.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2019 13:03 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/13 "2019-04-13T13:03:23Z")

</div>

Bed is calling.

I'm calling it a night.

Thanks for the help though.

Again: I appreciate it. It helps me see things from a different perspective, and that can be a good thing as it helps keep an open view on things.  
A narrow view can be problematic sometimes.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 April 2019 13:05 UTC](https://discourse.nodered.org/t/trigger-node-and-global-variables/10101/14 "2019-04-13T13:05:50Z")

</div>

combine the two flows I gave you and add a reset.

Only takes a little imagination...

```auto
[{"id":"eb862e26.25a2d","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"FAIL","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":260,"wires":[["71243006.b8b14"]]},{"id":"31f462f.376889e","type":"inject","z":"4ee0e132.e7df2","name":"set 277ms delay","topic":"","payload":"277","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":40,"wires":[["a9095cd3.3bb64"]]},{"id":"4aa9a154.13781","type":"inject","z":"4ee0e132.e7df2","name":"set 5s delay","topic":"","payload":"5000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":80,"wires":[["a9095cd3.3bb64"]]},{"id":"71243006.b8b14","type":"debug","z":"4ee0e132.e7df2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":180,"wires":[]},{"id":"a9095cd3.3bb64","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"maxtime","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":40,"wires":[[]]},{"id":"d83ed7d3.dc4ed8","type":"inject","z":"4ee0e132.e7df2","name":"set 1m delay","topic":"","payload":"60000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":120,"wires":[["a9095cd3.3bb64"]]},{"id":"d7293d4a.22f75","type":"inject","z":"4ee0e132.e7df2","name":"fake pulse train","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":180,"wires":[["4f608851.67b078","5b828c11.6e0874"]]},{"id":"4f608851.67b078","type":"function","z":"4ee0e132.e7df2","name":"on time?","func":"var maxtime = flow.get(\"maxtime\") || 20000;//default to 20s\nvar thisTimestamp = Date.now();\nvar lastTimestamp = flow.get(\"lastmsg\") || thisTimestamp;\nvar diffMS = thisTimestamp - lastTimestamp;\nflow.set(\"lastmsg\",thisTimestamp);\nif(diffMS < maxtime){\n msg.payload = \"OK\";\n} else {\n return null;\n}\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":180,"wires":[["71243006.b8b14","b6114284.392cd"]]},{"id":"4d5ce745.142e28","type":"delay","z":"4ee0e132.e7df2","name":"variable delay","pauseType":"delayv","timeout":"0","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":360,"y":260,"wires":[["eb862e26.25a2d"]]},{"id":"5b828c11.6e0874","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"maxtime","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":260,"wires":[["4d5ce745.142e28"]]},{"id":"b6114284.392cd","type":"change","z":"4ee0e132.e7df2","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":220,"wires":[["5b828c11.6e0874"]]}]

```
