Split incoming meta-data from Lora in Node-Red

I have data coming in from The ThingsNetwork using the TTN connectors. All works just fine but now there are more LoRa gateways around I am seeing the meta-data showing multiple sources.

[{"gtw_id":"eui-60c5a8fffe71ab20","timestamp":1177433204,
"time":"2019-05-18T05:37:18.649933Z",
"channel":7,"rssi":-125,"snr":-16,"rf_chain":1,
"latitude":-36.84942,"longitude":174.83958},
{"gtw_id":"eui-60c5a8fffe74d3a5","timestamp":1158909916,
"time":",
"channel":7,"rssi":-35,"snr":9.8,"rf_chain":1,
"latitude":-36.85412,"longitude":174.83879,"altitude":98}]

What I would like to do is only pull meta-data from my gateway (eui-60c5a8fffe74d3a5). Sometimes for example SNR is accessed as value = msg.metadata.gateways[0].snr; and sometimes it is value = msg.metadata.gateways[1].snr.

As more gateways appear it could be [2], [3] etc. How can I identify which offset in the array to access using a function that can look throughout the array, identify my gateway and from then on only pass thru the offset to my functions.

This is how I am doing it now using switches and duplicated code but I know it could be a lot tidier.

[{"id":"daa31028.bd4bf","type":"function","z":"62f2b249.fbdf4c","name":"Strip RSSI","func":"var value = msg.metadata.gateways[0].rssi;\nmsg.payload = value;\nmsg.topic = \"RSSI\";\nreturn msg;","outputs":1,"noerr":0,"x":690,"y":740,"wires":[["9c0e3bce.d88338","94808400.c69198"]]},{"id":"3c712fcf.09dae","type":"function","z":"62f2b249.fbdf4c","name":"Strip SNR","func":"var value = msg.metadata.gateways[0].snr;\nmsg.payload = value;\nmsg.topic = \"snr\";\nreturn msg;","outputs":1,"noerr":0,"x":680,"y":660,"wires":[["eaa62bef.7c1708","ac95c00c.2a26a"]]},{"id":"97c7af2d.7ccd2","type":"switch","z":"62f2b249.fbdf4c","name":"switch1","property":"metadata.gateways[0].gtw_id","propertyType":"msg","rules":[{"t":"eq","v":"eui-60c5a8fffe74d3a5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":700,"wires":[["7fcea8f9.1007a8","3c712fcf.09dae","daa31028.bd4bf"]]},{"id":"a19032a9.b784c","type":"switch","z":"62f2b249.fbdf4c","name":"switch3","property":"metadata.gateways[1].gtw_id","propertyType":"msg","rules":[{"t":"eq","v":"eui-60c5a8fffe74d3a5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":820,"wires":[["d007c91e.c0bcb8","fb9be853.7e5528","4b2b94bd.711ddc"]]},{"id":"4b2b94bd.711ddc","type":"function","z":"62f2b249.fbdf4c","name":"Strip RSSI","func":"var value = msg.metadata.gateways[1].rssi;\nmsg.payload = value;\nmsg.topic = \"RSSI\";\nreturn msg;","outputs":1,"noerr":0,"x":690,"y":860,"wires":[["9c0e3bce.d88338"]]},{"id":"fb9be853.7e5528","type":"function","z":"62f2b249.fbdf4c","name":"Strip SNR","func":"var value = msg.metadata.gateways[1].snr;\nmsg.payload = value;\nmsg.topic = \"snr\";\nreturn msg;","outputs":1,"noerr":0,"x":680,"y":800,"wires":[["eaa62bef.7c1708"]]}]

Example function:

input = [
  {
    "gtw_id": "eui-60c5a8fffe71ab20",
    "timestamp": 1177433204,
    "time": "2019-05-18T05:37:18.649933Z",
    "channel": 7,
    "rssi": -125,
    "snr": -16,
    "rf_chain": 1,
    "latitude": -36.84942,
    "longitude": 174.83958
  },
  {
    "gtw_id": "eui-60c5a8fffe74d3a5",
    "timestamp": 1158909916,
    "time": "2019-05-18T05:37:18.649933Z",
    "channel": 7,
    "rssi": -35,
    "snr": 9.8,
    "rf_chain": 1,
    "latitude": -36.85412,
    "longitude": 174.83879,
    "altitude": 98
  }
]

out = input.find(o => o.gtw_id === 'eui-60c5a8fffe74d3a5');
return {payload:out.snr};

Thanks Bakman2, how do I encapsulate this in a node-red function?
I am assuming 'input' would be possibly be message.payload but know payload is just the payload itself, not the meta-data.

I tried

var input = msg.metadata;
out = input.find(o => o.gtw_id === 'eui-60c5a8fffe74d3a5');
return {payload:out.snr};

and got

TypeError: input.find is not a function

What is the output of msg.metadata?

Perhaps it should be msg.metadata.gateways

I got an error message with "msg.metadata" that this was undefined.
same with msg.metadata,gateways

So first you have to determine what the input data is. Input->debug.

Changed the function to

var gateways = msg.metadata.gateways;
msg.payload = gateways;
return msg;

and got

[{"gtw_id":"eui-60c5a8fffe74d3a5","timestamp":3283714748,"time":"","channel":2,"rssi":-47,"snr":8,"rf_chain":0,"latitude":-36.85412,"longitude":174.83879,"altitude":98}]

note that the second gateway seems to have gone for the moment but could return at any time

this worked!

var gateways = msg.metadata.gateways;
out = gateways.find(o => o.gtw_id === 'eui-60c5a8fffe74d3a5');
var value1 = out.snr;
msg.payload = value1;
return msg;

so added

var gateways = msg.metadata.gateways;
out = gateways.find(o => o.gtw_id === 'eui-60c5a8fffe74d3a5');
var value = out.rssi;
msg.payload = value;
msg.topic = "RSSI";
return msg;

and all done and working. Thanks Bakman2

1 Like