# Search for function nodes with certain code?

**URL:** <https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612>\
**Category:** General\
**Created:** [12 September 2022 14:14 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612 "2022-09-12T14:14:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [12 September 2022 14:14 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/1 "2022-09-12T14:14:41Z")

</div>

Love the new junction node. Previously I used function nodes for this purpose, i.e. just created a function node with `return msg;`

In all the new snazziness, is there a way to search for function nodes that just contain this line?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 September 2022 14:22 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/2 "2022-09-12T14:22:53Z")

</div>

> [@hazymat](#):
>
> is there a way to search for function nodes that just contain this line?

unfortunately not - you will get all functions with that line (that will likely be the majority of them)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 September 2022 14:50 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/3 "2022-09-12T14:50:05Z")

</div>

You could try exporting the entrie flow (use the pretty format) and then opening it in a text editor.  
Next search for 'return msg' and when you find one, grab the `id` of the msg and in NR you can search for that using the ctrl-f (find).

```auto
{"id":"d2d2785eb1a00b17", <==== this 'd2d2785eb1a00b17' will be what you search on
"type":"function",
"z":"13040b63810e7b1b",
"name":"foo",
"func":"\nreturn msg;",
"outputs":1,
"noerr":0,
"initialize":"",
"finalize":"",
"libs":[],
"x":270,
"y":280,
"wires":[[]]},

```

Each one will need it's own search.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 September 2022 15:08 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/4 "2022-09-12T15:08:12Z")

</div>

you could also load the flow file into msg.payload and search the full array, using a change node and JSONata

```auto
$$.payload[$length($.func)<15].id

```

which should return an array of all id's where function property is less than 15 characters.  
e,g.

