Help - Need some advice

I'm using Node Red & Raspberry Pi, to excute an Api call once daily. The Api retrieves the data from Gas wells that we have across the US. This amounts to 111 wells the data is returned back in Array like this. (Please Note: I'm new to Node Red)

object[0]
meterid=123456
DP=13.6
AP=17.56
Gas (mCF)=138.96

object[1]
meterid=456789
DP=14.6
AP=19.56
Gas (mCF)=183.96

object[2]
meterid=963258
DP=23.6
AP=27.56
Gas (mCF)=238.96

And so on all the way to object[111]

Now I going to publish this to our Mqtt broker using Sparkplug b. I know how to make this work for a single object, but I can't figure out how to move through each object . I could hard code everything but I'm sure there is a better way.

Would like any advice?

You can use the split node and then deal with each msg. separately

if you use a function node:

for(let x in msg.payload){
    //Perform some operations on the data for msg.payload[x].
}

Another variation that makes coding a little easier:

for(let x of msg.payload){
    //Perform some operations on the data for x.
}

The first variation uses x as an index to the position in the original variable. So you would reference it as something like msg.payload[x].DP to get the value for DP. The second variation actually makes x a copy of msg.payload[x] so that you can use x by itself like x.DP. If you know what you want to do with the components, just put that in the for loop and reference your variables like I showed you.

If you want to use a function, that is...

I could split things out and thanks for the input.

I do want to look at what @madhouse suggest , It's just that since I'm trying to learn Javascript I was hoping for an easy way. But maybe I need to just dive in with both feet.

Thanks

That's a fair statement for anyone to make. Javascript is a very easy language and it's well documented all over the place. It's also a beast to learn and work with because it has so much it can do. And then there's all the variants like Node.js and Angular that carry most of the functionality, but not all. The good news is you don't have to bite off huge chunks and try to figure them out as you go. Node-Red makes it so you can put the hard parts together in nodes and then slowly either replace standard nodes with custom code or add specific functionality you can't find a node for.

If you want, I can walk you through any coding you may want to try. I'm no expert by any means. But a lot of my flows rely on functions instead of nodes because I find adding a couple lines of code into a function is a lot easier than adding several nodes. Just let me know.

2 Likes

Thank you so much, I'm sure I will be asking for your help.

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