How-To Guides
Analytics Guides

Use the JavaScript Processor Function

11min

You can use the JavaScript processor function for customizing and manipulating data from input tags using JavaScript programming language. This processor is helpful for scenarios where you need to add, remove, or modify fields in incoming messages.

You can rename fields, modify timestamps, or aggregate data based on your processing needs.

User Scenario

Review the following scenarios where the JavaScript processor function is used to manipulate incoming data for real-world applications. Then, you will simulate PLC data to customize input data in manufacturing settings effectively.

User Scenario 1 (Using one Input Function)

In a manufacturing plant, temperature sensors send data to a monitoring system. The sensor readings include several fields, but you only need specific ones to analyze furnace pressure. Using the JavaScript processor, you can filter out unnecessary fields and rename required fields for clarity.

Code Example:

JS


User Scenario 2 (Using multiple Input Functions)

In an assembly line, multiple devices report operational data, including pressure, speed, and temperature. To calculate the total pressure from all devices, you aggregate the value fields of each input message using the JavaScript processor.

Code Example:

JS


Step 1: Add a Device

Follow the steps to Connect a Device and configure the following parameters:

  • Device Type: Simulator
  • Driver Name: Generator
  • Enable Alias Topics: Select the checkbox.

Step 2: Add Tags

After connecting the device, add the following tags. See Add Tags to learn more. Each tag will represent one of the user scenarios noted above.

Tag 1

  • Name: Select S - Random value generator.
  • Value Type: Select float64.
  • Polling Interval: Enter 5.
  • Tag Name: Enter Tag1.
  • Min Value: Enter 1.
  • Max Value: Enter 10.

Step 3: Create Analytics Flows

You can now create the analytics flows using data from the device and tag you previously created. Follow the appropriate step out of 3a or 3b based on the two user scenarios for the JavaScript processor function you want to use.

Step 3a: Configure Single Input JavaScript Function (User Scenario 1)

  1. Navigate to Analytics.
  2. On the analytics canvas, click Add processor. The Create a processor dialog box displays.

    The Add processor option
    The Add processor option
    
  3. Select DataHub Subscribe.
  4. In the Topic field, click the Search icon, select the device you previously created, and then select the alias topic for the Tag1 tag.

    Create a Processor dialog box
    Create a Processor dialog box
    
  5. Click Save.
  6. Click Add processor again and select JavaScript processor. The following information defines this function:
    • Text Editor: Write the JavaScript code you want to execute. Learn more here. For this example, paste the code from User Scenario 1 above.
    • var ids = inputProcessors(); function transformMessage(message) { // Rename 'value' to 'furnacePressure' if (message.hasOwnProperty('value')) { message.furnacePressure = message.value; delete message.value; } // Keep only selected fields return { success: message.success, deviceId: message.deviceId, furnacePressure: message.furnacePressure }; } if (ids.length > 0) { if (values && values[ids[0]]) { result = transformMessage(values[ids[0]]); } }
  7. Click Save.

    Edit a Processor Dialog Box
    Edit a Processor Dialog Box
    
  8. Connect the DataHub Subscribe processor (tag: Tag1) to the JavaScript processor with a wire and enter the definition of A for the value-type connection.
  9. On the analytics canvas, click Save.

The configured analytics flows should look like the following:

Completed canvas flow with one input
Completed canvas flow with one input


Step 3b: Configure Multiple Inputs JavaScript Function (User Scenario 2)

  1. Navigate to Analytics.
  2. On the analytics canvas, click Add processor. The Create a processor dialog box displays.

    The Add processor option
    The Add processor option
    
  3. Select DataHub Subscribe.
  4. In the Topic field, click the Search icon, select the device you previously created, and then select the alias topic for the Tag1 tag.

    Create a Processor dialog box
    Create a Processor dialog box
    
  5. Repeat steps 2-4 in Step 3b to add another DataHub Subscribe node. Select the alias topic for the Tag1 tag again.
  6. Click Save.
  7. Click Add processor again and select JavaScript processor. The following information defines this function:
    • Text Editor: Write the JavaScript code you want to execute. Learn more here. For this example, paste the code from User Scenario 2 above.
    • var ids = inputProcessors(); var totalPressure = 0; if (ids.length > 0) { result["ids"] = ids; // Store input identifiers for (var i = 0; i < ids.length; i++) { var message = values[ids[i]]; if (message && message.value !== undefined) { totalPressure += message.value; // Aggregate 'value' fields } } result["total_pressure"] = totalPressure; // Output the total pressure }
  8. Click Save.

    Edit a Processor Dialog Box
    Edit a Processor Dialog Box
    
  9. Connect the DataHub Subscribe processor (tag: Tag1) to the JavaScript processor with a wire and enter the definition of A for the value-type connection.
  10. Connect another DataHub Subscribe processor (tag: Tag1) to the JavaScript processor with a wire and enter the definition of B for the value-type connection.
  11. On the analytics canvas, click Save.

The configured analytics flows should look like the following:

Completed canvas flow with multiple inputs
Completed canvas flow with multiple inputs


Step 4: View Output of Processor

Click the View icon in the JavaScript processor to view the output values.

View Output for User Scenario 1

The JavaScript function filters the furnace pressure from the defined tag values.

Output for single input JavaScript
Output for single input JavaScript


View Output for User Scenario 2

The JavaScript function calculates the sum from the defined tag values and gives the output Total_pressure.

Output for multiple input JavaScript
Output for multiple input JavaScript