If I have 1 input but multi output...How to do?

Dear All Sir :

Look at Pic and below :

  1. Just 1 input(RX).
  2. The function node code like:
for(i=0;i<6;i++)
{
    switch(rx_data[1])
    {
        case 0 : //show o3 value
            msg.topic="Read_All_is_OK";
            msg.payload="Read All Sensors Value is ok.";
        break;
        case 1 : //show co value
            msg.topic="Read_All_is_OK";
            msg.payload="Read All Sensors Value is ok.";
        break;
        case 2 : //show co2 value
            msg.topic="Read_All_is_OK";
            msg.payload="Read All Sensors Value is ok.";
        break;
        case 3 : //show tvoc value
            msg.topic="Read_All_is_OK";
            msg.payload="Read All Sensors Value is ok.";
        break;
        case 4 : //show ch2o value
            msg.topic="Read_All_is_OK";
            msg.payload="Read All Sensors Value is ok.";
        break;
        case 5 : //show no2 value
             msg.topic="Read_All_is_OK";
             msg.payload="Read All Sensors Value is ok.";
        break;
    }
    return msg;
}
  1. I have 6 muliti output

My question is at 2. :
How to send data to ouput at time in for loop,
Cause only one msg.payload and if I have multi msg but return msg.payload just one and return msg.payload will done function then exit ...
So how to do the for loop and return msg.payload each at time?

Thank u.

You want to send a mesage for each value in the for loop?
Use node.send()

