Loop a subflow to produce an array of values

Hi everybody,

I'm quite new to Node-RED and, being a novice programmer, I only know some very basic Javascript.

I have created a sub-flow which reads a particular property (a bit value) in a BACnet object. I have created an environmental variable which takes an integer (representing a BACnet instance), and the output provides me with the bit value, 0 or 1, when I inject a timestamp.

I'd like to loop this subflow so that it will run through a range of instances, and provide me with an array of bit values. I've done quite a lot of Googling but the stuff I have seen baffles me a bit.

Can anybody help, keeping any explanation really simple for a newbie?

Thanks,
Warren

OK so to define your problem down further

You want to create an array of values - each value with hold the BACnet bit value (1 or 0)

If you go to Chat GPT and type the following

I am using Node Red 4.02
I would like to create a function to populate an array - the array can be of indeterminate length
Each array member can hold a binary value of 0 or 1

Please write this using javascript as a Node red Function code and describe the steps to follow to make this into firstly a standard flow - and then show me what additional steps would be required to turn this into a Re-usable Node Red Sub Flow

Try that and see how you go

I very rarely handcraft basic javascript anymore

Craig

Hi Craig, thanks for replying.

I'd never looked at ChatGPT before this so it was all new to me. At first it didn't seem to help, but I persisted and eventually got it to spit out something which almost worked. I certainly have a flow that loops the sub-flow (for simplicity, I switched to reading the Present_Value of the desired BACnet points). The sub-flow loops away and the results for each iteration appear sequentially in the debug screen.

However, when I read the "results" array back, it is full of what appear to be epoch timestamps. To try and figure out where it had gone wrong I got ChatGPT to write a very basic loop-to-array function for me, without referring to a subflow. I'm not sure where it's gone wrong in looping my subflow, but it seems to be where the output of the subflow is read by the loop function.

My flow is attached.

myFlow.txt (9.5 KB)

Yep if you are new to something then always break it down into modules and easy to debug steps in your head.

I use Chatgpt and/or Gemini for nearly allmy stuff now - and as long as you break things into chunks you can test and find errors in - and then have chatgpt build ontop of those solutions you can get through most of it.

I have not looked at your code - but are you sure (for example) that you are storing the data in a Global Context variable - otherwise it will not be available between flows

Craig

I'm working entirely within one flow.

Here you go, Craig. The final working flow:

Looping a subflow in Node-Red.txt (9.2 KB)

It uses node-bacnet-contrib-extended to read the BACnet objects.

ChatGPT was actually quite helpful. Thank you, I never would have thought to use it without your prompt.

Warren

1 Like

No worries - please mark it as the solution

Craig

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