Search an Array

Hi,

I have a text file that I am loading into an array which contains lots of license plates.

I have a feed coming from a camera with msg.payload of licene plate data; I want to be able to search every message coming out of this to see if it exists in the license plate array and if it does then continue...

Any ideas?

Thanks

D

Welcome to the forums @Elad23

For anyone to be able to provide solutions, can you provide an example payload that you receive (of course with dummy plates :grimacing:)

Also can we assume your array is flat?

["xxxxxxxx", "xxxxxxxx", "xxxxxxxx"]

Of course.. here is what I load from the CSV into a set of objects:

image

Ok its still a little unclear on your setup, but I'll give it ago.

  1. Store your object array in a global variable (KnownPlates)
  2. When the camera detects a plate - check it

I'm going to pretend I know where the plate data is in the camera provided payload.

msg.payload.detectedLicensePlate :man_shrugging:

const plates = global.get("KnownPlates")
const found = plates.find((p) => p.plate === msg.payload.detectedLicensePlate)
if(found !== undefined){
    // continue (use found.plate/name/apartment etc etc)
}

Use that in a function node

I'm sure this can be achieved with a switch node and JSONata also.

EDIT
I updated the sample, so you can use The found plate

Thanks for the input Marcus...

I am using the CSV node with output single message array - how do I make this into a global that can then be read by the function that is reading the license plate data?

(As you have probably gathered I am a little new to node.red...)

Forget that - I read the forum and worked that out :slight_smile:

You can do this by triggering the inject Node after 1s of Node RED starting.
and use the change node to set the global.

Then my code example should work when attached to the camera output

I did it this way (which I think also works):
global.set("KnownPlates",msg.payload);

1 Like

OK so that seems all ok... now how do I return a different value in that object?

e.g. I have ammended the CSV so now it also has "parking" in it, which denotes which garage door they have access to... this is either a 1, 2 or 3 value and I want to return that numerical value so I can put into a switch to control the three relays :slight_smile:

Hi @Elad23

With my original code, found becomes the object, with that in mind....

you prob should rename found to driver..... DONE

const plates = global.get("KnownPlates")
const driver = plates.find((p) => p.plate === msg.payload.detectedLicensePlate)
if(driver !== undefined){
    msg.payload = {
       relayId: driver.parking,
       driverName: driver.name
    }
    return msg
}

This will then output a payload message of

{
  relayId: 5,
  driverName: 'Bob Geldof'
}

Then in the switch I put after that how do I get the value of the specific relayID in the switch - or do I need to seperate the array first?

Hi @Elad23

The code is already extracting 1 element out of the array (based on the plate number match)
and setting a new payload with properties from the matched object

if(driver !== undefined){
    /* HERE */
    msg.payload = {
       relayId: driver.parking,
       driverName: driver.name
    }
    return msg
}

So in a switch node further down the track, you can do...

You can set the payload to the entire matched object, given you all the properties, but you need to use payload.parking in the switch Node - or rename the CSV header for it :wink:

if(driver !== undefined){
    msg.payload = driver
    return msg
}

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