Function Nodes
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
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.
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.
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.
![Flows Manager : Level 1 Context Node Arrangement Flows Manager : Level 1 Context Node Arrangement](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/_AvYRaOUYvVtvTIy_3s4D_image.png?format=webp)
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:
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:
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/-mCvp2WmWDNfSo1yigCt5_image.png?format=webp)
Meanwhile, if you try to do the same thing with a regular variable:
You can see the below output in the debug window:
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/BcdulqpEvMrjq_wmZg8cw_image.png?format=webp)
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:
![Flows Manager : Level 2 Flow Node Arrangement Flows Manager : Level 2 Flow Node Arrangement](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/sK1OYdbIndGF4WSyh5GJb_image.png?format=webp)
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:
4. Click Done. Double-click the lower Function node and enter the following lines:
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:
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/SSM_ECsqtidnF9RePQHpS_image.png?format=webp)
Tip: You can use the flow context variable in any node in the Flow, even if nodes are unconnected.
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:
![Flows Manager : Level 3 Global Node Arrangement Flows Manager : Level 3 Global Node Arrangement](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/RgUN6FDBfUEbk9CjEyW2p_image.png?format=webp)
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:
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:
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.
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/Y7kuSMyinMmNaAWA-oO7k_image.png?format=webp)
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.
![Editing Inject node's msg.payload into global. Editing Inject node's msg.payload into global.](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/zlPmDBaFfLFr9sQNhqtKN_image.png?format=webp)
1. From the Flow Definition view, connect a DataHub Subscribe node, a Function node, and a Debug node.
![DataHub Subscribe with a raw topic connected to a Function node. The Function node connects to a Debug node DataHub Subscribe with a raw topic connected to a Function node. The Function node connects to a Debug node](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/nJV3hQuEGerf58oKP-o9s_image.png?format=webp)
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:
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.
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/W1eM_qbUG5CCKIHJ1i6iI_image.png?format=webp)
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.
![Two inject nodes connected to Join nodes. The Join node connects to Debug node. Two inject nodes connected to Join nodes. The Join node connects to Debug node.](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/f65B4X52bmJXVxjYASDEZ_image.png?format=webp)
2. Double-click each Inject node and select string from the drop-down menu adjacent to msg.payload. For each node, enter any message (Litmus and Automation in this example) 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.
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/ZWziH4bKpgb9s_d1TORYX_image.png?format=webp)
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.
![Inject node connect to a Function node that connects to a Debug node Inject node connect to a Function node that connects to a Debug node](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/tFu8mSycBnBeNGUhqiPXE_image.png?format=webp)
2. Double-click the Function node and enter the following lines of code:
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.
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/4-uMofYANI5sEiUwzqq-O_image.png?format=webp)
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.
![Inject node connect to a Function node that connects to a Debug node Inject node connect to a Function node that connects to a Debug node](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/9RYqTA8GWLvssGn3q7BGw_image.png?format=webp)
2. Double-click the Function node and enter the following lines of code:
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.
![Flow Manager view debug message Flow Manager view debug message](https://images.archbee.com/SSUUxKZUk9bFTEPNn_6Zo/2K0gVs62LE_rt5pBb3XWm_image.png?format=webp)