Splitting Value

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?

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:

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?

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

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

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.

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)

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

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

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

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

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

1 Like

Agreed, the question is whether the spec is correct.

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:

The history of s/w is strewn with debris from crashes caused by spec writers saying "this can't possibly happen". :slight_smile:
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.

2 Likes

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

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

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

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.

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.