Injecting arrays into flow

50 views
Skip to first unread message

Simon Walters

unread,
Feb 21, 2018, 3:36:14 AM2/21/18
to Node-RED
Hi
I'm using NodeRed to listen out for cheerlights colour and then send midi sequences off to my electronic piano

e.g it plays 99 red balloons when it receives red, Greensleeves for Green

so I have a Change node that is triggered by incoming cheerlight message and current it sets

msg.payload to "[[[144,66,64],0],[[144,68,64],300],[[144,64,64],500],[[144,68,64],1100],[[144,66,64],1600],[[144,64,64],1800],[[144,61,64],2000]]"

(Pairs or arrays containing the midi values and the delay time to space them out)

I then have a JSON node set to convert the string to a javascript object - then a split node which extracts each array items e.g [[144,66,64],0] - and then other nodes to send each sequence onwards 

My issue is that I feel that I shouldn't have to set the payload to a string and then have another node to convert it to an object

But for the life of me, I can't work out how to just do this with my Change node - i try doing things like selecting {} JSON and sticking [[[144,66,64],0],[[144,68,64],300],[[144,64,64],500],[[144,68,64],1100],[[144,66,64],1600],[[144,64,64],1800],[[144,61,64],2000]] as the value but that give an error

I feel there must be a way to directly get my array into the flow but its eluding me

I'm sure this is just down to my lack of understanding of javascript/JSON stuff :(

Simon




Dean Cording

unread,
Feb 21, 2018, 3:56:16 AM2/21/18
to Node-RED
Works OK for me:

[{"id":"11ff4d39.11b3a3","type":"change","z":"1c429fc1.6372a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[[[144,66,64],0],[[144,68,64],300],[[144,64,64],500],[[144,68,64],1100],[[144,66,64],1600],[[144,64,64],1800],[[144,61,64],2000]]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":1140,"wires":[["8c7ef179.6bf8c"]]},{"id":"587d383c.ecb1e8","type":"inject","z":"1c429fc1.6372a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":1080,"wires":[["11ff4d39.11b3a3"]]},{"id":"8c7ef179.6bf8c","type":"debug","z":"1c429fc1.6372a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":500,"y":1080,"wires":[]}]

Simon Walters

unread,
Feb 21, 2018, 4:12:00 AM2/21/18
to Node-RED
#LOL

Of course - it now works for me as well :)

I honestly spent at least an hour going round and round in circles yesterday.

Obviously, my gremlins have had their fun and moved to some other part of my computer :)

Ta

Simon 

Garry Hayne

unread,
Feb 21, 2018, 5:10:58 AM2/21/18
to Node-RED
In a change node choose  JSONata (J:) from the dropdown and put ([[[144,66,64],0],[[144,68,64],300],[[144,64,64],500],[[144,68,64],1100],[[144,66,64],1600],[[144,64,64],1800],[[144,61,64],2000]]) in the expression, this will put an array of arrays in the payload.

Garry

Garry Hayne

unread,
Feb 21, 2018, 5:20:34 AM2/21/18
to Node-RED


On Wednesday, February 21, 2018 at 8:36:14 AM UTC, Simon Walters wrote:
Hi
I'm using NodeRed to listen out for cheerlights colour and then send midi sequences off to my electronic piano

Simon, I would love to know how you are interfacing node-red with your piano, I was thinking of doing something similar with my Nord Rack 2X Synthesiser.

Garry 

Simon Walters

unread,
Feb 21, 2018, 9:07:41 AM2/21/18
to Node-RED
Hi Garry
I've got a Yamaha PSR-EW400 which has usbmidi interface and I've just plugged into my RaspberryPi and imported the node-red-contrib-midi from the pallette

You can then just send midi sequences as triplet arrays [144,60,64] plays note on, middle c, velocity =64

my current flow is
[{"id":"5365e08b.f65b7","type":"inject","z":"7698c679.9464c8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":440,"wires":[["5b2da726.a730e8"]]},{"id":"5b2da726.a730e8","type":"change","z":"7698c679.9464c8","name":"Set Notes Red","rules":[{"t":"set","p":"payload","pt":"msg","to":"[[[144,66,64],0],[[144,68,64],300],[[144,64,64],500],[[144,68,64],1100],[[144,66,64],1600],[[144,64,64],1800],[[144,61,64],2000]]","tot":"json"},{"t":"set","p":"delay","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":440,"wires":[["8c711781.e0ef48"]]},{"id":"8c711781.e0ef48","type":"function","z":"7698c679.9464c8","name":"Fan In","func":"\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":420,"wires":[["83e24321.71d46"]]},{"id":"83e24321.71d46","type":"split","z":"7698c679.9464c8","name":"","splt":":","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":710,"y":420,"wires":[["e0caf71e.3af558"]]},{"id":"e0caf71e.3af558","type":"change","z":"7698c679.9464c8","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"payload[1]","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload[0]","tot":"msg"},{"t":"set","p":"delay","pt":"flow","to":"delay + flow.delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":420,"wires":[["69cc0da2.69f884","b4b5cfbb.e5f0b"]]},{"id":"69cc0da2.69f884","type":"delay","z":"7698c679.9464c8","name":"Variable Delay","pauseType":"delayv","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1060,"y":420,"wires":[["e16ad1a.f8b003"]]},{"id":"e16ad1a.f8b003","type":"function","z":"7698c679.9464c8","name":"Fan In","func":"\nreturn msg;","outputs":1,"noerr":0,"x":1190,"y":360,"wires":[["309d7fd3.e7","f60edd36.595f5"]]},{"id":"309d7fd3.e7","type":"midi out","z":"7698c679.9464c8","name":"MidiOut","midiport":"1","x":1320,"y":460,"wires":[]}]

but I'm working on making it better as I really just want to specify notes as C2,E3 etc and I want the delay times to be relative and not absolute

Simon
Reply all
Reply to author
Forward
0 new messages