# How to search for string and cross-check

**URL:** <https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118>\
**Category:** General\
**Created:** [3 May 2021 14:44 UTC](https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118 "2021-05-03T14:44:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [3 May 2021 14:44 UTC](https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118/1 "2021-05-03T14:44:43Z")

</div>

Hello everyone,  
I am very new to Node Red and with help from the HA forum ( [How to automatically retrieve device name from entity\_id? - #7 by AleXSR7001 - Node-RED - Home Assistant Community](https://community.home-assistant.io/t/how-to-automatically-retrieve-device-name-from-entity-id/304846/7) ), I am trying to solve a problem.

So, I have a global defined as homeassistant.config. This is built as follows

```auto
inputdata: object
  areas: array[7]
  devices: array [25]
  entities: array[144]
    [0 … 9]
      0: object
         config_entry_id: "d25740dd25bb73"
         device_id: "f6af38b97d9"
         area_id: null
         disabled_by: "integration"
         entity_id: "sensor.ambient_light"
      1: object
         config_entry_id: "d2dd25bb73"
         device_id: "f6af7d9"
         area_id: null
         disabled_by: "integration"
         entity_id: "sensor.main_light"

```

and so on.  
Now I am trying to find a string inside the "entity\_id". So let's say the string is "ambient". I want it to find "ambient" in entity\_id, then from that object get the device\_id, then search through the array of devices and return the friendly\_name of the matching device.

Probably sounds simple to all of you, but is proving to be complicated to me.  
I am unable to find the string "ambient", so I am failing with step one already 🙈

So, here is where I am at right now.

```auto
[{"id":"aa642c6f.01afa","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"ce4e8cef.b3c4a","type":"inject","z":"aa642c6f.01afa","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ambient","payloadType":"str","x":90,"y":360,"wires":[["a328617b.422e"]]},{"id":"744fa604.4422c8","type":"function","z":"aa642c6f.01afa","name":"exact search (exists true/false)","func":"var findWhat = msg.payload;\nvar data = msg.inputdata;\nmsg.payload = data.entities.filter(e => {\n try {\n var conditions = e.conditions[0].condition\n if(conditions.includes(findWhat)) {\n return true;\n }\n } catch {\n \n }\n return false\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":360,"wires":[["d11e148d.d87bb8","30041c9c.853d44"]]},{"id":"a328617b.422e","type":"change","z":"aa642c6f.01afa","name":"","rules":[{"t":"set","p":"inputdata","pt":"msg","to":"homeassistant.config","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":360,"wires":[["744fa604.4422c8","607e9988.3398c8"]]},{"id":"d11e148d.d87bb8","type":"debug","z":"aa642c6f.01afa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1050,"y":360,"wires":[]},{"id":"607e9988.3398c8","type":"function","z":"aa642c6f.01afa","name":"partial search (exists true/false) (case insensitive)","func":"var findWhat = msg.payload.toLowerCase();\nvar data = msg.inputdata;\nmsg.payload = data.entities.filter(e => {\n try {\n var conditions = e.conditions[0].condition;\n var found = conditions.some(e => {\n return (e && e.length) ? e.toLowerCase().includes(findWhat) : false\n })\n return found;\n } catch {\n }\n return null\n});\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":420,"wires":[["c8cd13ba.4b378","7c039174.c795d"]]},{"id":"c8cd13ba.4b378","type":"debug","z":"aa642c6f.01afa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1090,"y":420,"wires":[]},{"id":"30041c9c.853d44","type":"switch","z":"aa642c6f.01afa","name":"Found?","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":810,"y":320,"wires":[["19b7b63f.b0b46a"]]},{"id":"19b7b63f.b0b46a","type":"debug","z":"aa642c6f.01afa","name":"first found item","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":320,"wires":[]},{"id":"7c039174.c795d","type":"switch","z":"aa642c6f.01afa","name":"Found?","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":800,"y":480,"wires":[["f6d949dd.411978"]]},{"id":"f6d949dd.411978","type":"debug","z":"aa642c6f.01afa","name":"first found item","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":480,"wires":[]}]

```

but the message is always undefined, so I am guessing that my way of searching is not appropriate for the input data.

Could someone here maybe help me find a solution? 🙂

Could the problem be the ‘catch’ command. I just saw that the editor states that the optional catch binding is only available in ES10.

Thank you all for your help.  
Alex

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 May 2021 15:18 UTC](https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118/2 "2021-05-03T15:18:12Z")

</div>

It is much easier for us to help you if you post the code of your function node directly - saves us always having to load lots of flows.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [3 May 2021 15:20 UTC](https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118/3 "2021-05-03T15:20:12Z")

</div>

Sorry, of course 🙂  
Exact match

```auto
var findWhat = msg.payload;
var data = msg.inputdata;
msg.payload = data.entities.filter(e => {
    try {
        var conditions = e.conditions[0].condition
        if(conditions.includes(findWhat)) {
            return true;
        }
    } catch {
        
    }
    return false
});
return msg;

```

Partial match:

```auto
var findWhat = msg.payload.toLowerCase();
var data = msg.inputdata;
msg.payload = data.entities.filter(e => {
    try {
        var conditions = e.conditions[0].condition;
        var found = conditions.some(e => {
            return (e && e.length) ? e.toLowerCase().includes(findWhat) : false
        })
        return found;
    } catch {
    }
    return null
});
return msg;

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 May 2021 15:33 UTC](https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118/4 "2021-05-03T15:33:26Z")

</div>

> [@AleXSR700](#):
>
> `conditions[0].condition`

I can't see a conditions array inside the entities object?

```auto
e.entity_id.indexOf(findWhat)

```

Will return a number of zero or above if it finds the string.

```js
if ( e.entity_id && (e.entity_id.indexOf(findWhat) > -1) ) return true
else return false

```

Is probably all you need. You don't need the try, I've included a test to make sure that e.entity\_id exists.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 July 2021 15:33 UTC](https://discourse.nodered.org/t/how-to-search-for-string-and-cross-check/45118/5 "2021-07-02T15:33:37Z")

</div>

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