# How to format energy data

**URL:** <https://discourse.nodered.org/t/how-to-format-energy-data/82237>\
**Category:** General\
**Created:** [21 October 2023 09:12 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237 "2023-10-21T09:12:43Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 October 2023 13:33 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/21 "2023-10-21T13:33:59Z")

</div>

> [@AleXSR700](#):
>
> ```auto
> for (let parameter of Parameters) {
> myValueDeltas.push({ parameter : msg.payload }) // this uses "parameter" as name but should use the value of parameter
> msg.ValueDeltas = myValueDeltas
> }
> 
> ```

I think you need to use the alternative syntax with square brackets to construct an object:

```auto
for (let parameter of Parameters) {
    let myobject = {}
    myobject[parameter] = msg.payload
    myValueDeltas.push(myobject)
    msg.ValueDeltas = myValueDeltas
}

```

EDIT: maybe this answer has been made irrelevant by subsequent events...

> [@AleXSR700](#):
>
> The object below ValueDeltas should not be named "0" but "ENERGY.Power".

The object below Value Deltas is not named "0"!  
As I said earlier on, the debug output prints the numerical index of array elements.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [21 October 2023 13:38 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/22 "2023-10-21T13:38:45Z")

</div>

> [@AleXSR700](#):
>
> `msg.payload[parameter.split('.')[0]]`

Yes, and I am 90 % there.

```auto
let Parameters = ["ENERGY.Power", "ENERGY.Today", "ENERGY.Voltage", "ENERGY.Current", "ENERGY.Factor", "ANALOG.Temperature"]
let myValueDeltas = []

for (let parameter of Parameters) {
    myValueDeltas.push({ [parameter.split('.')[0]] : { [parameter.split('.')[1]]: msg.payload} })
    msg.ValueDeltas = myValueDeltas
}    
return msg;

```

but the first object should not be 0 but already "ENERGY"

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/710fa0378b0a1dec8de5ca2280d6d9f62fc002f6.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 October 2023 13:40 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/23 "2023-10-21T13:40:14Z")

</div>

See the edit in my post immediately above.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9500d4ab0869e66cc4543e96413afe1b49ba111.png)

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [21 October 2023 13:42 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/24 "2023-10-21T13:42:31Z")

</div>

> [@AleXSR700](#):
>
> I did not specify this earlier because I would never have thought of your solution and what that would mean, but I am creating multiple new properties in my code, so with your approach I would have to execute all of the code once per msg property.... I think.

No, the loop is iterated for each element in the array

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [21 October 2023 13:47 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/25 "2023-10-21T13:47:12Z")

</div>

Oh, I just need to make it an object instead of an array. I did not realize that you had stated it as `= {}`.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [21 October 2023 14:08 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/26 "2023-10-21T14:08:38Z")

</div>

@AleXSR700 do you want `msg.ValueDeltas` to be an array or an object?

> [@AleXSR700](#):
>
> Bascially I want to create an object like "payload" in the picture which is not named "payload" but ValueDeltas.

So `msg.ValueDeltas` is an object in which you want certain properties of `ENERGY` object and `ANALOG` object

The result will be:

```js
{
    payload: {...},
    ValueDeltas: {
      ENERGY: {
        Power: [0, 1, 2], 
        Today: [0, 1, 2],
    }
}

```

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [21 October 2023 17:52 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/27 "2023-10-21T17:52:12Z")

</div>

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.

So I want to be able to process this input:

```auto
{
    "Time": "2023-10-21T18:49:01",
    "ENERGY": {
        "TotalStartTime": "2023-08-31T12:04:17",
        "Power": [
            9,
            10
        ]
    }
}

```

but also this input

```auto
{
    "Time": "2023-10-21T18:49:01",
    "ANALOG": {
        "Temperature": 70
    },
    "ENERGY": {
        "TotalStartTime": "2023-09-01T21:46:07",
        "Power": 5
    }
}

```

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.

```auto
            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 ☹

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [21 October 2023 18:04 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/28 "2023-10-21T18:04:26Z")

</div>

I don't understand why you don't use my code, it does everything you ask, no need to get into hellish loops

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [21 October 2023 18:18 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/29 "2023-10-21T18:18:48Z")

</div>

> [@AleXSR700](#):
>
> ```auto
> if (!Array.isArray(msg.payload[parameter.split('.')[0]][parameter.split('.')[1]])) {
> i =""
> }
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [21 October 2023 18:29 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/30 "2023-10-21T18:29:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 December 2023 18:29 UTC](https://discourse.nodered.org/t/how-to-format-energy-data/82237/31 "2023-12-20T18:29:21Z")

</div>

Closed on 20 December 2023 18:29 UTC

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

[Previous page](https://discourse.nodered.org/t/how-to-format-energy-data/82237.md?page=1)
