This looks awfully complex - and possibly not necessary. The loops are certainly not recommended. What is this doing? Perhaps you could share this part of the flow?
The flow then goes to the getter.
The switch node receives the payload from the getter and sends the flow on where msg.trip is equal to 1.
The next function node then process the data and makes another request object.
const obj = {}
const fields = msg.fields
msg.payload.data.forEach((data, index) => {
if(fields[index].field == "PID effect") {
obj[fields[index].field] = data
return
}
obj[fields[index].field] = Number(calculateFromRawValueMA(data).toFixed(1))
})
msg.influxObj.Temperature = obj
msg.fields = context.get("Holding")
msg.payload = {
'fc': 3,
'unitid': 1,
'address': 0,
'quantity': msg.fields.length
}
msg.trip = 2
return msg;
function calculateFromRawValueMA(mA) {
// 4-20mA signal
// -50 to 150 range
const PvHigh = 150 // Process value high
const PvLow = -50 // Process value low
const Ihigh = 20000 // mA high
const Ilow = 4000 // mA low
const I = parseInt(mA) // Amp/Process value
const Pv = (PvHigh - PvLow)/(Ihigh - Ilow)*(I-Ilow)+PvLow // Process value
return Pv
}
Then the flow goes to a 100ms delay to the getter.
This repeats itself until the end, where all the data is formatted pr influxdb standard and sent to influx.
The reason for the number of loops is requesting between different modbus functions and some of them because of several thousands difference in modbus addresses in the same modbus function...
if that makes any sense.