# Wrong Value in Array

**URL:** https://discourse.nodered.org/t/wrong-value-in-array/80374
**Category:** General
**Tags:** function-node
**Created:** [7 August 2023 12:17 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374 "2023-08-07T12:17:08Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Phant0m-code](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phant0m-code/32/81819_2.png) [@Phant0m-code](https://discourse.nodered.org/u/Phant0m-code)
#### Post date: [7 August 2023 12:17 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/1 "2023-08-07T12:17:08Z")

</div>

Hello my dear friends, I need to create a small system where three input values of the power phase need to be summed. For this, I am using the "join" node to create an array. But the problem now is that the values from the array do not match the individual values in the power phase nodes. Can someone show me how to create a simple array with three values (coming from different nodes). Or can someone help me sum three values from three different nodes? I WOULD greatly appreciate any advice.

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

And as u can see at this picture below it does not make any sense.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e6de0944ff3f5d215602c55c329698e0f362d4f.png)  
'  
and again i would really appreciate any help i can get from you guys.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [7 August 2023 12:28 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/2 "2023-08-07T12:28:17Z")

</div>

My guess is that the 'phase 2' node is sending a msg, then the 'phase 3' node is sending 2 msg's and since the join node is looking for three messages is sends those three messages.

You could look at the sidebar debug and look at the timestamps for the messages to check this out.

Let me think about how you could only allow one from each to get thru....

What would you want to occur it phase 1 sent a message with a value and then phase 2 sends two messages one with a value of 100 and the second with 200...which value would you want?

---

<div class="post-metadata">

### Author: ![Phant0m-code](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phant0m-code/32/81819_2.png) [@Phant0m-code](https://discourse.nodered.org/u/Phant0m-code)
#### Post date: [7 August 2023 12:34 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/3 "2023-08-07T12:34:12Z")

</div>

First of all thx for your fast respond 🙂 So your first guess sound pretty good i think. but yea the question is how can i "collect" three values (from Phase1,2 and 3 ) create an array and send it to the next node. and not mixing it up with different values if u know what i mean.

---

<div class="post-metadata">

### Author: ![Phant0m-code](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phant0m-code/32/81819_2.png) [@Phant0m-code](https://discourse.nodered.org/u/Phant0m-code)
#### Post date: [7 August 2023 12:36 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/4 "2023-08-07T12:36:51Z")

</div>

