[{"data":1,"prerenderedAt":811},["ShallowReactive",2],{"node-red-\u002Fnode-red\u002Fcore-nodes\u002Ffunction":3},{"id":4,"title":5,"body":6,"description":43,"extension":805,"meta":806,"navigation":165,"path":807,"seo":808,"stem":809,"__hash__":810},"nodeRed\u002Fnode-red\u002Fcore-nodes\u002Ffunction.md","Node-RED - Function Node",{"type":7,"value":8,"toc":790},"minimark",[9,14,19,23,26,29,37,132,138,333,340,352,460,464,467,472,485,489,492,496,499,503,506,510,513,533,546,560,564,567,587,591,600,603,608,616,619,622,626,786],[10,11,13],"h1",{"id":12},"function","Function",[15,16,18],"h2",{"id":17},"what-is-a-function-node-in-node-red","What Is a Function node in Node-RED?",[20,21,22],"p",{},"In Node-RED, a function node allows you to write custom JavaScript code to process message objects in your flow. It's used for specific tasks that can't be accomplished with the standard built-in nodes alone. When you write custom JavaScript in a function node, this code gets executed every time a message passes through the node.",[20,24,25],{},"In Node-RED, a function should either return an object, which is a message object, or nothing at all. Returning other data types instead of an object will cause an error. By default, the function node returns the message object unchanged, passing the data as it is to further nodes.",[20,27,28],{},"Ideally, the function node should have the message object returned at the end of the code written within it. Placing the return statement in the middle of the code may result in incomplete execution of the remaining code.",[20,30,31,32,36],{},"If the function node needs to perform an asynchronous action before sending a message, it cannot use the return statement to send the message at the end of the function. Instead, in such cases, you must use ",[33,34,35],"code",{},"node.send()",", as shown below:",[38,39,44],"pre",{"className":40,"code":41,"language":42,"meta":43,"style":43},"language-javascript shiki shiki-themes github-light github-dark","\u002F\u002F Simulate an asynchronous operation with setTimeout\nsetTimeout(() => {\n    \u002F\u002F After 2 seconds, create a message object with some data\n    const message = { payload: \"Async operation complete\" };\n    \u002F\u002F Send the message to subsequent nodes\n    node.send(message);\n}, 2000);\n","javascript","",[33,45,46,55,73,79,102,108,120],{"__ignoreMap":43},[47,48,51],"span",{"class":49,"line":50},"line",1,[47,52,54],{"class":53},"sJ8bj","\u002F\u002F Simulate an asynchronous operation with setTimeout\n",[47,56,58,62,66,70],{"class":49,"line":57},2,[47,59,61],{"class":60},"sScJk","setTimeout",[47,63,65],{"class":64},"sVt8B","(() ",[47,67,69],{"class":68},"szBVR","=>",[47,71,72],{"class":64}," {\n",[47,74,76],{"class":49,"line":75},3,[47,77,78],{"class":53},"    \u002F\u002F After 2 seconds, create a message object with some data\n",[47,80,82,85,89,92,95,99],{"class":49,"line":81},4,[47,83,84],{"class":68},"    const",[47,86,88],{"class":87},"sj4cs"," message",[47,90,91],{"class":68}," =",[47,93,94],{"class":64}," { payload: ",[47,96,98],{"class":97},"sZZnC","\"Async operation complete\"",[47,100,101],{"class":64}," };\n",[47,103,105],{"class":49,"line":104},5,[47,106,107],{"class":53},"    \u002F\u002F Send the message to subsequent nodes\n",[47,109,111,114,117],{"class":49,"line":110},6,[47,112,113],{"class":64},"    node.",[47,115,116],{"class":60},"send",[47,118,119],{"class":64},"(message);\n",[47,121,123,126,129],{"class":49,"line":122},7,[47,124,125],{"class":64},"}, ",[47,127,128],{"class":87},"2000",[47,130,131],{"class":64},");\n",[20,133,134,135,137],{},"Additionally, if you need to pass a message object mid-script within a function node to subsequent nodes, you can utilize ",[33,136,35],{}," for this purpose while continuing the execution of the remaining code, as shown below:",[38,139,141],{"className":40,"code":140,"language":42,"meta":43,"style":43},"\u002F\u002F Extract data from the incoming message\nconst inputData = msg.payload;\n\n\u002F\u002F Perform some processing\nconst processedData = inputData * 2;\n\n\u002F\u002F Send a message object with the processed data\nnode.send({ payload: `Processed data: ${processedData}` });\n\n\u002F\u002F Continue executing the rest of the code...\n\n\u002F\u002F Example of further processing...\nif (processedData > 100) {\n    node.warn(\"High processed data value detected!\");\n} else {\n    node.log(\"Processed data value within normal range.\");\n}\n\n\u002F\u002F Return the modified message object\nreturn msg;\n\n",[33,142,143,148,161,167,172,193,197,202,225,230,236,241,247,265,281,292,307,313,318,324],{"__ignoreMap":43},[47,144,145],{"class":49,"line":50},[47,146,147],{"class":53},"\u002F\u002F Extract data from the incoming message\n",[47,149,150,153,156,158],{"class":49,"line":57},[47,151,152],{"class":68},"const",[47,154,155],{"class":87}," inputData",[47,157,91],{"class":68},[47,159,160],{"class":64}," msg.payload;\n",[47,162,163],{"class":49,"line":75},[47,164,166],{"emptyLinePlaceholder":165},true,"\n",[47,168,169],{"class":49,"line":81},[47,170,171],{"class":53},"\u002F\u002F Perform some processing\n",[47,173,174,176,179,181,184,187,190],{"class":49,"line":104},[47,175,152],{"class":68},[47,177,178],{"class":87}," processedData",[47,180,91],{"class":68},[47,182,183],{"class":64}," inputData ",[47,185,186],{"class":68},"*",[47,188,189],{"class":87}," 2",[47,191,192],{"class":64},";\n",[47,194,195],{"class":49,"line":110},[47,196,166],{"emptyLinePlaceholder":165},[47,198,199],{"class":49,"line":122},[47,200,201],{"class":53},"\u002F\u002F Send a message object with the processed data\n",[47,203,205,208,210,213,216,219,222],{"class":49,"line":204},8,[47,206,207],{"class":64},"node.",[47,209,116],{"class":60},[47,211,212],{"class":64},"({ payload: ",[47,214,215],{"class":97},"`Processed data: ${",[47,217,218],{"class":64},"processedData",[47,220,221],{"class":97},"}`",[47,223,224],{"class":64}," });\n",[47,226,228],{"class":49,"line":227},9,[47,229,166],{"emptyLinePlaceholder":165},[47,231,233],{"class":49,"line":232},10,[47,234,235],{"class":53},"\u002F\u002F Continue executing the rest of the code...\n",[47,237,239],{"class":49,"line":238},11,[47,240,166],{"emptyLinePlaceholder":165},[47,242,244],{"class":49,"line":243},12,[47,245,246],{"class":53},"\u002F\u002F Example of further processing...\n",[47,248,250,253,256,259,262],{"class":49,"line":249},13,[47,251,252],{"class":68},"if",[47,254,255],{"class":64}," (processedData ",[47,257,258],{"class":68},">",[47,260,261],{"class":87}," 100",[47,263,264],{"class":64},") {\n",[47,266,268,270,273,276,279],{"class":49,"line":267},14,[47,269,113],{"class":64},[47,271,272],{"class":60},"warn",[47,274,275],{"class":64},"(",[47,277,278],{"class":97},"\"High processed data value detected!\"",[47,280,131],{"class":64},[47,282,284,287,290],{"class":49,"line":283},15,[47,285,286],{"class":64},"} ",[47,288,289],{"class":68},"else",[47,291,72],{"class":64},[47,293,295,297,300,302,305],{"class":49,"line":294},16,[47,296,113],{"class":64},[47,298,299],{"class":60},"log",[47,301,275],{"class":64},[47,303,304],{"class":97},"\"Processed data value within normal range.\"",[47,306,131],{"class":64},[47,308,310],{"class":49,"line":309},17,[47,311,312],{"class":64},"}\n",[47,314,316],{"class":49,"line":315},18,[47,317,166],{"emptyLinePlaceholder":165},[47,319,321],{"class":49,"line":320},19,[47,322,323],{"class":53},"\u002F\u002F Return the modified message object\n",[47,325,327,330],{"class":49,"line":326},20,[47,328,329],{"class":68},"return",[47,331,332],{"class":64}," msg;\n",[20,334,335,336,339],{},"If you don't want the function to pass anything to subsequent node, you can do this by returning ",[33,337,338],{},"null"," in the function node.",[20,341,342,343,347,348,351],{},"By default, a function node has a single output, but you can configure it to have multiple outputs in the ",[344,345,346],"strong",{},"setup tab"," with the ",[344,349,350],{},"output"," property. You can then send the message to each output using an array, placing them in order of which output they should go to.",[38,353,355],{"className":40,"code":354,"language":42,"meta":43,"style":43},"var msg1 = { payload: 1 };\nvar msg2 = { payload: [3,45,2,2,4] };\nvar msg3 = { payload: {\"name\":\"bob\"} };\nvar msg4 = { payload: \"This is string\" };\nreturn [msg1, msg2, msg3, msg4];\n",[33,356,357,375,413,437,453],{"__ignoreMap":43},[47,358,359,362,365,368,370,373],{"class":49,"line":50},[47,360,361],{"class":68},"var",[47,363,364],{"class":64}," msg1 ",[47,366,367],{"class":68},"=",[47,369,94],{"class":64},[47,371,372],{"class":87},"1",[47,374,101],{"class":64},[47,376,377,379,382,384,387,390,393,396,398,401,403,405,407,410],{"class":49,"line":57},[47,378,361],{"class":68},[47,380,381],{"class":64}," msg2 ",[47,383,367],{"class":68},[47,385,386],{"class":64}," { payload: [",[47,388,389],{"class":87},"3",[47,391,392],{"class":64},",",[47,394,395],{"class":87},"45",[47,397,392],{"class":64},[47,399,400],{"class":87},"2",[47,402,392],{"class":64},[47,404,400],{"class":87},[47,406,392],{"class":64},[47,408,409],{"class":87},"4",[47,411,412],{"class":64},"] };\n",[47,414,415,417,420,422,425,428,431,434],{"class":49,"line":75},[47,416,361],{"class":68},[47,418,419],{"class":64}," msg3 ",[47,421,367],{"class":68},[47,423,424],{"class":64}," { payload: {",[47,426,427],{"class":97},"\"name\"",[47,429,430],{"class":64},":",[47,432,433],{"class":97},"\"bob\"",[47,435,436],{"class":64},"} };\n",[47,438,439,441,444,446,448,451],{"class":49,"line":81},[47,440,361],{"class":68},[47,442,443],{"class":64}," msg4 ",[47,445,367],{"class":68},[47,447,94],{"class":64},[47,449,450],{"class":97},"\"This is string\"",[47,452,101],{"class":64},[47,454,455,457],{"class":49,"line":104},[47,456,329],{"class":68},[47,458,459],{"class":64}," [msg1, msg2, msg3, msg4];\n",[15,461,463],{"id":462},"function-node-different-tabs","Function Node Different Tabs",[20,465,466],{},"In the function node, we have four different types of tabs, each with its unique use case:",[468,469,471],"h3",{"id":470},"setup","Setup",[473,474,475,479,482],"ul",{},[476,477,478],"li",{},"Output: This property allows you to configure how many outputs the function node will have.",[476,480,481],{},"Timeout: This property Allows you to define how long the function node can run before an error is raised. By default, if set to 0, no timeout is applied.",[476,483,484],{},"Modules: This property Allows you to add or import additional modules into Function nodes, and they will be automatically installed when the flow is deployed. However, in the - settings, you'll need to set 'functionExternalModules' to 'true'.",[468,486,488],{"id":487},"on-start","On Start",[20,490,491],{},"In this tab, you can provide code that will run whenever the node is started. This can be used to set up any state the Function node requires.",[468,493,495],{"id":494},"on-message","On Message",[20,497,498],{},"This is the tab where you can provide the JavaScript code that will execute when it receives a message passed by another nodes.",[468,500,502],{"id":501},"on-stop","On Stop",[20,504,505],{},"This tab allows you to add code to clean up any ongoing tasks or close connections before the flow is redeployed.",[15,507,509],{"id":508},"logging-events","Logging events",[20,511,512],{},"When a function node needs to log something, it can utilize the following methods:",[473,514,515,521,527],{},[476,516,517,520],{},[33,518,519],{},"node.log()",": This is used for general logging purposes.",[476,522,523,526],{},[33,524,525],{},"node.warn()",": This method is used to log warnings.",[476,528,529,532],{},[33,530,531],{},"node.error()",": This is used to log errors.",[20,534,535,536,538,539,541,542,545],{},"Messages logged using ",[33,537,525],{}," and ",[33,540,531],{}," will be sent to the ",[344,543,544],{},"debug tab",".",[20,547,548,549,551,552,555,556,559],{},"To view messages logged using ",[33,550,519],{},", you can check the command from where you started Node-RED. If you're running it under an app like PM2, it will have its own method for displaying logs. On a Raspberry Pi, the install script adds a ",[33,553,554],{},"node-red-log"," command that shows the log. If you're using FlowFuse Cloud, you can find the logged messages in the Instance's ",[344,557,558],{},"Node-RED logs"," tab.",[15,561,563],{"id":562},"node-red-objects-accessible-in-function-node","Node-RED Objects Accessible in Function Node",[20,565,566],{},"The following Node-RED objects can be accessed in a function node:",[473,568,569,572,575,578,581,584],{},[476,570,571],{},"node: This object encapsulates properties and methods used for customizing and interacting with nodes in a flow.",[476,573,574],{},"context: The node’s local context.",[476,576,577],{},"flow: The flow scope context.",[476,579,580],{},"global: The global scope context.",[476,582,583],{},"RED: This object provides module access to the Node-RED runtime API.",[476,585,586],{},"env: This object contains the get method to access environment variables.",[15,588,590],{"id":589},"use-cases-and-examples","Use-cases and examples",[592,593,594],"ol",{},[476,595,596,599],{},[344,597,598],{},"Custom logic",": Sometimes your flow might require very specific logic that can’t be achieved using existing nodes. Function node allow you to implement this custom logic.",[20,601,602],{},"In the example flow below, we have a function that converts temperature data, simulated using an inject node with a random number, from Celsius to Fahrenheit. Additionally, it performs other formatting.",[604,605],"render-flow",{":height":606,"flow":607},"200","W3siaWQiOiJiMTM4YjYwM2M4Zjk0Y2Y4IiwidHlwZSI6ImluamVjdCIsInoiOiJhMjI0MGVhOTUyMDUxZTgxIiwibmFtZSI6IlRlbXBlcmF0dXJlIHNlbnNvciIsInByb3BzIjpbeyJwIjoicGF5bG9hZCJ9XSwicmVwZWF0IjoiNSIsImNyb250YWIiOiIiLCJvbmNlIjp0cnVlLCJvbmNlRGVsYXkiOjAuMSwidG9waWMiO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vdXQiOjAsIm5vZXJyIjowLCJpbml0aWFsaXplIjoiIiwiZmluYWxpemUiOiIiLCJsaWJzIjpbXSwieCI6NTEwLCJ5IjoyNDAsIndpcmVzIjpbWyJiNzI3MWJkMzI1OWJjNTZkIl1dfSx7ImlkIjoiYjcyNzFiZDMyNTliYzU2ZCIsInR5cGUiOiJkZWJ1ZyIsInoiOiJhMjI0MGVhOTUyMDUxZTgxIiwibmFtZSI6ImRlYnVnIDEiLCJhY3RpdmUiOnRydWUsInRvc2lkZWJhciI6dHJ1ZSwiY29uc29sZSI6ZmFsc2UsInRvc3RhdHVzIjpmYWxzZSwiY29tcGxldGUiOiJmYWxzZSIsInN0YXR1c1ZhbCI6IiIsInN0YXR1c1R5cGUiOiJhdXRvIiwieCI6NzYwLCJ5IjoyNDAsIndpcmVzIjpbXX0seyJpZCI6IjhmNTUzYTE1ZTRmNWE4ZGQiLCJ0eXBlIjoiY29tbWVudCIsInoiOiJhMjI0MGVhOTUyMDUxZTgxIiwibmFtZSI6IkNvbnZlcnQgcmVjZWl2ZWQgdGVtcGVyYXR1cmUgZnJvbSBDZWxzaXVzIHRvIEZhaHJlbmhlaXQgYW5kIGZvcm1hdCBpdCIsImluZm8iOiIiLCJ4Ijo1MTAsInkiOjE4MCwid2lyZXMiOltdfV0=",[592,609,610],{"start":57},[476,611,612,615],{},[344,613,614],{},"Conditional Routing:"," When dealing with a broad range of conditions that require intricate logic for each case, the switch node may fall short. In such scenarios, using a function node with multiple outputs can be benificial.",[20,617,618],{},"In the example flow below, we have an inject node generating a random number and sending it to the function node. We've set up the function node to evaluate the received numeric value and perform conditional routing based on predefined ranges, sending the message to different outputs accordingly.",[604,620],{":height":606,"flow":621},"W3siaWQiOiI2YTFmOGRkNWU1MDM3ZDI0IiwidHlwZSI6ImluamVjdCIsInoiOiJhMjI0MGVhOTUyMDUxZTgxIiwibmFtZSI6InNlbmQgcmFuZG9tIG51bWJlciIsInByb3BzIjpbeyJwIjoicGF5bG9hZCJ9XSw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HNlLCJ0b3N0YXR1cyI6ZmFsc2UsImNvbXBsZXRlIjoiZmFsc2UiLCJzdGF0dXNWYWwiOiIiLCJzdGF0dXNUeXBlIjoiYXV0byIsIngiOjc0MCwieSI6MTgwLCJ3aXJlcyI6W119LHsiaWQiOiI1YTk5ZWJjNGVlNDM5NzU3IiwidHlwZSI6ImRlYnVnIiwieiI6ImEyMjQwZWE5NTIwNTFlODEiLCJuYW1lIjoiZGVidWcgMyIsImFjdGl2ZSI6dHJ1ZSwidG9zaWRlYmFyIjp0cnVlLCJjb25zb2xlIjpmYWxzZSwidG9zdGF0dXMiOmZhbHNlLCJjb21wbGV0ZSI6ImZhbHNlIiwic3RhdHVzVmFsIjoiIiwic3RhdHVzVHlwZSI6ImF1dG8iLCJ4Ijo3NDAsInkiOjIyMCwid2lyZXMiOltdfSx7ImlkIjoiYjFlOGNhMmZhYTNmYWFmYyIsInR5cGUiOiJjb21tZW50IiwieiI6ImEyMjQwZWE5NTIwNTFlODEiLCJuYW1lIjoiVGhlIGZ1bmN0aW9uIGNoZWNrcyB0aGUgbnVtZXJpYyB2YWx1ZSBhZ2FpbnN0IGR5bmFtaWMgcmFuZ2VzIGFuZCBzZW5kcyB0aGVtIHRvIG91dHB1dHMgYWNjb3JkaW5nbHkuIiwiaW5mbyI6IiIsIngiOjQ1MCwieSI6ODAsIndpcmVzIjpbXX1d",[15,623,625],{"id":624},"node-documentation","Node Documentation",[627,628,631,634,635,634,641,634,648,634,651,634,661,634,664,634,668,634,678,634,683,634,689,634,692,634,700,634,707,634,710,634,713,634,717,634,720,634,737,634,746,634,749,634,753,634,756,634,776,634,780],"div",{"className":629},[630],"core-node-doc",[20,632,633],{},"A JavaScript function to run against the messages being received by the node."," ",[20,636,637,638,545],{},"The messages are passed in as a JavaScript object called ",[33,639,640],{},"msg",[20,642,643,644,647],{},"By convention it will have a ",[33,645,646],{},"msg.payload"," property containing\nthe body of the message.",[20,649,650],{},"The function is expected to return a message object (or multiple message objects), but can choose\nto return nothing in order to halt a flow.",[20,652,653,654,657,658,660],{},"The ",[655,656,488],"b",{}," tab contains code that will be run whenever the node is started.\nThe ",[655,659,502],{}," tab contains code that will be run when the node is stopped.",[20,662,663],{},"If the On Start code returns a Promise object, the node will not start handling messages\nuntil the promise is resolved.",[468,665,667],{"id":666},"details","Details",[20,669,670,671,677],{},"See the ",[672,673,676],"a",{"target":674,"href":675},"_blank","https:\u002F\u002Fnodered.org\u002Fdocs\u002Fwriting-functions.html","online documentation","\nfor more information on writing functions.",[679,680,682],"h4",{"id":681},"sending-messages","Sending messages",[20,684,685,686,545],{},"The function can either return the messages it wants to pass on to the next nodes\nin the flow, or can call ",[33,687,688],{},"node.send(messages)",[20,690,691],{},"It can return\u002Fsend:",[473,693,694,697],{},[476,695,696],{},"a single message object - passed to nodes connected to the first output",[476,698,699],{},"an array of message objects - passed to nodes connected to the corresponding outputs",[20,701,702,703,706],{},"Note: The setup code is executed during the initialization of nodes. Therefore, if ",[33,704,705],{},"node.send"," is called in the setup tab, subsequent nodes may not be able to receive the message.",[20,708,709],{},"If any element of the array is itself an array of messages, multiple\nmessages are sent to the corresponding output.",[20,711,712],{},"If null is returned, either by itself or as an element of the array, no\nmessage is passed on.",[679,714,716],{"id":715},"logging-and-error-handling","Logging and Error Handling",[20,718,719],{},"To log any information, or report an error, the following functions are available:",[473,721,722,727,732],{},[476,723,724],{},[33,725,726],{},"node.log(\"Log message\")",[476,728,729],{},[33,730,731],{},"node.warn(\"Warning\")",[476,733,734],{},[33,735,736],{},"node.error(\"Error\")",[20,738,739,740,742,743,430],{},"The Catch node can also be used to handle errors. To invoke a Catch node,\npass ",[33,741,640],{}," as a second argument to ",[33,744,745],{},"node.error",[38,747,748],{},"node.error(\"Error\",msg);",[679,750,752],{"id":751},"accessing-node-information","Accessing Node Information",[20,754,755],{},"The following properties are available to access information about the node:",[473,757,758,764,770],{},[476,759,760,763],{},[33,761,762],{},"node.id"," - id of the node",[476,765,766,769],{},[33,767,768],{},"node.name"," - name of the node",[476,771,772,775],{},[33,773,774],{},"node.outputCount"," - number of node outputs",[679,777,779],{"id":778},"using-environment-variables","Using environment variables",[20,781,782,783,545],{},"Environment variables can be accessed using ",[33,784,785],{},"env.get(\"MY_ENV_VAR\")",[787,788,789],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":43,"searchDepth":57,"depth":57,"links":791},[792,793,799,800,801,802],{"id":17,"depth":57,"text":18},{"id":462,"depth":57,"text":463,"children":794},[795,796,797,798],{"id":470,"depth":75,"text":471},{"id":487,"depth":75,"text":488},{"id":494,"depth":75,"text":495},{"id":501,"depth":75,"text":502},{"id":508,"depth":57,"text":509},{"id":562,"depth":57,"text":563},{"id":589,"depth":57,"text":590},{"id":624,"depth":57,"text":625,"children":803},[804],{"id":666,"depth":75,"text":667},"md",{},"\u002Fnode-red\u002Fcore-nodes\u002Ffunction",{"title":5,"description":43},"node-red\u002Fcore-nodes\u002Ffunction","7azoNsPnfervey2EEMipVEXoNL5YrutY_8uiX6X8arU",1780070555918]