# Splitting Value

**URL:** <https://discourse.nodered.org/t/splitting-value/10189>\
**Category:** Dashboard\
**Created:** [15 April 2019 21:15 UTC](https://discourse.nodered.org/t/splitting-value/10189 "2019-04-15T21:15:57Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [15 April 2019 21:15 UTC](https://discourse.nodered.org/t/splitting-value/10189/1 "2019-04-15T21:15:57Z")

</div>

Firstly I am totally new to all of this so please be gentle with me, however, I am really struggling with splitting data to be able to display in to two Gauges. It is presented from a single API value and the range is from -3300 to +3300. Ideally what I would like is one Gauge displaying data from 0 to -3300 and one displaying +1 to +3300.

So far I have written a function with two outputs as follows:

var x = msg.payload.Pac\_total\_W;

if (x \>=1){  
var msg\_0 = {payload : msg.payload.Pac\_total\_W}  
return [msg\_0]}  
else if (x \<=0){  
var msg\_1 = {payload : msg.payload.Pac\_total\_W}  
return [msg\_1]}

And used msg\_0 and msg\_1 as the outputs?

---

<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:** [15 April 2019 21:23 UTC](https://discourse.nodered.org/t/splitting-value/10189/2 "2019-04-15T21:23:18Z")

</div>

First off always surround your code with three back tics or you can highlight it and press the `</>` icon in the menu and it will iook like this:

```auto
if (x >=1) {
    var msg_0 = {payload : msg.payload.Pac_total_W}
    return [msg_0]}
else if (x <=0) {
    var msg_1 = {payload : msg.payload.Pac_total_W}
return [msg_1]}

```

What do you see if you attach a debug node on the outout of the function node?

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [16 April 2019 06:40 UTC](https://discourse.nodered.org/t/splitting-value/10189/3 "2019-04-16T06:40:41Z")

</div>

When I run the debug it shows output on the first out but not the second one. I have attached a screenshot of the way I have the nodes configured.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/29c032eaca5e92d9af26fabca3994991d3cc8487.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 April 2019 06:57 UTC](https://discourse.nodered.org/t/splitting-value/10189/4 "2019-04-16T06:57:16Z")

</div>

The else isn't sending to the second output. Check the docs how to use multiple outputs  
[https://nodered.org/docs/writing-functions#multiple-outputs](https://nodered.org/docs/writing-functions#multiple-outputs)

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [16 April 2019 11:12 UTC](https://discourse.nodered.org/t/splitting-value/10189/5 "2019-04-16T11:12:39Z")

</div>

Thanks for the document, that is what I used to write what I currently have, so I’m still not a 100% why the else statement isn’t working.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 April 2019 11:16 UTC](https://discourse.nodered.org/t/splitting-value/10189/6 "2019-04-16T11:16:56Z")

</div>

The very first example there shows you an array with two things in it... (one for each output)... how many things do you have in the arrays that you are returning? (hint: not 2)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 April 2019 11:42 UTC](https://discourse.nodered.org/t/splitting-value/10189/7 "2019-04-16T11:42:56Z")

</div>

```auto
var x = msg.payload.Pac_total_W;

if (x >=1){
  msg.payload = msg.payload.Pac_total_W;
  return [msg,null]//only trigger 1st output
}
else if (x <=0){
  msg.payload = msg.payload.Pac_total_W;
  return [null, msg]//only trigger 2nd output
}
return null; //dont trigger any output

```

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [16 April 2019 19:10 UTC](https://discourse.nodered.org/t/splitting-value/10189/8 "2019-04-16T19:10:20Z")

</div>

Awesome!!!

So I now get it, I have made a slight change to the code after re-reading the Multiple Outputs doc...

I can't thank you all enough, my changed code below.

var x = msg.payload.Pac\_total\_W;  
var y = {payload:"0"}

if (x \>=1){  
msg.payload = msg.payload.Pac\_total\_W;  
return [msg,y]//only trigger 1st output  
}  
else if (x \<=0){  
msg.payload = msg.payload.Pac\_total\_W;  
return [y, msg]//only trigger 2nd output  
}  
return null; //dont trigger any output

---

<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:** [16 April 2019 20:54 UTC](https://discourse.nodered.org/t/splitting-value/10189/9 "2019-04-16T20:54:15Z")

</div>

Did you mean it to do nothing if x is, for example, 0.5? Perhaps you think that can never happen, but it is better not to allow such holes in your s/w even if you think it will never happen. This would probably be a better way to code it

```auto
if ( x > 0 ) {
 ...
} else {
  // must be less than or equal to 0
  ...
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 April 2019 21:47 UTC](https://discourse.nodered.org/t/splitting-value/10189/10 "2019-04-16T21:47:48Z")

</div>

I spotted that too however the spec said

> what I would like is one Gauge displaying data from 0 to -3300 and one displaying +1 to +3300

---

<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:** [17 April 2019 07:47 UTC](https://discourse.nodered.org/t/splitting-value/10189/11 "2019-04-17T07:47:48Z")

</div>

Agreed, the question is whether the spec is correct.

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [17 April 2019 08:26 UTC](https://discourse.nodered.org/t/splitting-value/10189/12 "2019-04-17T08:26:45Z")

</div>

Well, it's an interesting question. However the API I am reading from doesn't display say 0.1 or 0.2 hence the 1 figures as that would be the 1st plus figure I am pulling. But technically i am guessing that greater than \>0 would be correct as if a change was made to the API that allowed this it would be presented correctly in the application.

Its working great though screenshot of the output below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e36e56372702a00d19d0132d1ddf05b7e12717b3.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:** [17 April 2019 08:51 UTC](https://discourse.nodered.org/t/splitting-value/10189/13 "2019-04-17T08:51:33Z")

</div>

The history of s/w is strewn with debris from crashes caused by spec writers saying "this can't possibly happen". 🙂  
In addition the suggested code is slightly more efficient as only one test is required, and there does not appear to be a down side.

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [17 April 2019 12:20 UTC](https://discourse.nodered.org/t/splitting-value/10189/14 "2019-04-17T12:20:13Z")

</div>

OK so this is weird, on the second output the gauge doesnt move so that Batton always looks like it is at 0 for Charge and Import?

See below?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7d3a3d58ba9fab71b07885b9126c1e97b8071cc8.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:** [17 April 2019 12:37 UTC](https://discourse.nodered.org/t/splitting-value/10189/15 "2019-04-17T12:37:49Z")

</div>

Is that the one you are sending a -ve value to?

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [17 April 2019 12:50 UTC](https://discourse.nodered.org/t/splitting-value/10189/16 "2019-04-17T12:50:27Z")

</div>

It is yes? Is that an issue, I have converted it in the text box by doing the following {{value\*-1}}W

---

<div class="post-metadata">

**Author:** ![llamedos](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@llamedos](https://discourse.nodered.org/u/llamedos)\
**Post date:** [17 April 2019 12:54 UTC](https://discourse.nodered.org/t/splitting-value/10189/17 "2019-04-17T12:54:24Z")

</div>

Weird so if I do a conversion in the function it works fine, so I take it the system has a bug with minus figures not that it matters to me as I convert it to a plus figure.

---

<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:** [17 April 2019 13:31 UTC](https://discourse.nodered.org/t/splitting-value/10189/18 "2019-04-17T13:31:22Z")

</div>

Not a bug, a misunderstanding. The gauge itself (not the text display) shows the value in the payload. You have scaled the gauge 0 to 3300 and then given it a -ve value, so it is off the bottom. By changing it in the function you are now giving the gauge a value in range.  
It is always best to initially assume that some malfunction is of ones own making. Occasionally it will be a bug, but my experience is that usually it is myself that is missing something.
