Splitting Value

#1

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?

0 Likes

#2

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?

0 Likes

#3

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

0 Likes

#4

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

0 Likes

#5

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.

0 Likes

#6

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)

0 Likes

#7
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

0 Likes

#8

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

0 Likes

#9

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
  ...
}
0 Likes

#10

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

#11

Agreed, the question is whether the spec is correct.

0 Likes

#12

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:

0 Likes

#13

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

#14

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

0 Likes

#15

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

0 Likes

#16

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

0 Likes

#17

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.

0 Likes

#18

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.

0 Likes