[{"id":"a0a26e694a19fa92","type":"inject","z":"d18b45291a2aaa67","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello","payloadType":"str","x":470,"y":140,"wires":[["041d4b181b44b721"]]},{"id":"041d4b181b44b721","type":"function","z":"d18b45291a2aaa67","name":"","func":"var foo = msg.payload;\nfor(i=0;i<6;i++)\n{\n    msg.payload = foo + i;\n    node.send(msg);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["82193971a0d55db7"]]},{"id":"82193971a0d55db7","type":"debug","z":"d18b45291a2aaa67","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":140,"wires":[]}]

Dear jbudd :

But my msg.payload is different, In ur code just one mag.payload!

Thank u.

Dear jbudd :

And I need 6 topic output for switch node, So how to do...?

Thank u.

It seems to be that my example has six different msg.payloads "hello0", "hello1"... "hello5" while yours only has one "Read All Sensors Value is ok."

I clearly don't understand your questions. Sorry/

Dear jbudd :

Its my fault , post code again :

for(i=0;i<6;i++)
{
    switch(rx_data[1])
    {
        case 0 : //show o3 value
            msg.topic="O3_Send_OK";
            msg.payload="Read O3 Sensor Value is ok.";
        break;
        case 1 : //show co value
            msg.topic="CO_Send_OK";
            msg.payload="Read CO Sensor Value is ok.";
        break;
        case 2 : //show co2 value
            msg.topic="CO2_Send_OK";
            msg.payload="Read CO2 Sensor Value is ok.";
        break;
        case 3 : //show tvoc value
            msg.topic="TVOC_Send_OK";
            msg.payload="Read TVOC Sensor Value is ok.";
        break;
        case 4 : //show ch2o value
            msg.topic="CH2O_is_OK";
            msg.payload="Read CH2O Sensor Value is ok.";
        break;
        case 5 : //show no2 value
            msg.topic="NO2_Send_OK";
            msg.payload="Read NO2 Sensor Value is ok.";
       break;
    }
    return msg;
}

Do u have any ideal?

Thanku.

Have a look at Writing Functions : Node-RED under Multiple Outputs.

Dear Bobo :

  1. It seems can be ...
  2. Another question :
    How can I combine array data to beome int,uint,long,float?
    int=array[0]<<8|array[1]; //It can be?
    float=...? //How?

Thank u .

You should find all you need here: JavaScript Number Methods

Dear Bobo :

My code is right? Cause Im not familiar with JS...
Some is integer,
Some is long,
Some is float.

var msg_read={ payload:"Read_All_is_OK" ,topic:"Read_All_is_OK"};
var o3_value=rx_data[16]<<24+rx_data[17]<<16+rx_data[18]<<8+rx_data[19];
var msg_o3={ payload:o3_value ,topic:"O3_Send_Value"};
var co_value=rx_data[20]<<24+rx_data[21]<<16+rx_data[22]<<8+rx_data[23];
var msg_co={ payload:co_value ,topic:"CO_Send_Value"};
var co2_value=rx_data[24]<<8+rx_data[25];
var msg_co2={ payload:co2_value ,topic:"CO2_Send_Value"};
var tvoc_value=rx_data[26]<<8+rx_data[27];
var msgtvoc={ payload:tvoc_value ,topic:"TVOC_Send_Value"};
var ch2o_value=rx_data[28]<<24+rx_data[29]<<16+rx_data[30]<<8+rx_data[31];
var msg_ch2o={ payload:ch2o_value ,topic:"CH2O_Send_Value"};
var no2_value=rx_data[32]<<8+rx_data[33];
var msg_no2={ payload:no2_value ,topic:"NO2_Send_Value"};
//
return [ msg_read, msg_o3, msg_co , msg_co2 , msg_tvoc , msg_ch2o , msg_no2];

Thank u.

As has been suggested in other posts, node-red-contrib-buffer-parser (node) - Node-RED can do all of this for you.

image

image

[{"id":"2a94e767.b2e158","type":"inject","z":"5e6c8b.7f38b374","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34]","payloadType":"json","x":1314,"y":1136,"wires":[["27c16664.aa0e3a"]]},{"id":"27c16664.aa0e3a","type":"buffer-parser","z":"5e6c8b.7f38b374","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int32be","name":"O3_Send_Value","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32be","name":"CO_Send_Value","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"CO2_Send_Value","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"TVOC_Send_Value","offset":26,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32be","name":"CH2O_Send_Value","offset":28,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"NO2_Send_Value","offset":32,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":6,"x":1510,"y":1136,"wires":[["94e3c954.8ec968"],["1966c447.40f55c"],["404bc20c.70f2bc"],["41363156.abe7e"],["d62519ae.dbaf98"],["e8ebb500.08c3b8"]]},{"id":"94e3c954.8ec968","type":"debug","z":"5e6c8b.7f38b374","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1736,"y":1024,"wires":[]},{"id":"1966c447.40f55c","type":"debug","z":"5e6c8b.7f38b374","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1734,"y":1072,"wires":[]},{"id":"404bc20c.70f2bc","type":"debug","z":"5e6c8b.7f38b374","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1734,"y":1120,"wires":[]},{"id":"41363156.abe7e","type":"debug","z":"5e6c8b.7f38b374","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1734,"y":1168,"wires":[]},{"id":"d62519ae.dbaf98","type":"debug","z":"5e6c8b.7f38b374","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1734,"y":1216,"wires":[]},{"id":"e8ebb500.08c3b8","type":"debug","z":"5e6c8b.7f38b374","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1734,"y":1264,"wires":[]}]

Alternatively, you can output all values as one object...
image

Dear Steve-Mcl :

I will test it.

Thank u.

Dear Steve-Mcl :

It seems can not!

1.Cause The buffer parser output1 That I want connect to gauge and text...
So I think it can not to do that? Right?
2. Look at Pic :
How did this value come from? Sorry, Im not smart!

Thank u.

Why not? Just connect it to a chart. It will work.

It comes from converting the bytes into int16 and int32 as per the settings entered and data inputted.

What don't you understand?

Dear Stevev-Mcl :

  1. It cant cause I another one output use payload(to ui_text), But buffer parser isnt, Right?
  2. Like O3 data is from array[16]~array[19],I didnt see the O3 value, So maybe I think its json data type, Right?

Thank u.

Dear Stevev-Mcl :

Look at pic and code at below :

var msg_read={ payload:"Read_All_is_OK" ,topic:"Read_All_is_OK"};
//
var o3_value=[msg.payload.data[16],msg.payload.data[17],msg.payload.data[18],msg.payload.data[19]];
var o3_value_buf=new Buffer(o3_value);
o3_value=o3_value_buf.readFloatBE(0);
var msg_o3={ payload:o3_value ,topic:"O3_Send_Value"};
//
var co_value=[msg.payload.data[20],msg.payload.data[21],msg.payload.data[22],msg.payload.data[23]];
var co_value_buf=new Buffer(co_value);
co_value=co_value_buf.readFloatBE(0);
var msg_co={ payload:co_value ,topic:"CO_Send_Value"};
//
var co2_value=[msg.payload.data[24],msg.payload.data[25]];
var co2_value_buf=new Buffer(co2_value);
co2_value=co2_value_buf.readUInt16BE(0);
var msg_co2={ payload:co2_value ,topic:"CO2_Send_Value"};
//
var tvoc_value=[msg.payload.data[26],msg.payload.data[27]];
var tvoc_value_buf=new Buffer(tvoc_value);
tvoc_value=tvoc_value_buf.readUInt16BE(0);
var msg_tvoc={ payload:tvoc_value ,topic:"TVOC_Send_Value"};
//
var ch2o_value=[msg.payload.data[28],msg.payload.data[29],msg.payload.data[30],msg.payload.data[31]];
var ch2o_value_buf=new Buffer(ch2o_value);
ch2o_value=ch2o_value_buf.readFloatBE(0);
var msg_ch2o={ payload:ch2o_value ,topic:"CH2O_Send_Value"};
 //
var no2_value=[msg.payload.data[32],msg.payload.data[33]];
var no2_value_buf=new Buffer(no2_value);
no2_value=co2_value_buf.readUInt16BE(0);
var msg_no2={ payload:no2_value ,topic:"NO2_Send_Value"};
//
return [ msg_read, msg_o3, msg_co , msg_co2 , msg_tvoc , msg_ch2o , msg_no2];

RX has receive data,But Tte Gauge didnt show value! Where I was wrong?

Thank u.


And why has the error?

Read the Gauge built in help - that is NOT how you use Value Format

Because there is no data property in msg.payload


Here is a working demo...

EcbfW1U61G

[{"id":"2a94e767.b2e158","type":"inject","z":"f9687f620c582b41","name":"Demo Data 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,246,102,102,65,68,204,205,0,50,0,150,66,254,51,51,0,0]","payloadType":"bin","x":1630,"y":1500,"wires":[["27c16664.aa0e3a","a3052117.1edea"]]},{"id":"27c16664.aa0e3a","type":"buffer-parser","z":"f9687f620c582b41","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatbe","name":"O3_Send_Value","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatbe","name":"CO_Send_Value","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"CO2_Send_Value","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"TVOC_Send_Value","offset":26,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatbe","name":"CH2O_Send_Value","offset":28,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"NO2_Send_Value","offset":32,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":6,"x":1826,"y":1612,"wires":[["94e3c954.8ec968","467c82ff.3f4a5c"],["853dc0bd.61069","3c949324.15898c"],["7e904a21.609dd4","56f58ef4.c662"],[],[],[]]},{"id":"94e3c954.8ec968","type":"debug","z":"f9687f620c582b41","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2090,"y":1460,"wires":[]},{"id":"467c82ff.3f4a5c","type":"ui_gauge","z":"f9687f620c582b41","name":"","group":"81dd3718.b97888","order":0,"width":"5","height":"4","gtype":"gage","title":"O3 Value","label":"units","format":"{{value | number:2}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":2080,"y":1508,"wires":[]},{"id":"853dc0bd.61069","type":"ui_gauge","z":"f9687f620c582b41","name":"","group":"81dd3718.b97888","order":0,"width":"5","height":"4","gtype":"gage","title":"CO Value","label":"units","format":"{{value | number:2}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":2080,"y":1604,"wires":[]},{"id":"3c949324.15898c","type":"debug","z":"f9687f620c582b41","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2090,"y":1556,"wires":[]},{"id":"7e904a21.609dd4","type":"debug","z":"f9687f620c582b41","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2090,"y":1652,"wires":[]},{"id":"56f58ef4.c662","type":"ui_gauge","z":"f9687f620c582b41","name":"","group":"81dd3718.b97888","order":0,"width":"5","height":"4","gtype":"gage","title":"CO Value","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":2080,"y":1700,"wires":[]},{"id":"a3052117.1edea","type":"debug","z":"f9687f620c582b41","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1826,"y":1500,"wires":[]},{"id":"d248a0f033e182b9","type":"inject","z":"f9687f620c582b41","name":"Demo Data 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,66,246,102,102,64,68,204,205,0,45,0,150,66,254,51,51,0,0]","payloadType":"bin","x":1630,"y":1560,"wires":[["27c16664.aa0e3a"]]},{"id":"b7cdb12b79ef6cfd","type":"inject","z":"f9687f620c582b41","name":"Demo Data 3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,65,253,253,212,66,1,204,205,0,56,0,150,66,254,51,51,0,0]","payloadType":"bin","x":1630,"y":1620,"wires":[["27c16664.aa0e3a"]]},{"id":"81dd3718.b97888","type":"ui_group","name":"Monitor","tab":"4e11db25.239e94","order":1,"disp":true,"width":"13","collapse":true,"className":""},{"id":"4e11db25.239e94","type":"ui_tab","name":"Gauges","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Dear McI :

If I want add a parser output O3_Send_Value to text ant show "O3_Send_is_OK."
But...How?

Thank u.

What makes O3_Send OK?

What makes O3_Send BAD?