Product Features
Flows Manager

Function Nodes

18min

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.

Flows Manager : Level 1 Context Node Arrangement
Flows Manager : Level 1 Context Node Arrangement


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:

JS


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


Meanwhile, if you try to do the same thing with a regular variable:

JS


You can see the below output in the debug window:

Flow Manager view debug message
Flow Manager view debug message


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:

Flows Manager : Level 2 Flow Node Arrangement
Flows Manager : Level 2 Flow Node Arrangement


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:

JS


4. Click Done. Double-click the lower Function node and enter the following lines:

JS


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


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:

Flows Manager : Level 3 Global Node Arrangement
Flows Manager : Level 3 Global Node Arrangement


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:

JS


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:

JS


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


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.


Reading JSON Objects

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


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:

JS


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


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.

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.


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


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.

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


2. Double-click the Function node and enter the following lines of code:

JS


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


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.

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


2. Double-click the Function node and enter the following lines of code:

JS


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