# Start a flow in a variable time

**URL:** <https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373>\
**Category:** General\
**Created:** [31 October 2019 15:20 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373 "2019-10-31T15:20:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 15:20 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/1 "2019-10-31T15:20:34Z")

</div>

How can I start a flow by a specific time? I have a created a time-variable (msg.payload). Now I ask myself how can I start the flow at this variable time?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [31 October 2019 15:41 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/2 "2019-10-31T15:41:27Z")

</div>

Take a look at [flows.nodered.org](http://flows.nodered.org) where there are lots of timer nodes, some of which allow you to send a message to the node with the time

---

<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:** [31 October 2019 15:58 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/3 "2019-10-31T15:58:48Z")

</div>

The cron+ node lets you send the time (specified in CRON format). Many of the timer nodes don't let you send a message to define the time.

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 16:18 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/4 "2019-10-31T16:18:43Z")

</div>

I need exactly the opposite. 😉  
I've packed the time into a variable and that's supposed to be done at the time.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [31 October 2019 16:24 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/5 "2019-10-31T16:24:01Z")

</div>

> I need exactly the opposite.

I doubt it. You "packed" the time into a variable, so instead of "packing" it in a variable, inject it in a cron node for example.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [31 October 2019 16:29 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/6 "2019-10-31T16:29:01Z")

</div>

Explore this example, may be you'll get some ideas [https://flows.nodered.org/flow/986e43672b35b4329d58ceb3d987596b](https://flows.nodered.org/flow/986e43672b35b4329d58ceb3d987596b)

---

<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:** [31 October 2019 17:00 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/7 "2019-10-31T17:00:53Z")

</div>

Do you actually want to do something some time after the present time? In other words after a delay from now? If so then is it a fixed delay or variable?

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 17:28 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/8 "2019-10-31T17:28:56Z")

</div>

I am trying to bulid a Power-On Switch for a lamp. The data where the lamp should be turned on comes from a calendar. So my Idea is, I read out the data of the calendar and extract the start date into a variable. The variable is the start button.  
I'm not still quite sure how I can use the cron+ node for that.

---

<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:** [31 October 2019 17:31 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/9 "2019-10-31T17:31:43Z")

</div>

Do you want to do that each day or read the calendar and set up a repeating schedule for the future?

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 17:47 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/10 "2019-10-31T17:47:16Z")

</div>

Currently I have my calendar read out twice a day. But I'm just seperating the upcoming date. Exactly this date should turn on a lamp.

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 17:49 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/11 "2019-10-31T17:49:21Z")

</div>

I would like to steer the lamp over a calendar entry, since I work in shift service I have irregular times.

---

<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:** [31 October 2019 18:07 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/12 "2019-10-31T18:07:12Z")

</div>

Understood, so you want to trigger a one-off event at some time in the future.  
I don't know of a one-shot timer node that will trigger at a defined time. Others may be able to suggest something.

I think I would probably do that in javascript using setTimeout(). Alternatively you could calculate how far in the future it is and then send the message to a delay node, telling the node how much to delay the message for. How to do that is described in the info panel for the Delay 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:** [31 October 2019 18:11 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/13 "2019-10-31T18:11:15Z")

</div>

The cron-plus node can be (dynamically) set via time or timestamp or string date time

IE. It doesn't have to be a Cron expression

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 18:14 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/14 "2019-10-31T18:14:33Z")

</div>

Good Idea, Colin.  
I will also check out cron+. I still haven't senn how can I handle it. But I'm brand new in Node-Red.  
I also found a node called calendar trigger. That sound good to me as well.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [31 October 2019 18:21 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/15 "2019-10-31T18:21:39Z")

</div>

And here is how to do it with time events. As there is always many ways to do it.

