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 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]]); } } 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 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 } step 1 add a device follow the steps to connect a device docid\ ish7bqhzxswtdx8vbnszb 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 docid\ xgwokqbtpevii7or82ll0 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 float 64 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) navigate to analytics on the analytics canvas, click add processor the create a processor dialog box displays select datahub subscribe in the topic field, click the search icon, select the device you previously created, and then select the alias topic for the tag1 tag click save 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]]); } } click save 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 on the analytics canvas, click save the configured analytics flows should look like the following step 3b configure multiple inputs javascript function (user scenario 2) navigate to analytics on the analytics canvas, click add processor the create a processor dialog box displays select datahub subscribe in the topic field, click the search icon, select the device you previously created, and then select the alias topic for the tag1 tag repeat steps 2 4 in step 3b to add another datahub subscribe node select the alias topic for the tag1 tag again click save 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 } click save 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 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 on the analytics canvas, click save the configured analytics flows should look like the following 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 view output for user scenario 2 the javascript function calculates the sum from the defined tag values and gives the output total pressure