> [@zenofmud](#):
>
> What would you want to occur it phase 1 sent a message with a value and then phase 2 sends two messages one with a value of 100 and the second with 200...which value would you want?

well i dont exactlly understand what u mean with this but as i said. the three values from the same timestamp sould be joined into one array. 😕

At the end it sould be something like this (There are also the values from the same time (i guess) ) ....  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/60ff4f62ac356b427d83eb926038ce65830b0313.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [7 August 2023 12:41 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/5 "2023-08-07T12:41:22Z")

</div>

I suggest using the Join node in key/value mode, and make sure the three inputs have separate topics. Then you will be guaranteed to get all three inputs. Then you can organise the data however you want.

---

<div class="post-metadata">

### Author: ![Phant0m-code](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phant0m-code/32/81819_2.png) [@Phant0m-code](https://discourse.nodered.org/u/Phant0m-code)
#### Post date: [7 August 2023 12:52 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/6 "2023-08-07T12:52:10Z")

</div>

i will try it and let you guys know if it helped. thank you in advice

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [7 August 2023 12:57 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/7 "2023-08-07T12:57:07Z")

</div>

> [@Phant0m-code](#):
>
> but as i said. the three values from the same timestamp sould be joined into one array.

I didn't see that in the previous pots but I was going to suggest the same thing as @Colin did, add a unique topic to each msg and then you will have an object.

However if you want ALL the data in each msg you will have to change things so that msg.payload is an object. This could be done in a `change` node before getting to the `join` node.

Here is a simple example:

```auto
[{"id":"16d736db0bdee8b8","type":"join","z":"c40339e97a7d4c88","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":690,"y":200,"wires":[["39ef0430c89ad973"]]},{"id":"66f9975e11d1d49d","type":"inject","z":"c40339e97a7d4c88","name":"1","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"300","payloadType":"num","x":170,"y":120,"wires":[["a9c72fc019615889"]]},{"id":"d6db8c058923a2ae","type":"inject","z":"c40339e97a7d4c88","name":"2a","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"250","payloadType":"num","x":170,"y":180,"wires":[["341fd1f9b45f27e4"]]},{"id":"e0ee180a5ce0df67","type":"inject","z":"c40339e97a7d4c88","name":"2b","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"700","payloadType":"num","x":170,"y":220,"wires":[["341fd1f9b45f27e4"]]},{"id":"83de4e5df156326b","type":"inject","z":"c40339e97a7d4c88","name":"3","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"three","payload":"16","payloadType":"num","x":170,"y":320,"wires":[["43c69fe65f52a086"]]},{"id":"39ef0430c89ad973","type":"debug","z":"c40339e97a7d4c88","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":180,"wires":[]},{"id":"a9c72fc019615889","type":"change","z":"c40339e97a7d4c88","name":"","rules":[{"t":"set","p":"oldpayload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"move","p":"oldpayload","pt":"msg","to":"payload.payload","tot":"msg"},{"t":"set","p":"payload.time","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":120,"wires":[["16d736db0bdee8b8"]]},{"id":"341fd1f9b45f27e4","type":"change","z":"c40339e97a7d4c88","name":"","rules":[{"t":"set","p":"oldpayload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"move","p":"oldpayload","pt":"msg","to":"payload.payload","tot":"msg"},{"t":"set","p":"payload.time","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":200,"wires":[["16d736db0bdee8b8"]]},{"id":"43c69fe65f52a086","type":"change","z":"c40339e97a7d4c88","name":"","rules":[{"t":"set","p":"oldpayload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"move","p":"oldpayload","pt":"msg","to":"payload.payload","tot":"msg"},{"t":"set","p":"payload.time","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":320,"wires":[["16d736db0bdee8b8"]]}]

```

---

<div class="post-metadata">

### Author: ![Phant0m-code](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phant0m-code/32/81819_2.png) [@Phant0m-code](https://discourse.nodered.org/u/Phant0m-code)
#### Post date: [7 August 2023 13:43 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/8 "2023-08-07T13:43:04Z")

</div>

So my dear friends it worked ( for now heheh) so i tried to add topics and send the whole object to the next node and yeaaa now it works. its syncron to the other data and it is doing what i want it to do 😃 I wish you all a nice day and if somebody ever have the same problem/issue like me here my way:

First i added to every phase a topic and a source with the three functions (func1,2,3) (picture 1) then i joind it with key/value option and at the sumup i used this code:

```auto

// Initialize the total sum to 0
let totalSum = 0;

// Loop through the object properties
for (const prop in msg.payload) {
  if (msg.payload.hasOwnProperty(prop) && msg.payload[prop].hasOwnProperty('payload') && msg.payload[prop].payload.hasOwnProperty('value')) {
    // Add the value of each phase to the total sum
    totalSum += msg.payload[prop].payload.value;
  }
}

// Create a new message with the total sum
msg.payload = totalSum;

// Optionally, you can set a new topic for the message if desired
msg.topic = "Total Power Sum";

// Return the new message
return msg;

```

There the pictures: '

Picture1:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b9eb3e0012dd1b7a1fccc93426625d953db0d437.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f1b6bf151441966cbd54da51051410a2c04d0e0.png)

Well thats is from my side 🙂 cya

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [7 August 2023 14:41 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/9 "2023-08-07T14:41:46Z")

</div>

> [@Phant0m-code](#):
>
> ```auto
> // Create a new message with the total sum
> msg.payload = totalSum;
> 
> ```

Being pedantic, you are not creating a new message, you are just setting the payload on the original message. There is usually no need to create a new message.

---

<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: [21 August 2023 14:42 UTC](https://discourse.nodered.org/t/wrong-value-in-array/80374/10 "2023-08-21T14:42:12Z")

</div>

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