```auto
[{"id":"fa9d56dd.b926f8","type":"function","z":"d0a71ce6.110d8","name":"Time Events","func":"var stamp = msg.payload;\n\nfunction pretty(num) {\n if (num < 10) {\n return \"0\" + num;\n }\n return \"\" + num;\n}\nvar updated = false;\nvar o_second = null;\nvar o_minute = null;\nvar o_hour = null;\nvar o_date = null;\nvar o_month = null;\nvar o_year = null;\nvar timeupdate = new Date(stamp);\n\nvar currenttime = global.get(\"currenttime\") || {second:0,minute:0,hour:0,date:0,month:0,year:0};\n\nif (currenttime.second !== timeupdate.getSeconds()) {\n currenttime.second = timeupdate.getSeconds();\n updated = true;\n o_second = {\n topic: \"TimeEventSecond\",\n payload: currenttime.second,\n timestamp: stamp\n };\n}\nif (currenttime.minute !== timeupdate.getMinutes()) {\n currenttime.minute = timeupdate.getMinutes();\n o_minute = {\n topic: \"TimeEventMinute\",\n payload: currenttime.minute,\n timestamp: stamp\n };\n}\nif (currenttime.hour !== timeupdate.getHours()) {\n currenttime.hour = timeupdate.getHours();\n o_hour = {\n topic: \"TimeEventHour\",\n payload: currenttime.hour,\n timestamp: stamp\n };\n}\nif (currenttime.date !== timeupdate.getDate()) {\n currenttime.date = timeupdate.getDate();\n o_date = {\n topic: \"TimeEventDate\",\n payload: currenttime.date,\n timestamp: stamp\n };\n}\nif (currenttime.month !== timeupdate.getMonth() + 1) {\n currenttime.month = timeupdate.getMonth() + 1;\n o_month = {\n topic: \"TimeEventMonth\",\n payload: currenttime.month,\n timestamp: stamp\n };\n}\nif (currenttime.year !== timeupdate.getFullYear()) {\n currenttime.year = timeupdate.getFullYear();\n o_year = {\n topic: \"TimeEventYear\",\n payload: currenttime.year,\n timestamp: stamp\n };\n}\nif (updated) {\n currenttime.timestamp = stamp;\n // can hold prettyfied date-time values in currenttime if needed \n /*\n var displaytime = pretty(currenttime.hour) + \":\" + pretty(currenttime.minute) + \":\" + pretty(currenttime.second);\n currenttime.displaytime = displaytime;\n currenttime.displaydate = pretty(currenttime.date) + \".\" + pretty(currenttime.month);\n */\n global.set(\"currenttime\", currenttime);\n \n \n return [o_second, o_minute, o_hour, o_date, o_month, o_year];\n \n}","outputs":6,"noerr":0,"x":330,"y":200,"wires":[["7e172815.fa88b8"],["43cfb168.9a04b"],["cfbff7e4.ff20d8"],["86740614.819928"],["5feaae2c.e30b8"],["93745496.41ac88"]]},{"id":"8e360b48.8a7298","type":"inject","z":"d0a71ce6.110d8","name":"","topic":"","payload":"","payloadType":"date","repeat":".5","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":200,"wires":[["fa9d56dd.b926f8"]]},{"id":"7e172815.fa88b8","type":"link out","z":"d0a71ce6.110d8","name":"te-second","links":[],"x":495,"y":100,"wires":[]},{"id":"43cfb168.9a04b","type":"link out","z":"d0a71ce6.110d8","name":"te-minute","links":["1aae89ad.457c66","435e96bd.8d5388"],"x":495,"y":140,"wires":[]},{"id":"cfbff7e4.ff20d8","type":"link out","z":"d0a71ce6.110d8","name":"te-hour","links":["cd2a9f30.2e52d"],"x":495,"y":180,"wires":[]},{"id":"86740614.819928","type":"link out","z":"d0a71ce6.110d8","name":"te-date","links":[],"x":495,"y":220,"wires":[]},{"id":"5feaae2c.e30b8","type":"link out","z":"d0a71ce6.110d8","name":"te-month","links":[],"x":495,"y":260,"wires":[]},{"id":"93745496.41ac88","type":"link out","z":"d0a71ce6.110d8","name":"te-year","links":[],"x":495,"y":300,"wires":[]},{"id":"1aae89ad.457c66","type":"link in","z":"d0a71ce6.110d8","name":"listen minute","links":["43cfb168.9a04b"],"x":105,"y":700,"wires":[["35cc7978.f7f4d6"]]},{"id":"35cc7978.f7f4d6","type":"switch","z":"d0a71ce6.110d8","name":"hour 10","property":"currenttime.hour","propertyType":"global","rules":[{"t":"eq","v":"10","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":700,"wires":[["ef630780.145ec8","c6695639.c621f8"]]},{"id":"ef630780.145ec8","type":"switch","z":"d0a71ce6.110d8","name":"minute 15","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"15","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":680,"wires":[["c824c9c2.94f4c8"]]},{"id":"c6695639.c621f8","type":"switch","z":"d0a71ce6.110d8","name":"minute 17","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"17","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":720,"wires":[["b6347371.3da2c"]]},{"id":"c824c9c2.94f4c8","type":"debug","z":"d0a71ce6.110d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":680,"wires":[]},{"id":"b6347371.3da2c","type":"debug","z":"d0a71ce6.110d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":720,"wires":[]},{"id":"c6cac971.edcef8","type":"comment","z":"d0a71ce6.110d8","name":"Do something every day at 10:15AM","info":"","x":520,"y":640,"wires":[]},{"id":"8a3daff5.fc1dc","type":"comment","z":"d0a71ce6.110d8","name":"Do something every day at 10:17AM","info":"","x":520,"y":760,"wires":[]},{"id":"d0eaa338.6add2","type":"comment","z":"d0a71ce6.110d8","name":"Connected to listen TimeEventMinute","info":"","x":190,"y":640,"wires":[]},{"id":"cd2a9f30.2e52d","type":"link in","z":"d0a71ce6.110d8","name":"listen hour","links":["cfbff7e4.ff20d8"],"x":135,"y":510,"wires":[["b37c8d5d.c602a"]]},{"id":"b37c8d5d.c602a","type":"switch","z":"d0a71ce6.110d8","name":"hour 12","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"12","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":510,"wires":[["2a5460a8.da475"],["cc044898.badfb8"]]},{"id":"2a5460a8.da475","type":"debug","z":"d0a71ce6.110d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":430,"y":490,"wires":[]},{"id":"732f45d4.9c9a2c","type":"comment","z":"d0a71ce6.110d8","name":"Do something every day at midday","info":"","x":520,"y":450,"wires":[]},{"id":"f3683f54.3304d","type":"comment","z":"d0a71ce6.110d8","name":"Do something else on every other hour","info":"","x":530,"y":570,"wires":[]},{"id":"cc044898.badfb8","type":"debug","z":"d0a71ce6.110d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":430,"y":530,"wires":[]},{"id":"e7dc37b1.78e3b8","type":"comment","z":"d0a71ce6.110d8","name":"Connected to listen TimeEventHour","info":"","x":200,"y":460,"wires":[]},{"id":"8951381b.1126c8","type":"function","z":"d0a71ce6.110d8","name":"set upcoming time","func":"var upcoming = {date:31,hour:22,minute:15}\n// make object with separated units of time to make it\n//easely compareable with time events\n// and store somewhere. flow context will be fine\nflow.set(\"upcoming\",upcoming)\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":860,"wires":[[]]},{"id":"53bb14f8.fd710c","type":"inject","z":"d0a71ce6.110d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":860,"wires":[["8951381b.1126c8"]]},{"id":"435e96bd.8d5388","type":"link in","z":"d0a71ce6.110d8","name":"upcoming","links":["43cfb168.9a04b"],"x":85,"y":950,"wires":[["32197b88.1c98c4"]]},{"id":"32197b88.1c98c4","type":"switch","z":"d0a71ce6.110d8","name":"compare date","property":"upcoming.date","propertyType":"flow","rules":[{"t":"eq","v":"currenttime.date","vt":"global"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":950,"wires":[["44bc5416.1cc3ec"]]},{"id":"44bc5416.1cc3ec","type":"switch","z":"d0a71ce6.110d8","name":"compare hours","property":"upcoming.hour","propertyType":"flow","rules":[{"t":"eq","v":"currenttime.hour","vt":"global"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":950,"wires":[["2aeae7cf.cc37e8"]]},{"id":"2aeae7cf.cc37e8","type":"switch","z":"d0a71ce6.110d8","name":"compare minute","property":"upcoming.minute","propertyType":"flow","rules":[{"t":"eq","v":"currenttime.minute","vt":"global"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":950,"wires":[["789afae8.f77be4"]]},{"id":"789afae8.f77be4","type":"debug","z":"d0a71ce6.110d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":950,"wires":[]},{"id":"89f8a19d.3f139","type":"comment","z":"d0a71ce6.110d8","name":"Do something at the time stored in flow","info":"","x":310,"y":910,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [31 October 2019 23:31 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/16 "2019-10-31T23:31:00Z")

</div>

Has anyone here experience with calendar trigger? Unfortunately, this does not work for me.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [1 November 2019 01:14 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/17 "2019-11-01T01:14:13Z")

</div>

> [@Smarty](#):
>
> Has anyone here experience with calendar trigger?

Yes, I do. I have incorporated it into a calendar flow. You need to parse the date in as an array, even if it is only an array of 1.

It works fine, but is rather limited. So I was forking/updating it, until someone else came along and made my efforts redundant. 😢

---

<div class="post-metadata">

**Author:** ![Smarty](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Smarty](https://discourse.nodered.org/u/Smarty)\
**Post date:** [1 November 2019 07:47 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/18 "2019-11-01T07:47:43Z")

</div>

How can I parse the date into an array?

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [1 November 2019 09:45 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/19 "2019-11-01T09:45:39Z")

</div>

Take a look here [https://www.w3schools.com/js/js\_date\_formats.asp](https://www.w3schools.com/js/js_date_formats.asp) at the proper formatting needed (specifically at the ISO Dates (Date-Time) section).

Once your date is formatted then just use msg.dates[0] = _your properly formatted date_ in a Function node, and parse this to the Calendar-Trigger node.

---

<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:** [1 November 2019 10:33 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/20 "2019-11-01T10:33:47Z")

</div>

As well as using a function node and JavaScript, you can do it with a change node and JSONata.

This is a guess, I haven't tried it but something like:

```auto
[payload]

```

might do the job. Assuming the data is on the payload.

[Next page](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373.md?page=2)
