How to use msg.payload as an index in a change node?

#1

(This is a hallowen project)
In an change node, I've built a flow object 'flow.wolf' that is an array

21%20AM

I then pick a random number between 0 and 2 using the random node so msg.payload now contains a number 0, 1 or 2. I feed this to another change node and I want to use msg.payload as the index of the flow object but I can't get it to work. In the change node I have:

57%20AM

But msg.audioURI is not being set. ???

Here is the flow:

[{"id":"86d7e7ad.263bc8","type":"change","z":"df3238f6.f811b","name":"","rules":[{"t":"set","p":"wolf","pt":"flow","to":"{\"howl\":[\"wolf_howlin.wav\",\"wolf2.wav\",\"wolf3.wav\"]}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":130,"y":140,"wires":[["4ebe550d.f715b4"]]},{"id":"f8b66d0a.a6708","type":"inject","z":"df3238f6.f811b","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":100,"wires":[["86d7e7ad.263bc8"]]},{"id":"4ebe550d.f715b4","type":"random","z":"df3238f6.f811b","name":"","low":"0","high":"2","inte":"true","property":"payload","x":120,"y":180,"wires":[["c787a992.59da4"]]},{"id":"c787a992.59da4","type":"change","z":"df3238f6.f811b","name":"Set audioURI to random sound","rules":[{"t":"set","p":"audioURI","pt":"msg","to":"wolf.howl[msg.payload]","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":190,"y":220,"wires":[["7726d7b9.dc03f"]]},{"id":"7726d7b9.dc03f","type":"debug","z":"df3238f6.f811b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":110,"y":260,"wires":[]}]
'''
0 Likes

"change node" Variables in key
#2

The flow/global types of the TypedInput field doesn't support referencing message properties. To do that, you have to use the Expression type with some JSONata:

$flowContext('wolf').howl[$$.payload]
0 Likes

#3

Excellent! Thanks! (and why didn't I think about using JSONata...sigh)

0 Likes