hi... is there anyway to use inject node to start code in a function node which repeats as a loop and updates msg.payload at the end of each loop cycle ? and then continues to loop until another inject node is triggered to stop the function repeating ?
i read somewhere that loops can cause node red to stop working but is there any creative method to achieve something along these lines ? i cant really use the repeat at intervals option within the inject node as im trying to make the function node update msg.payload at varying intervals and if the inject node repeats at set intervals it will interfere with the aim of the function..
i downloaded a couple of loop nodes but wasnt sure if there is a way to achieve that with them ? im still learning Node Red and stumbling my way around javascript.. appreciate any help..
A node can only do one thing at a time, processing it's current message.
So yes you can have a loop in a function node, but the node won't be aware that you have sent another message "stop" until it has finished with the current one.
You could, within your loop, check the value of a flow context variable and break out of the loop if it equals "stop".
But if the function node is busy doing it's loop, does the part of your flow which sets the variable ever get to run? I'm not sure.
if you do a continuous loop in a function node-red will not run correctly. In fact this is the opposite of how an event driven environment (aka node-red) should be used.
If you clarify what you are doing and what the end goal is, someone will show you how.
In many cases, a JS loop is not even required - even where you think it is! It can often be achieved with the split - > change -> join nodes pattern.
I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.
thanks everyone for the help.. since i posted earlier i have been sitting here working on the flow and now have it doing most of what i need (in principle at least).. i have an inject node starting the flow which then goes to a function node that randomly selects a value from an array and then steps through a sequence of other nodes including delays and if/else queries and that seems to be all working..
then at the end of the flow i have the last node connected back to the function node with the array and the whole flow cycles around again.. which has solved the loop thing i was asking in the original post...
So that is all good.. however the only problem now is once i press the first inject to "start" the flow keeps cycling around but is there anyway to add a second inject node that will "stop" the loop ? currently (to make it stop) im just breaking the connection between the last node and the function node and redeploying it..
inject1--> set flow.enable to true --> start loop --> switch node (is flow.enabled == true?) --> do stuff --> next loop
flow 2
inject2 --> set flow.enable to false
ok, so back to what I said earlier...
If you actually share your work and fully explain your end goal, I am certain there will be there is a better way than looping. There almost always is - you just have to approach the thing differently.
Or feed the two inject nodes in to the function node with different topics and use the Stop topic to prevent further operation until you get a Start topic. However, I agree with Steve, there may well be a better way of approaching the whole problem.
cool thanks... i'll give those ideas a go tommorow when i look at it again..
i started a large post earlier to describe what i was trying to achieve but i was finding it confusing to describe so i went back to trying some things and found i was making some progress so kept going..
i am aiming to automate a PTZ camera.. i want to put it near a stage and store half a dozen preset positions and then recall those positions from a RPi and Node Red. however i didnt want to just randomly select 1 through 6 presets each time and always wait a fixed period, i wanted some presets to be prioritized more than others and the intervals for staying on each preset to vary.. also some zoom to occasionally occur...
ive managed to get something along those lines together.. if i can get the inject to stop it cycling it should do what i need.. hopefully....
appreciate all the help, i'll try the other points mentioned tomorrow.. thanks
i wanted the sequence to follow a basic process but have some variety.. it runs something like this...
function node randomly picks a number ranging from 1 to 6 from an array of 20 values.. (this chooses a camera preset).. i want presets 1&2 to be selected around 60% of the time so i have them across first 12 array values and then numbers 3-6 across the other 8 values..
(i thought there might be a better way to do this but this made sense for me and i can easily change priorities by changing the array)
send preset command to move camera using HTTP get..
wait a random time interval between 10-15 secs (so camera remains on chosen preset)
choose a number from an array to determine a zoom decision yes or no..
if zoom equals YES.. zoom for a few seconds.. then send stop command and stay stationary for another random interval.. then loop back to step 1 to choose another camera preset from first array and repeat sequence.
if zoom decision equals NO wait a random interval of time and then return back to step 1 where the array chooses another preset and start sequence again..
i am still playing round with the delay times but essentially the goal is to just have it automate with some variety... ultimately i want to have a RPi running this and use its GPIO to add hardware start and stop buttons.. i just need to work the stop inject out.. i will try the suggestions today.. thanks all for the help..
thanks mate.. i did watch that video series you linked a few weeks ago.. at that time i felt i grasped most of it but then its not until you do things that they sink in further.. i'll watch it again..
i think ive managed to get the Inject switch thing happening also.. i did a search for your suggestion of set flow.enable true and the search returned another Node called "traffic light" which seems to do what i need... not sure if there are any pros or cons to using one method over another ? but so far so good..