# Function node question

hi all... i have a problem im trying to solve in function node.. i have 4 cameras named "Wide" "Left" "Center" "Right". At any given time one of them is active which is put in a context variable "active"..

i want a function node (when engaged) to learn the active camera, then make a random decision to put the other 3 cameras which arent active into function node variables called cam2, cam3, cam4.. each time the node is engaged i want cam2 cam3 and cam4 to have different variations of where they "land"..

I know how to put the active camera in the function node variable (flow.get) and i have some long winded method of making a random decision for the other 3 cameras but its not very good.. wondering if anyone might know an effective way ? thanks..

It might help if you share your flow so we can better understand what you are trying to do.

I think this is what you are looking for, let me know if you need more help ?

``````[{"id":"6473791628a7893b","type":"function","z":"b42d972d049a7da8","name":"function 32","func":"flow.set(\"active\", msg.payload)\n\n// active camera from flow\nlet activeCamera = flow.get(\"active\");\n\n\nlet positions = [\"Wide\", \"Left\", \"Center\", \"Right\"];\n\n// Remove the active camera\nif (activeCamera) {\n    const index = positions.indexOf(activeCamera);\n    if (index !== -1) {\n        positions.splice(index, 1);\n    }\n}\n\n// shuffle \nfor (let i = positions.length - 1; i > 0; i--) {\n    const j = Math.floor(Math.random() * (i + 1));\n    [positions[i], positions[j]] = [positions[j], positions[i]];\n}\n\n// Store values in flow variables\nflow.set(\"cam1\", positions[0] || \"\");\nflow.set(\"cam2\", positions[1] || \"\");\nflow.set(\"cam3\", positions[2] || \"\");\n\nmsg.payload = positions;\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":435,"wires":[["a0d9bfd6cec59452"]]},{"id":"eb93680a217d607c","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Wide","payloadType":"str","x":515,"y":360,"wires":[["6473791628a7893b"]]},{"id":"a0d9bfd6cec59452","type":"debug","z":"b42d972d049a7da8","name":"debug 393","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":435,"wires":[]},{"id":"2c1b86b5890cdeb5","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Left","payloadType":"str","x":515,"y":405,"wires":[["6473791628a7893b"]]},{"id":"afa3490cedfe8993","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Center","payloadType":"str","x":515,"y":450,"wires":[["6473791628a7893b"]]},{"id":"fef2735830088598","type":"inject","z":"b42d972d049a7da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Right","payloadType":"str","x":515,"y":495,"wires":[["6473791628a7893b"]]}]
``````

thanks i'll give it go..

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.