```auto
[{"id":"6b47984.fc07568","type":"change","z":"aaa27ee4852b402b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload[$length($.func)<15].id","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":380,"wires":[["f5f3d227.61fc28"]]},{"id":"8f14c8ad.4c12f","type":"inject","z":"aaa27ee4852b402b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"id\":\"aaa27ee4852b402b\",\"type\":\"tab\",\"label\":\"Flash LED\",\"disabled\":false,\"info\":\"\"},{\"id\":\"8fc2f37d8b8d1eba\",\"type\":\"delay\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"pauseType\":\"delayv\",\"timeout\":\"400\",\"timeoutUnits\":\"milliseconds\",\"rate\":\"1\",\"nbRateUnits\":\"1\",\"rateUnits\":\"second\",\"randomFirst\":\"1\",\"randomLast\":\"5\",\"randomUnits\":\"seconds\",\"drop\":false,\"x\":1020,\"y\":160,\"wires\":[[\"77f31641.d63258\"]]},{\"id\":\"b8e91e92.ccca58\",\"type\":\"split\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"splt\":\"1\",\"spltType\":\"len\",\"arraySplt\":1,\"arraySpltType\":\"len\",\"stream\":false,\"addname\":\"\",\"x\":690,\"y\":160,\"wires\":[[\"456471b9.2667e\"]]},{\"id\":\"b71e99df.9661f\",\"type\":\"debug\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"active\":true,\"tosidebar\":true,\"console\":false,\"tostatus\":false,\"complete\":\"false\",\"statusVal\":\"\",\"statusType\":\"auto\",\"x\":870,\"y\":120,\"wires\":[]},{\"id\":\"456471b9.2667e\",\"type\":\"change\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"rules\":[{\"t\":\"set\",\"p\":\"payload\",\"pt\":\"msg\",\"to\":\"$number($$.payload) = 1\",\"tot\":\"jsonata\"},{\"t\":\"set\",\"p\":\"delay\",\"pt\":\"msg\",\"to\":\"$number($$.topic) * ($$.parts.index)\",\"tot\":\"jsonata\"}],\"action\":\"\",\"property\":\"\",\"from\":\"\",\"to\":\"\",\"reg\":false,\"x\":850,\"y\":160,\"wires\":[[\"8fc2f37d8b8d1eba\"]]},{\"id\":\"33d07144.1f6546\",\"type\":\"inject\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"props\":[{\"p\":\"payload\"},{\"p\":\"topic\",\"vt\":\"str\"}],\"repeat\":\"\",\"crontab\":\"\",\"once\":false,\"onceDelay\":0.1,\"topic\":\"400\",\"payload\":\"3\",\"payloadType\":\"str\",\"x\":370,\"y\":60,\"wires\":[[\"5ba6739.ccddc8c\"]]},{\"id\":\"951a4dc9.b79f38\",\"type\":\"inject\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"props\":[{\"p\":\"payload\"},{\"p\":\"topic\",\"vt\":\"str\"}],\"repeat\":\"\",\"crontab\":\"\",\"once\":false,\"onceDelay\":0.1,\"topic\":\"200\",\"payload\":\"5\",\"payloadType\":\"str\",\"x\":350,\"y\":160,\"wires\":[[\"21022e92.635112\"]]},{\"id\":\"3fb67766.cbb2c8\",\"type\":\"inject\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"props\":[{\"p\":\"payload\"},{\"p\":\"topic\",\"vt\":\"str\"}],\"repeat\":\"\",\"crontab\":\"\",\"once\":false,\"onceDelay\":0.1,\"topic\":\"200\",\"payload\":\"1010101010\",\"payloadType\":\"str\",\"x\":360,\"y\":280,\"wires\":[[\"dd89688f.c7b02\"]]},{\"id\":\"21022e92.635112\",\"type\":\"change\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"rules\":[{\"t\":\"set\",\"p\":\"payload\",\"pt\":\"msg\",\"to\":\"$substring(\\\"1010101010101010101010101010101010101010101010101010101010\\\", 0 , $number($$.payload) * 2)\",\"tot\":\"jsonata\"}],\"action\":\"\",\"property\":\"\",\"from\":\"\",\"to\":\"\",\"reg\":false,\"x\":520,\"y\":160,\"wires\":[[\"b8e91e92.ccca58\"]]},{\"id\":\"5ba6739.ccddc8c\",\"type\":\"function\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"func\":\"const timeout = ((count,time) => setTimeout(() => {\\n node.send({payload: count % 2 === 0})\\n }, Number(time) * count\\n ))\\nfor(let i = 0; i < Number(msg.payload) * 2; i++) {\\n timeout(i, msg.topic);\\n}\\nreturn null;\\n\",\"outputs\":1,\"noerr\":0,\"initialize\":\"\",\"finalize\":\"\",\"libs\":[],\"x\":550,\"y\":60,\"wires\":[[\"91d2c758.a70948\"]]},{\"id\":\"91d2c758.a70948\",\"type\":\"debug\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"active\":true,\"tosidebar\":true,\"console\":false,\"tostatus\":false,\"complete\":\"false\",\"statusVal\":\"\",\"statusType\":\"auto\",\"x\":740,\"y\":60,\"wires\":[]},{\"id\":\"cf054a62.3a36b8\",\"type\":\"inject\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"props\":[{\"p\":\"payload\"},{\"p\":\"topic\",\"vt\":\"str\"}],\"repeat\":\"\",\"crontab\":\"\",\"once\":false,\"onceDelay\":0.1,\"topic\":\"400\",\"payload\":\"2\",\"payloadType\":\"str\",\"x\":350,\"y\":120,\"wires\":[[\"f7550e34.44162\"]]},{\"id\":\"f7550e34.44162\",\"type\":\"function\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"func\":\"for(let i = 0; i < Number(msg.payload) * 2; i++) {\\n node.send({payload: i % 2 === 0, delay: Number(msg.topic) * i})\\n}\\nreturn null;\",\"outputs\":1,\"noerr\":0,\"initialize\":\"\",\"finalize\":\"\",\"libs\":[],\"x\":510,\"y\":120,\"wires\":[[\"5168b6da.1baa78\"]]},{\"id\":\"5168b6da.1baa78\",\"type\":\"delay\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"pauseType\":\"delayv\",\"timeout\":\"400\",\"timeoutUnits\":\"milliseconds\",\"rate\":\"1\",\"nbRateUnits\":\"1\",\"rateUnits\":\"second\",\"randomFirst\":\"1\",\"randomLast\":\"5\",\"randomUnits\":\"seconds\",\"drop\":false,\"x\":680,\"y\":120,\"wires\":[[\"b71e99df.9661f\"]]},{\"id\":\"77f31641.d63258\",\"type\":\"debug\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"active\":true,\"tosidebar\":true,\"console\":false,\"tostatus\":false,\"complete\":\"false\",\"statusVal\":\"\",\"statusType\":\"auto\",\"x\":1190,\"y\":220,\"wires\":[]},{\"id\":\"dd89688f.c7b02\",\"type\":\"split\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"splt\":\"1\",\"spltType\":\"len\",\"arraySplt\":1,\"arraySpltType\":\"len\",\"stream\":false,\"addname\":\"\",\"x\":530,\"y\":280,\"wires\":[[\"4e9cfb27.fb1334\"]]},{\"id\":\"4e9cfb27.fb1334\",\"type\":\"change\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"rules\":[{\"t\":\"set\",\"p\":\"payload\",\"pt\":\"msg\",\"to\":\"$number($$.payload) = 1\",\"tot\":\"jsonata\"},{\"t\":\"set\",\"p\":\"delay\",\"pt\":\"msg\",\"to\":\"$number($$.topic) * ($$.parts.index)\",\"tot\":\"jsonata\"}],\"action\":\"\",\"property\":\"\",\"from\":\"\",\"to\":\"\",\"reg\":false,\"x\":690,\"y\":280,\"wires\":[[\"a94a5dce.fac198\"]]},{\"id\":\"a94a5dce.fac198\",\"type\":\"delay\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"pauseType\":\"delayv\",\"timeout\":\"400\",\"timeoutUnits\":\"milliseconds\",\"rate\":\"1\",\"nbRateUnits\":\"1\",\"rateUnits\":\"second\",\"randomFirst\":\"1\",\"randomLast\":\"5\",\"randomUnits\":\"seconds\",\"drop\":false,\"x\":860,\"y\":280,\"wires\":[[\"ea40d8ce.f01e1\"]]},{\"id\":\"ea40d8ce.f01e1\",\"type\":\"debug\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"active\":true,\"tosidebar\":true,\"console\":false,\"tostatus\":false,\"complete\":\"false\",\"statusVal\":\"\",\"statusType\":\"auto\",\"x\":1030,\"y\":280,\"wires\":[]},{\"id\":\"8f14c8ad.4c12f\",\"type\":\"inject\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"props\":[{\"p\":\"payload\"},{\"p\":\"topic\",\"vt\":\"str\"}],\"repeat\":\"\",\"crontab\":\"\",\"once\":false,\"onceDelay\":0.1,\"topic\":\"\",\"payload\":\"\",\"payloadType\":\"date\",\"x\":460,\"y\":380,\"wires\":[[]]},{\"id\":\"dacc79f0.67f2e8\",\"type\":\"function\",\"z\":\"aaa27ee4852b402b\",\"name\":\"\",\"func\":\"\\nreturn msg;\",\"outputs\":1,\"noerr\":0,\"initialize\":\"\",\"finalize\":\"\",\"libs\":[],\"x\":380,\"y\":240,\"wires\":[[]]}]","payloadType":"json","x":440,"y":380,"wires":[["6b47984.fc07568"]]},{"id":"f5f3d227.61fc28","type":"debug","z":"aaa27ee4852b402b","name":"","active":true,"tosidebar":true,"console":false," clicked wrong reply button":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":380,"wires":[]}]

```

