# 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:** 14\
**Page:** 2

<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 10:39 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/21 "2019-11-01T10:39:01Z")

</div>

Yes, there a few ways to achieve it, even without that JSONata thing. 🤪

You could probably use the Moment node (thought someone would've mentioned already) to format the date (might be easier actually) and then a Change node to parse msg.dates.

Look Ma, no JSONata 😂

---

<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 12:11 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/22 "2019-11-01T12:11:32Z")

</div>

Well, except that would be a lot more complex. JSONata is a real boon for simple transformations, often much easier than JS in a function node.

Not always so for more complex transforms, especially if you already know JS.

Moment is great (well after all, I wrote the node) if you need to handle more complex date/time format transforms, especially with locale/dst transforms or simple offsets. But is overkill for simpler things.

---

<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 16:09 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/23 "2019-11-01T16:09:51Z")

</div>

You helped me a lot! I Didn't know an array is such easy and the Moment Node is fantasic!

---

<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 20:57 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/24 "2019-11-01T20:57:12Z")

</div>

Oh no, it doens't work. ☹  
Do you have an example for me where I can guiess where my problem is? I have no idea whats going wrong.

---

<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 21:51 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/25 "2019-11-01T21:51:48Z")

</div>

Can you put a debug node so that you get the output that you are feeding into moment? Also if you could re-explain what you think has gone wrong and what you expect so see?

---

<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 22:52 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/26 "2019-11-01T22:52:01Z")

</div>

After trying for a while to feed the trigger, I first did the thing manually. I set "DateVariable" manually. msg.dates [0] = mydate  
Where I have the variable mydate about 10 minutes further.  
Debug Error = "TypeError: Can not set property '0' of undefined"

I do not understand, because I have paid extra attention that this is a Date variable. If I set the variable msg.dates = mydate, I do not get an error message, but nothing happens. Of course, I have made sure that the variable is an ISO date: 2019-11-01T22: 45: 00

---

<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:** [2 November 2019 00:30 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/27 "2019-11-02T00:30:27Z")

</div>

Sorry, you'll need to post your actual flow I think.

---

<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:** [2 November 2019 04:44 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/28 "2019-11-02T04:44:20Z")

</div>

> [@TotallyInformation](#):
>
> Moment is great (well after all, I wrote the node)

That is why I mentioned it, but I use it for simpler things as well. I think you are being too harsh about it being overkill tbh.

@Smarty as Julian says please post a flow example. And you have put a Debug node on the end obviously? Just checking 🙂

---

<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:** [2 November 2019 08:19 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/29 "2019-11-02T08:19:54Z")

</div>

I just play arround to find out how does that work, I am still leraning. 😇  
So I'm trying to get out how the variable works and you will find a manually date in my function node, which I change manually at any time.  
As soon as I press the timestamp button I get this error message:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fb2472991f028391cf1f6fc38295f8e1a47b9311.png)

This is my function node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/63f008d10e38aab6f20e7d5399a67fb639eabe9c.png)

this is the whole flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f6b2e011f97bd079f270ca52bbea3437069329a4.png)

Here is the flow:  
[{"id":"6c4e0b56.340e54","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"268bd29b.176e5e","type":"function","z":"6c4e0b56.340e54","name":"Setting Time","func":"var mydate = new Date("2019-11-01T22:45:00z");\n//msg.dates = "2019-11-01T23:20:00Z";\n//return mydate;\nmsg.dates[0] = mydate;\n\n\n\n","outputs":1,"noerr":0,"x":470,"y":220,"wires":[["dd1bf695.114358","e22db3ff.32b"]]},{"id":"8d9c2d86.b81d8","type":"inject","z":"6c4e0b56.340e54","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":220,"wires":[["268bd29b.176e5e"]]},{"id":"dd1bf695.114358","type":"debug","z":"6c4e0b56.340e54","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":360,"wires":},{"id":"e22db3ff.32b","type":"calendar-trigger","z":"6c4e0b56.340e54","name":"Time-Trigger","x":670,"y":220,"wires":[["8b7c16b.e7377e8","e331af53.ccd56"]]},{"id":"e331af53.ccd56","type":"cast-to-client","z":"6c4e0b56.340e54","name":"Radio","url":"xxx.mp3","contentType":"","message":"","language":"","ip":"192.168.178.31","port":"","volume":"25","x":890,"y":140,"wires":[]},{"id":"8b7c16b.e7377e8","type":"change","z":"6c4e0b56.340e54","name":"Switch on the light","rules":[{"t":"set","p":"payload","pt":"msg","to":"{"NewAIN":"XXXX","NewSwitchState":"ON"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":220,"wires":[["69319ef1.d043a"]]},{"id":"69319ef1.d043a","type":"fritzbox-in","z":"6c4e0b56.340e54","device":"eb9cf10f.83afb","name":"The lamp","service":"urn:dslforum-org:service:X\_AVM-DE\_Homeauto:1","action":"SetSwitch","arguments":"{"NewAIN":"value","NewSwitchState":"value"}","x":1240,"y":220,"wires":[]},{"id":"eb9cf10f.83afb","type":"fritzbox-config","z":"","name":"XXXX","host":"fritz.box","port":"49443","ssl":true}]

---

<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:** [2 November 2019 08:22 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/30 "2019-11-02T08:22:22Z")

</div>

msg.dates is never created hence the error.  
Add `msg.dates = []` before trying to populate it. Also, I would use `msg.dates.push(mydate)`

---

<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:** [2 November 2019 08:40 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/31 "2019-11-02T08:40:28Z")

</div>

Looks good to me! No more error-messages in the debug. 👍  
However the trigger is not still working... ☹

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 November 2019 10:40 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/32 "2019-11-02T10:40:54Z")

</div>

If all you did to the function was add the `msg.dates = []` what is returned `(hint hint)` from the function???

---

<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:** [2 November 2019 11:17 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/33 "2019-11-02T11:17:56Z")

</div>

Just to expand slightly on the variable creation issue.

If you want to populate either an object `{...}` or an array `[...]`, you do need to initialise the object/array first. JavaScript has several ways to do this but if you forget, you will get errors like this.

In the case of your function, you could do:

```javascript
var mydate = new Date() // gives you 'now' or use your version if you want to pin it to a specific date/time
msg.dates = [mydate] // returns as an array with a single entry

return msg // this is missing from your function example

```

Here is another alternative

```javascript
msg.dates = []

var mydate = new Date()
msg.dates.push(mydate)

return msg

```

Also, please remember to wrap your code in back-ticks - especially an example flow because otherwise, it cannot be imported.

---

<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:** [3 November 2019 01:07 UTC](https://discourse.nodered.org/t/start-a-flow-in-a-variable-time/17373/34 "2019-11-03T01:07:40Z")

</div>

Guys, it works!  
Thank you for your patience!

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