It is supposed to be an object, so I successfully modified jbudd's code.
Thank you both for making this first step possible!
On a related problem I would like to iterate through the length of an object but I would like to use the same code block to also process name key pair. I hope I said it right.
I am able to enter the for loop but I cannot get the value of "Power" for the second case because it is not an object, so it does not have the index 0.
for (let i = 0; i < (Array.isArray(msg.payload[parameter.split('.')[0]][parameter.split('.')[1]]) ? msg.payload[parameter.split('.')[0]][parameter.split('.')[1]].length : 1); i++) {
if (!Array.isArray(msg.payload[parameter.split('.')[0]][parameter.split('.')[1]])) {
i =""
}
flow.set(msg.topic + "/" + parameter.replace(".","") + [i], msg.payload[parameter.split('.')[0]][parameter.split('.')[1]][i])
...
//rest of code
As you can see I tried to initialize "i" as "" but that does not allow me to read msg.payload[parameter.split('.')[0]][parameter.split('.')[1]][i] and returns TypeError: Cannot set properties of undefined (setting '')
It would work if I removed the [i], so the rest seems to be fine. I just need to find a way to make the [i]"disappear" if isArray is false.
I am guessing that there is a really simple solution but I cannot figure it out
If that is the case, then I apologize because I then I do not understand how your code works
The content of msg.ValueDeltas depends on different conditions being met in my code. So I check the input data and depending on what it is and what the parameter is, I process it differently.
From my (possible poor) understanding, your code assumes that I always assing the value of msg.payload.WhatEverParameter and do not perform further processing.
And if I did, I would have to do the processing inside msg.ValueDeltas = {}. And have a separete processing block for TimeDelta and ValueDelta.
If that is the case, then that would also be a complete code re-write as creating those msg properties is only one command in a block of commands.
So you can define your process in the reduce function: actually the function adds the property key (e.g. "Power") to the object and the value as msg.payload.ENERGY[property] (e.g. "msg.payload.ENERGY.Power"). You can change the value to whatever you want.
There are two functions, one for ENERGY and the other for ANALOG, joining them would be difficult to reread and would complicate its role.
You can use Object.entries to convert object to array, if you prefer