[edit] Sorry @Steve-Mcl clicked wrong reply button.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 September 2022 15:13 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/5 "2022-09-12T15:13:34Z")

</div>

A little script to run against your flows file (on a Linux machine, or WSL, Cygwin etc).

As with @zenofmud's suggestion, the file should be in "Pretty Print" format. (`flowFilePretty: true,` in settings.js).

It's far from foolproof but it should give you a list of function nodes containing just `return msg` or `\nreturn msg`.

On my Raspberry Pi it gives this

```auto
"ec3fbe435ba643b0", "func": "\nreturn msg;",
"8188766be3fe8981", "func": "return msg;",

```

Edit - This version will also list all nodes who's label is not shown, it's likely to produce duplicate output

```auto
#! /bin/bash
FILE=/home/pi/.node-red/flows.json

awk ' /"id":/ { lastid = $2}
/"l": false/ { print lastid $0 }
/"func": "return msg/ { print lastid $0 }
/"func": "\\nreturn msg/ { print lastid $0 } ' $FILE

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 September 2022 16:04 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/6 "2022-09-12T16:04:19Z")

</div>

> [@E1cid](#):
>
> [edit] Sorry @Steve-Mcl clicked wrong reply button.

Lol - no worries. I did start to edit my post but I got as far as typing "but you can search the json" and then got distracted by the smell of coffee and never finished it - but you guys have offered some good ideas.

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [12 September 2022 16:26 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/7 "2022-09-12T16:26:25Z")

</div>

Super response all, many thanks!

---

<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:** [11 November 2022 16:26 UTC](https://discourse.nodered.org/t/search-for-function-nodes-with-certain-code/67612/8 "2022-11-11T16:26:27Z")

</div>

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