<solved> Find an object in an array of objects

My goal is to detect when my phone or my wife's phone is at home or not. One way I thought of was to use the ARP node to get all of the IP and MAC addresses in my router's ARP table, then look for our phones.

If there's a better/easier way to determine home or away, please advise me.

I think that I would use the JSONATA node to find a matching IP or MAC, but the data doesn't look right for a JSONATA search. (I have never used JSONATA before). So, how would I determine, for example, if "192.168.1.31" is present?

Here's what my ARP node delivers in msg.payload:

[
   {
      "ip":"192.168.1.67",
      "mac":"cc:50:e3:0c:6e:00",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.31",
      "mac":"c4:1c:ff:13:9e:61",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.4",
      "mac":"dc:a6:32:09:22:a1",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.42",
      "mac":"bc:dd:c2:25:9a:0f",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.19",
      "mac":"14:91:38:8f:71:68",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.57",
      "mac":"98:5f:d3:e6:aa:9b",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.23",
      "mac":"1c:69:7a:0a:62:c6",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.61",
      "mac":"a4:ba:db:b8:96:45",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.34",
      "mac":"68:54:fd:33:c2:79",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.101",
      "mac":"8c:7f:3b:76:18:c8",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.11",
      "mac":"00:bb:3a:e4:77:0c",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.49",
      "mac":"a0:20:a6:30:de:20",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.204",
      "mac":"60:01:94:86:74:b8",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.49",
      "mac":"a0:20:a6:30:de:20",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.204",
      "mac":"60:01:94:86:74:b8",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.15",
      "mac":"2c:3a:e8:11:8e:2e",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.53",
      "mac":"78:0f:77:5a:e3:f0",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.26",
      "mac":"0c:ee:99:07:df:08",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.41",
      "mac":"ec:fa:bc:06:6d:93",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.30",
      "mac":"dc:a6:32:09:22:a1",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.7",
      "mac":"d0:e7:82:f0:c4:57",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.70",
      "mac":"50:02:91:68:ea:7f",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.18",
      "mac":"ec:fa:bc:06:80:d2",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.56",
      "mac":"dc:ef:ca:bc:96:c3",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.33",
      "mac":"6c:ad:f8:61:b6:ad",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.22",
      "mac":"84:0d:8e:ad:ad:c1",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.37",
      "mac":"b0:35:b5:b6:fe:bd",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.203",
      "mac":"84:0d:8e:44:2f:a2",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.100",
      "mac":"3c:df:a9:6c:bf:de",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.10",
      "mac":"b4:e6:2d:67:b7:d0",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.48",
      "mac":"5c:cf:7f:fa:b7:45",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.230",
      "mac":"00:01:fb:00:1f:41",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.25",
      "mac":"44:65:0d:5f:a5:9d",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.14",
      "mac":"8c:f5:a3:68:55:0f",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.52",
      "mac":"2c:3a:e8:01:1f:0f",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.65",
      "mac":"1c:69:7a:0f:87:76",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.2",
      "mac":"00:d0:2d:43:8d:62",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.17",
      "mac":"60:38:e0:5c:3f:85",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.6",
      "mac":"60:01:94:69:2d:b5",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.21",
      "mac":"9c:8e:cd:0e:98:51",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.59",
      "mac":"50:02:91:69:78:9c",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.32",
      "mac":"50:e5:49:56:4c:87",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.9",
      "mac":"60:01:94:66:ad:29",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.63",
      "mac":"3c:5c:c4:31:59:d5",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.72",
      "mac":"a4:ba:db:b8:96:45",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.36",
      "mac":"cc:50:e3:5b:66:8a",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.202",
      "mac":"60:01:94:ab:45:90",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.13",
      "mac":"30:95:e3:24:03:fd",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.143",
      "mac":"a0:20:a6:0f:38:fa",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.24",
      "mac":"80:7d:3a:7f:e8:f3",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.1",
      "mac":"18:1b:eb:7b:0d:df",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.55",
      "mac":"00:18:dd:06:9a:fd",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.64",
      "mac":"1c:69:7a:0f:87:76",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.131",
      "mac":"b8:27:eb:66:72:3b",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.28",
      "mac":"6c:ad:f8:30:a9:f7",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.5",
      "mac":"60:01:94:51:d1:25",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.43",
      "mac":"a4:ee:57:5b:e2:87",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.16",
      "mac":"60:01:94:69:36:df",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.47",
      "mac":"24:4c:e3:ec:a1:c2",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.20",
      "mac":"5c:cf:7f:b4:61:a3",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.58",
      "mac":"3c:5c:c4:0f:40:8e",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.35",
      "mac":"d8:f1:5b:c6:4f:48",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.201",
      "mac":"60:01:94:ab:53:2b",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.8",
      "mac":"7c:a7:b0:9b:a5:d8",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.62",
      "mac":"10:ae:60:a7:2a:48",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.39",
      "mac":"00:d0:2d:b2:52:c5",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.50",
      "mac":"4c:11:ae:0d:81:ef",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.27",
      "mac":"84:0d:8e:ad:a7:5b",
      "iface":"eth0"
   },
   {
      "ip":"192.168.1.54",
      "mac":"1c:69:7a:0a:62:c6",
      "iface":"eth0"
   }
]

