How to search for string and cross-check

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 ), I am trying to solve a problem.

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

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 :see_no_evil:

So, here is where I am at right now.

[{"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? :slight_smile:

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

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.

Sorry, of course :slight_smile:
Exact match

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:

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;

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

e.entity_id.indexOf(findWhat)

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

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.

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