Start a flow in a variable time

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

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 :joy:

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.

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

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

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?

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

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

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 :slight_smile:

I just play arround to find out how does that work, I am still leraning. :innocent:
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

This is my function node:
image

this is the whole flow:

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}]

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

Looks good to me! No more error-messages in the debug. :+1:
However the trigger is not still working... :frowning:

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

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:

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

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.

Guys, it works!
Thank you for your patience!