Node red if function for data selection

Hi,

This is my complete message from my gateway
2/20/2024, 10:29:13 AMnode: allkbeacon/publish/282C0221FD0B : msg.payload : Object

object
msg: "advData"
loc: object
gmac: "282C0221FD0B"
obj: array[4]
0: object
1: object
2: object
type: 1
dmac: "BC5729028155"
time: "2024-02-20 10:29:11"
rssi: -73
ver: 0
vbatt: 3018
temp: 29.8
humidty: 79.49
x0: 76
y0: -30
z0: 1041
3: object

As you can see in the array, there are 4 objects, and at times will be more detected. I need to only display the data for object with specific dmac only. Then display into the dashboard. currently the object is randomized so i get data from different sensor for the object number.
for example, in the above, the object 2 dmac is BC5729028155, but in the next data in this would change. so my dashboard will be a randomized data from sensors/object that the gateway picked up.

thank you. i am very2 new to this.

Hello @mitchysho,

Please use a debug node set to display msg.payload.
At the right hand side of the debug output is a "Copy value" button.
image

Then back in the forum, on a new line click the </> button and paste the debug output over the top of
image

Wrapping between the lines with three back ticks makes it much easier to import and understand your data.

But I am pretty sure you can achieve what you want with

  • A change node to move just the array to msg.payload
  • A split node to make each array element it's own discrete message and
  • A switch node to only pass the one where msg.payload.dmac is the value you are looking for.
1 Like

You could use a change node with an expression, something like:

**['BC5729028155' in dmac].temp

Which will return 29.8

This will filter based on the dmac provided.

1 Like

Just to point out that **['BC5729028155' in dmac].temp is not the most efficient way to fine the specific dmacs, as using wild card search will have to look every where first.

I think $$.payload.obj[$.dmac = "BC5729028155"] would be better/more efficient. You can still add .temp if you want specific values to.
Assuming I am reading the payload object correctly, as jbudd has pointed out there is a better way to copy and paste an object.

[edit] As to in a function as you asked in title, you would use the filter method Array.prototype.filter() - JavaScript | MDN

msg.payload = msg.payload.obj.filter((obj) => obj.dmac === "BC5729028155");
retrun msg;
1 Like

thank you. this seems to be working. all of you are wizard :exploding_head:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.