Product Features
Flows Manager
Function Nodes
17min
function nodes are powerful tools in flows refer to the following sections for information on flow node capabilities contexts and globals reading json objects combining inputs binary data hex values contexts and globals flows function nodes support different context levels for variables the different contexts allow you to share data among unconnected nodes and among different flows entirely the three levels there are three levels for function node variables that range from less visible to more visible more visibility makes pieces of information available to more nodes and more flows level 1 context the context level makes a variable visible to different functions within a node the example below shows what a context level variable can do that a regular variable cannot 1\ connect nodes together 2\ double click the inject node and change the payload to string enter the payload as your string 3\ double click the function node and enter the following lines function f1 () { // declares a function with name, f1 context set("cv", " and the context variable "); // sets our context variable, named cv, to a string message } function f2 (msg) { // declares a function named f2 that takes msg as a parmeter msg payload = msg payload + context get("cv"); // adds the message in our context variable to the msg payload return msg; } f1(); // calls f1 to set the context variable return f2(msg); // returns the result of calling f2 4\ click done click deploy , and then click the button on left side of the inject node 5\ see the output below in the debug tab meanwhile, if you try to do the same thing with a regular variable function f1 () { var contextvariable = " and the context variable "; } function f2 (msg) { msg payload = msg payload + contextvariable; return msg; } f1(); return f2(msg); you can see the below output in the debug window level 1 context level 2 flow the flows level makes a variable visible to all nodes in the flow the example below shows how to use this context level 1\ connect nodes together like the picture below 2, double click the inject node and change the payload to string enter the payload as your string 3\ double click the upper function node and enter the following lines flow\ set("fv", " here is our flow context variable "); // set a flow context variable, named fv, to be a string message msg payload = "a variable for this whole flow "; // sets the msg payload to be a short string message return msg; 4\ click done double click the lower function node and enter the following lines msg payload = flow\ get("fv"); // sets our message to the string stored in fv return msg; 5\ click done , and then click deploy 6\ click the button on left side of the upper inject node, then click the lower inject node button you can see the output below in the debug tab tip you can use the flow context variable in any node in the flow, even if nodes are unconnected level 3 global this global level example below makes a variable visible to all nodes in every flow that you create 1\ connect nodes together like the picture below 2\ double click the inject node and change the payload to string enter a global message! as your string 3\ double click the function node and enter the following lines global set("global var", msg payload); // stores the message payload from the inject node in a global variable named global var return msg; 4\ click done in a new flow, connect nodes together like in step 1 5\ double click the function node and enter the following lines global set("global var", msg payload); return msg; 6\ click done , and then click deploy 7\ return to the first flow that you made and click the inject node button 8\ on the second flow, click the inject node button tip you can use the flow context variable in any node in the flow, even if nodes are unconnected you can also access global context variables through inject nodes by selecting global from the dropdown menu reading json objects 1\ from the flow definition view, connect a datahub subscribe node, a function node, and a debug node 2\ navigate to devicehub > tags 3\ click the copy icon next to an existing tag to copy a raw topic add a tag if there are none 4\ double click the datahub subscribe node and paste the copied topic 5\ double click the function node enter the following lines of code var obj = json parse(msg payload); // stores json text as an object var temp = json stringify(obj registerid); // gets the registerid from our object and makes it a string msg payload = temp; // assigns the registerid string to our output message payload string return msg; 6\ click done 7\ click deploy 8\ expand the message window beneath the flow and click the debug icon a message appears in the messages pane combining inputs the join function node combines two different inputs into a single output this node combines outputs from multiple flows into a single message to make a flow that combines two different inputs 1\ from the flow definition view, connect two input sources, such as inject nodes, to a join node and a debug node 2\ double click each inject node and select string from the drop down menu adjacent to msg payload for each node, enter any message that you want to combine with the other click done 3\ double click the join node and select manual mode from the drop down menu field information mode automatic mode makes assumptions about your flow manual mode lets you configure settings specific to your flow combine each sets the msg property to combine it is typically msg payload to create sets the data type for the output a string, an array, a key/value object, or a merged object joined using specific to string outputs sets the character to join the strings on \n is a newline character that appears at the end of messages, this is ideal for most cases using specific to key/value objects sets the part of the msg object to serve as the property key for the output object after a number of message parts sets the amount of messages for the join node to receive before sending the combined output after a timeout following the first message sets the amount of time (in seconds) to wait for messages after receiving the first one before sending the combined output note join nodes do not send a message unless the msg complete property is set if needed, you can always set this property to true with a function node 4\ for this example, enter 2 for the after a number of message parts field click done 5\ click deploy 6\ expand the message window beneath the flow and click the debug icon 7\ click both buttons to the left of the inject nodes to view the output in the debug tab at the bottom of the flow screen binary data function nodes can convert data into binary for viewing and performing operations on individual bits of data to create a flow that converts data into binary and can show individual bits of data 1\ from the flow definition view, connect an inject node, a function node, and a debug node 2\ double click the function node and enter the following lines of code function decimaltobinary (decimal) { // declares a function, named decimaltobinary, that converts decimal to binary return decimal tostring(2); // converts decimal into binary by passing in 2 (for base 2) into tostring } function getbit (number, bit) { // declares a function to get a single bit from a number / ands the number with 00000001 shifted by the bit you want to get in this example, we get the second bit from 3 (0000011 in binary), by anding with 00000010 to get the 2nd bit / var bit = number & (1 << bit); / if we output bit as is, it shows up as "10" the following if statement ensures that our output is "1" or "0" / if (bit == 0) { return 0; } return 1; } var decimal = msg payload; // assigns the number from the msg payload to a variable, called decimal var binary = decimaltobinary(decimal); // stores the result from the decimaltobinary function in a variable, called binary var secondbit = getbit(binary, 1); // gets the bit in the 1 position (the 2nd bit) the rightmost bit is the 0 position / construct a string message with our results and output the message / msg payload = "decimal " + decimal + ", binary " + binary + ", secondbit " + secondbit; return msg; 3\ click done 4\ double click the inject node and select number from the drop down menu adjacent to msg payload this example uses 3 , but you can enter any number that you wish 5\ click done 6\ click deploy 7\ expand the message window beneath the flow and click the debug icon hex values function nodes can convert hexadecimal (hex) values into normal decimal integers this is useful when performing operations on hex values to create a flow to convert hex values to integers 1\ from the flow definition view, connect an inject node, a function node, and a debug node 2\ double click the function node and enter the following lines of code function hextoint(hex) { // declares a function, named hextoint, for converting hex to integers return parseint(hex, 16); // converts the hex into an integer by passing in 16 (for base 16) to parseint } var hex = msg payload; // saves the msg payload to pass into our function var decimal = hextoint(hex); // calls the function to convert the hex into a decimal integer msg payload = "hex " + hex + ", decimal " + decimal; // formats the text for output return msg; 3\ click done 4\ double click the inject node and select string from the drop down menu adjacent to msg payload enter in any hex value to convert to an integer this example uses 0x00a8 , which is 168 as a decimal integer 5\ click done 6\ click deploy 7\ expand the message window beneath the flow and click the debug icon 8\ click the button to the left of the inject node