Try this! :slight_smile:

$[`ip` ~> /192.168.1.31/i ]

I tested in https://try.jsonata.org/3oWik6Q_k.

Another way:

'192.168.1.31' in payload.ip

Example flow:

[{"id":"f6a9b90c.76b6e8","type":"switch","z":"1d2ad601.7d596a","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"'192.168.1.31' in payload.ip","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":60,"wires":[["34f6bd9e.cef3a2"]]},{"id":"aff7e53b.985338","type":"inject","z":"1d2ad601.7d596a","name":"","topic":"","payload":"[{\"ip\":\"192.168.1.67\",\"mac\":\"cc:50:e3:0c:6e:00\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.31\",\"mac\":\"c4:1c:ff:13:9e:61\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.4\",\"mac\":\"dc:a6:32:09:22:a1\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.42\",\"mac\":\"bc:dd:c2:25:9a:0f\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.19\",\"mac\":\"14:91:38:8f:71:68\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.57\",\"mac\":\"98:5f:d3:e6:aa:9b\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.23\",\"mac\":\"1c:69:7a:0a:62:c6\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.61\",\"mac\":\"a4:ba:db:b8:96:45\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.34\",\"mac\":\"68:54:fd:33:c2:79\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.101\",\"mac\":\"8c:7f:3b:76:18:c8\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.11\",\"mac\":\"00:bb:3a:e4:77:0c\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.49\",\"mac\":\"a0:20:a6:30:de:20\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.204\",\"mac\":\"60:01:94:86:74:b8\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.49\",\"mac\":\"a0:20:a6:30:de:20\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.204\",\"mac\":\"60:01:94:86:74:b8\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.15\",\"mac\":\"2c:3a:e8:11:8e:2e\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.53\",\"mac\":\"78:0f:77:5a:e3:f0\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.26\",\"mac\":\"0c:ee:99:07:df:08\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.41\",\"mac\":\"ec:fa:bc:06:6d:93\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.30\",\"mac\":\"dc:a6:32:09:22:a1\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.7\",\"mac\":\"d0:e7:82:f0:c4:57\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.70\",\"mac\":\"50:02:91:68:ea:7f\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.18\",\"mac\":\"ec:fa:bc:06:80:d2\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.56\",\"mac\":\"dc:ef:ca:bc:96:c3\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.33\",\"mac\":\"6c:ad:f8:61:b6:ad\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.22\",\"mac\":\"84:0d:8e:ad:ad:c1\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.37\",\"mac\":\"b0:35:b5:b6:fe:bd\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.203\",\"mac\":\"84:0d:8e:44:2f:a2\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.100\",\"mac\":\"3c:df:a9:6c:bf:de\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.10\",\"mac\":\"b4:e6:2d:67:b7:d0\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.48\",\"mac\":\"5c:cf:7f:fa:b7:45\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.230\",\"mac\":\"00:01:fb:00:1f:41\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.25\",\"mac\":\"44:65:0d:5f:a5:9d\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.14\",\"mac\":\"8c:f5:a3:68:55:0f\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.52\",\"mac\":\"2c:3a:e8:01:1f:0f\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.65\",\"mac\":\"1c:69:7a:0f:87:76\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.2\",\"mac\":\"00:d0:2d:43:8d:62\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.17\",\"mac\":\"60:38:e0:5c:3f:85\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.6\",\"mac\":\"60:01:94:69:2d:b5\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.21\",\"mac\":\"9c:8e:cd:0e:98:51\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.59\",\"mac\":\"50:02:91:69:78:9c\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.32\",\"mac\":\"50:e5:49:56:4c:87\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.9\",\"mac\":\"60:01:94:66:ad:29\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.63\",\"mac\":\"3c:5c:c4:31:59:d5\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.72\",\"mac\":\"a4:ba:db:b8:96:45\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.36\",\"mac\":\"cc:50:e3:5b:66:8a\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.202\",\"mac\":\"60:01:94:ab:45:90\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.13\",\"mac\":\"30:95:e3:24:03:fd\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.143\",\"mac\":\"a0:20:a6:0f:38:fa\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.24\",\"mac\":\"80:7d:3a:7f:e8:f3\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.1\",\"mac\":\"18:1b:eb:7b:0d:df\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.55\",\"mac\":\"00:18:dd:06:9a:fd\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.64\",\"mac\":\"1c:69:7a:0f:87:76\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.131\",\"mac\":\"b8:27:eb:66:72:3b\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.28\",\"mac\":\"6c:ad:f8:30:a9:f7\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.5\",\"mac\":\"60:01:94:51:d1:25\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.43\",\"mac\":\"a4:ee:57:5b:e2:87\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.16\",\"mac\":\"60:01:94:69:36:df\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.47\",\"mac\":\"24:4c:e3:ec:a1:c2\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.20\",\"mac\":\"5c:cf:7f:b4:61:a3\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.58\",\"mac\":\"3c:5c:c4:0f:40:8e\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.35\",\"mac\":\"d8:f1:5b:c6:4f:48\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.201\",\"mac\":\"60:01:94:ab:53:2b\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.8\",\"mac\":\"7c:a7:b0:9b:a5:d8\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.62\",\"mac\":\"10:ae:60:a7:2a:48\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.39\",\"mac\":\"00:d0:2d:b2:52:c5\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.50\",\"mac\":\"4c:11:ae:0d:81:ef\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.27\",\"mac\":\"84:0d:8e:ad:a7:5b\",\"iface\":\"eth0\"},{\"ip\":\"192.168.1.54\",\"mac\":\"1c:69:7a:0a:62:c6\",\"iface\":\"eth0\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":60,"wires":[["f6a9b90c.76b6e8"]]},{"id":"34f6bd9e.cef3a2","type":"debug","z":"1d2ad601.7d596a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":60,"wires":[]}]

But wouldn't it make more sense to compare to the MAC address so no need to assign an IP in the router?

For me an ARP check has worked almost flawlessly to detect if me or my girlfriend is at home for 2 years. I use it to turn off the lights when both of us have left the apartment. We both use Android phones which make a difference. Some here have complained it doesn't work for them.

I think you will find that ARP is not sufficiently reliable. There have been many threads here on how to detect presence and no real conclusion on the best way to do it.
I do it by determining whether the phone is connected to the WiFi, by using fixed IP addresses and ping. Even that is not perfect as a phone that has not been used for a while may go to sleep and fail to connect for some time after arrival. The technique has become less perfect over time as phones have become more aggressive at saving battery usage.

1 Like

Both me and my girlfriend have a relatively new Android phone (Samsung Galaxy S9 and S10) and it's worked 100% if both have WiFi enabled. So it's definitely worth a shot. I have a delay of 5 or 10 minutes to check the availability of a phone.
I've also setup a similar thing to state trail which displays a time line of both our presence and it can show a continuous "home" status days on end (during the corona situation for example).

1 Like

Following jsonata query also does the job:

$[ip = '192.168.1.31' ]
1 Like

Thanks. I would hope it's that easy, but my payload is an array of objects. There is no payload.ip
How do you detect the MAC address in your setup? Like I said, I'm not committed to any method at this time.

jsonata is fancy but it is easy enough to do with plain JavaScript.

var ipToSearch = "192.168.1.31"

var found = msg.payload.find(element => element.ip === ipToSearch)// returns object if exists, undefined otherwise
if(found){
    msg.payload = found
    return msg;  
}else{
    node.warn(ipToSearch+" is not found")
}

or with split and change nodes

[{"id":"39d57941.2edb76","type":"function","z":"94f3e22a.5d351","name":"","func":"var p = [\n   {\n      \"ip\":\"192.168.1.67\",\n      \"mac\":\"cc:50:e3:0c:6e:00\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.31\",\n      \"mac\":\"c4:1c:ff:13:9e:61\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.4\",\n      \"mac\":\"dc:a6:32:09:22:a1\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.42\",\n      \"mac\":\"bc:dd:c2:25:9a:0f\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.19\",\n      \"mac\":\"14:91:38:8f:71:68\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.57\",\n      \"mac\":\"98:5f:d3:e6:aa:9b\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.23\",\n      \"mac\":\"1c:69:7a:0a:62:c6\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.61\",\n      \"mac\":\"a4:ba:db:b8:96:45\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.34\",\n      \"mac\":\"68:54:fd:33:c2:79\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.101\",\n      \"mac\":\"8c:7f:3b:76:18:c8\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.11\",\n      \"mac\":\"00:bb:3a:e4:77:0c\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.49\",\n      \"mac\":\"a0:20:a6:30:de:20\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.204\",\n      \"mac\":\"60:01:94:86:74:b8\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.49\",\n      \"mac\":\"a0:20:a6:30:de:20\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.204\",\n      \"mac\":\"60:01:94:86:74:b8\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.15\",\n      \"mac\":\"2c:3a:e8:11:8e:2e\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.53\",\n      \"mac\":\"78:0f:77:5a:e3:f0\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.26\",\n      \"mac\":\"0c:ee:99:07:df:08\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.41\",\n      \"mac\":\"ec:fa:bc:06:6d:93\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.30\",\n      \"mac\":\"dc:a6:32:09:22:a1\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.7\",\n      \"mac\":\"d0:e7:82:f0:c4:57\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.70\",\n      \"mac\":\"50:02:91:68:ea:7f\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.18\",\n      \"mac\":\"ec:fa:bc:06:80:d2\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.56\",\n      \"mac\":\"dc:ef:ca:bc:96:c3\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.33\",\n      \"mac\":\"6c:ad:f8:61:b6:ad\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.22\",\n      \"mac\":\"84:0d:8e:ad:ad:c1\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.37\",\n      \"mac\":\"b0:35:b5:b6:fe:bd\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.203\",\n      \"mac\":\"84:0d:8e:44:2f:a2\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.100\",\n      \"mac\":\"3c:df:a9:6c:bf:de\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.10\",\n      \"mac\":\"b4:e6:2d:67:b7:d0\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.48\",\n      \"mac\":\"5c:cf:7f:fa:b7:45\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.230\",\n      \"mac\":\"00:01:fb:00:1f:41\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.25\",\n      \"mac\":\"44:65:0d:5f:a5:9d\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.14\",\n      \"mac\":\"8c:f5:a3:68:55:0f\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.52\",\n      \"mac\":\"2c:3a:e8:01:1f:0f\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.65\",\n      \"mac\":\"1c:69:7a:0f:87:76\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.2\",\n      \"mac\":\"00:d0:2d:43:8d:62\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.17\",\n      \"mac\":\"60:38:e0:5c:3f:85\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.6\",\n      \"mac\":\"60:01:94:69:2d:b5\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.21\",\n      \"mac\":\"9c:8e:cd:0e:98:51\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.59\",\n      \"mac\":\"50:02:91:69:78:9c\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.32\",\n      \"mac\":\"50:e5:49:56:4c:87\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.9\",\n      \"mac\":\"60:01:94:66:ad:29\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.63\",\n      \"mac\":\"3c:5c:c4:31:59:d5\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.72\",\n      \"mac\":\"a4:ba:db:b8:96:45\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.36\",\n      \"mac\":\"cc:50:e3:5b:66:8a\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.202\",\n      \"mac\":\"60:01:94:ab:45:90\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.13\",\n      \"mac\":\"30:95:e3:24:03:fd\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.143\",\n      \"mac\":\"a0:20:a6:0f:38:fa\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.24\",\n      \"mac\":\"80:7d:3a:7f:e8:f3\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.1\",\n      \"mac\":\"18:1b:eb:7b:0d:df\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.55\",\n      \"mac\":\"00:18:dd:06:9a:fd\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.64\",\n      \"mac\":\"1c:69:7a:0f:87:76\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.131\",\n      \"mac\":\"b8:27:eb:66:72:3b\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.28\",\n      \"mac\":\"6c:ad:f8:30:a9:f7\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.5\",\n      \"mac\":\"60:01:94:51:d1:25\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.43\",\n      \"mac\":\"a4:ee:57:5b:e2:87\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.16\",\n      \"mac\":\"60:01:94:69:36:df\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.47\",\n      \"mac\":\"24:4c:e3:ec:a1:c2\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.20\",\n      \"mac\":\"5c:cf:7f:b4:61:a3\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.58\",\n      \"mac\":\"3c:5c:c4:0f:40:8e\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.35\",\n      \"mac\":\"d8:f1:5b:c6:4f:48\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.201\",\n      \"mac\":\"60:01:94:ab:53:2b\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.8\",\n      \"mac\":\"7c:a7:b0:9b:a5:d8\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.62\",\n      \"mac\":\"10:ae:60:a7:2a:48\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.39\",\n      \"mac\":\"00:d0:2d:b2:52:c5\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.50\",\n      \"mac\":\"4c:11:ae:0d:81:ef\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.27\",\n      \"mac\":\"84:0d:8e:ad:a7:5b\",\n      \"iface\":\"eth0\"\n   },\n   {\n      \"ip\":\"192.168.1.54\",\n      \"mac\":\"1c:69:7a:0a:62:c6\",\n      \"iface\":\"eth0\"\n   }\n]\nmsg.payload = p\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":280,"wires":[["9ba70e96.98a9d","4443f46a.0a113c"]]},{"id":"8801ffd1.1b616","type":"inject","z":"94f3e22a.5d351","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["39d57941.2edb76"]]},{"id":"9ba70e96.98a9d","type":"function","z":"94f3e22a.5d351","name":"","func":"var ipToSearch = \"192.168.1.31\"\n\nvar found = msg.payload.find(element => element.ip === ipToSearch)// returns object if exists, undefined otherwise\nif(found){\n    msg.payload = found\n    return msg;  \n}else{\n    node.warn(ipToSearch+\" is not found\")\n}\n","outputs":1,"noerr":0,"x":450,"y":240,"wires":[["b4b8c730.eb1218"]]},{"id":"b4b8c730.eb1218","type":"debug","z":"94f3e22a.5d351","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":240,"wires":[]},{"id":"56dcfafb.27b7f4","type":"switch","z":"94f3e22a.5d351","name":"","property":"payload.ip","propertyType":"msg","rules":[{"t":"eq","v":"192.168.1.31","vt":"str"},{"t":"eq","v":"192.168.1.32","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":300,"wires":[["fe3d2b2.a9ed9d8"],["865f4948.1202e8"]]},{"id":"4443f46a.0a113c","type":"split","z":"94f3e22a.5d351","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":450,"y":300,"wires":[["56dcfafb.27b7f4"]]},{"id":"fe3d2b2.a9ed9d8","type":"debug","z":"94f3e22a.5d351","name":"31","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":280,"wires":[]},{"id":"865f4948.1202e8","type":"debug","z":"94f3e22a.5d351","name":"32","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":320,"wires":[]}]
1 Like

Did you import my test flow? It has an inject node with the data you described. How it works is that JSONata creates a new array with only all the ip's from the result then evaluates the check if the given ip is on the array of ip numbers.

This was a useful question btw! I didn't know the answer so it made me look up the documentation and learn something new. :slightly_smiling_face:

I'm really on the borderline if I like the expressiveness of JSONata or if there's too much magic going on to render the expression incomprehensible in a few months time. :grin:

1 Like

Thank you everyone for your suggestions. I learned something from each of you.

My (complete??) solution is to feed the output of ARP node into a function like HotNipi suggested, except to search for the MAC address, and return a msg of true or false, which is then selected in a switch node.

var macToSearch = "c4:1c:ff:13:9e:60";

var found = msg.payload.find(element => element.mac === macToSearch);  // returns object if exists, undefined otherwise
if(found){
    msg.payload = true;
}else{
    node.warn(macToSearch+" is not found");
    msg.payload = false;
}
return msg;

1 Like

Great that you got it working! Note that my suggestion '192.168.1.31' in payload.ip as well as the other JSONata suggestions also return true/false if you put them in a change node (set msg.payload to expression).

But most important in the end is that you understand how the code/expression works so a JavaScript snippet is better if you feel it's easier to understand later on.

I tried your test flow, but all I got was "payload: array[71], jsonataStatus: "Invalid JSON string ([object O…" }"

As I said, the output of the ARP node didn't look like JSON data, but I am just barely understanding JSON, so JSONATA is still beyond my grasp at this time.

But, from your suggestion, I did switch my search to the MAC address.

And for the record - I also used JavaScript on my own solution as I hadn't taken the time to get into JSONata yet. It seems I've wanted to go all fancy and experiment with functional programming and Ramda JavaScript library. :joy:

const R = global.get('ramda');
const { payload } = msg;

const ristomatti = '08:c5:e1:xx:xx:xx';
const riia = '8c:b8:4a:xx:xx:xx';

const macMatches = R.curry((mac, arp) => arp.mac === mac);
const found = R.curry(mac => R.any(macMatches(mac), payload));

return [
  { payload: found(ristomatti) },
  { payload: found(riia) },
  { 
    payload: {
      ristomatti: found(ristomatti),
      riia: found(riia)
    }
  }
];

This creates a payload to 3 outputs two of which I use to draw the presence graphs and one that is used to check if both is away. I don't remember why I didn't just send a true/false on the third output but what is done is done. :grinning:

That is strange as I tested it... Which Node-RED version are you running? Did it fail with that error already in my example flow or with the actual data from the arp node?

It's Version 0.17.5
I know it's old, but every time I upgrade Node Red, something breaks. On the other hand, this is my test installation on a Raspberry Pi. I like to test things here so that if I really screw it up, my main Node-Red on an Intel NUC running Ubuntu is not affected.

It wouldn't hurt to try a version upgrade on the Raspberry, but first, I'll copy the Micro SD.

My main Node-Red is Version 1.0.6, and this is where the working flows will be located.

I was using the msg.payload data from the ARP.

Ok my guess is that it bundles a too old version of JSONata which could explain why it didn't work out.

I tried again on the Version 1.0.6 NR, same "Invalid JSON String".
But, I have a working function now. Thanks for the help.

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