Write code for a nodered function node to split the openbeken config payload into individual messages.
Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie tam{
"dev": {
"ids": [
"Pow_187"
],
"name": "Pow_187",
"sw": "1.17.293",
"mf": "Beken Corporation",
"mdl": "BK7231N",
"cu": "http://192.168.178.187/index"
},
"name": "voltage",
"~": "pow_187",
"avty_t": "~/connected",
"uniq_id": "Pow_187_sensor_0",
"qos": 1,
"dev_cla": "voltage",
"unit_of_meas": "V",
"stat_t": "~/voltage/get",
"stat_cla": "measurement"
}
erhardfalkenberg wrote:Hello,(...) ation",
"mdl": "BK7231N",
"cu": "http://192.168.178.187/index"
},
"name": "voltage",
"~": "pow_187",
"avty_t":"~/connected",
"uniq_id": "pow_187_sensor_0",
"qos": 1,
"dev_cla": "voltage",
"unit_of_meas": "V",
"stat_t":"~/voltage/get",
"stat_cla": "measurement"
}
{
"dev": {
"ids": [
"Pow_187"
],
"name": "Pow_187",
"sw": "1.17.293",
"mf": "Beken Corporation",
"mdl": "BK7231N",
"cu": "http://192.168.178.187/index"
},
"name": "voltage",
"~": "pow_187",
"avty_t": "~/connected",
"uniq_id": "Pow_187_sensor_0",
"qos": 1,
"dev_cla": "voltage",
"unit_of_meas": "V",
"stat_t": "~/voltage/get",
"stat_cla": "measurement"
}
// Get the input JSON payload from the message
var inputPayload = msg.payload;
// Check if the payload is an array
if (Array.isArray(inputPayload)) {
// If it's an array, split it into individual messages
for (var i = 0; i < inputPayload.length; i++) {
// Create a new message for each element in the array
var newMsg = {
payload: inputPayload[i]
};
// Send the new message to the appropriate output
if (i < node.outputs) {
// Send to the corresponding output if available
node.send([newMsg, null]);
} else {
// If there are more elements than outputs, send the remaining elements to the last output
node.send([null, newMsg]);
}
}
} else {
// If the payload is not an array, just pass it through
node.send([msg, null]);
}
return null; // Return null to end the function node[/i]
erhardfalkenberg wrote:It looks like you just shared a JavaScript code for Node-RED that processes an incoming message. The code checks if the incoming payload is an array. If so, each element of the array is split into a separate message and forwarded to the appropriate outputs. If there are more array elements than outputs, the additional messages are all sent to the last output. If the incoming payload is not an array, the message is passed through unchanged.(...) If there are more elements than outputs, send the remaining elements to the last output
node.send([null, newMsg]);
}
}
} else {
// If the payload is not an array, just pass it through
node.send([msg, null]);
}
return null; // Return null to end the function node