Use the Tengo Script Function
You can use Tengo Script to manipulate data. The processor uses Tengo scripting rules similar to the Go programming language.
Note: If you need further information on Tengo scripting, please refer to the following:
This function allows adding, removing, or modifying fields from a single input or multiple inputs.
Each message from the input is stored in an array called values. It is important to note:
- Using the for _, message in values{} statement allows you to access this input and manipulate it.
- The definition in the connection wire is optional for single input but helpful for multi-inputs to differentiate between them.
- The message parameter contains the entire input message, and you can manipulate it as needed.
Note: _ and message can be any custom variable name.
Suppose you have three different inputs attached to this scripting processor, with the connecting wires named A, B, and C:
- for i, message in values{} will now have three inputs.
- i represents the connection wire from which the input is coming.
- With if-else statements, each input can be either individually manipulated or receive a common manipulation.
- You have the option to output the result for one or all inputs.
Review the following scenario for the Tengo Script processor function. Then, you will simulate PLC data and manipulate outputs using a single input and multiple inputs processor.
In a factory setting, many sensors constantly monitor air quality by measuring parameters such as CO2 levels, humidity, and temperature. Using the Tengo Script and inputting a single value, such as a CO2 emission reading, it is possible to manipulate this data stream to create a comprehensive air quality index. This can be achieved by dividing the data reading by the standard average of the factory and then multiplying it by 100.
In a manufacturing plant, three key sensors track the production line's efficiency: one measures the speed of the conveyor belt, another monitors the temperature of machinery, and the third tracks the number of units produced for adjusting the output rate of finished products. Using the Tengo Script with these three inputs, you can optimize production efficiency. It adjusts production speed based on output rate data, converts temperature to a normalized scale for monitoring, and adjusts production rate for the number of units produced.
Follow the steps to Connect a Device and configure the following parameters:
- Device Type: Simulator
- Driver Name: Generator
- Enable Alias Topics: Select the checkbox.
After connecting the device, add the following tags. See Add Tags to learn more. Each tag will be used for simulating the different user scenarios mentioned above.
- Name: Select S - Random value generator
- Value Type: Select int64
- Polling Interval: Enter 5
- Tag Name: Enter co2emission
- Min_value: Enter 50
- Max_value: Enter 800
- Name: Select S - Random value generator
- Value Type: Select float64
- Polling Interval: Enter 5
- Tag Name: Enter speed
- Min_value: Enter 0
- Max_value: Enter 250
- Name: Select S - Random value generator
- Value Type: Select float64
- Polling Interval: Enter 5
- Tag Name: Enter temperature
- Min_value: Enter 10
- Max_value: Enter 150
- Name: Select S - Random value generator
- Value Type: Select int64
- Polling Interval: Enter 5
- Tag Name: Enter units
- Min_value: Enter 1
- Max_value: Enter 1000
You can now create the analytics flows using data from the device and tag you previously created. Follow the appropriate step (3a or 3b) based on the two user scenarios for the Tengo Script function you want to use.
- 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 co2emission tag.
- Click Save.
- Click Add processor again and select Tengo script processor. The following information defines this function:
- Timeout: Enter waiting period in ms in the system before a specific event occurs.
- Code Block: Copy and paste the Single Input User Scenario code example. // Single input example result := 0 for _, message in values{ // 'value' is the CO2 emission reading. // Normalize this reading by dividing by a standard average and multiplying by 100 standardAverage := 400 // Example value for standard CO2 level in parts per million normalizedCO2Index := (float(message["value"]) / standardAverage) * 100 result = { "timestamp": message["timestamp"], "normalizedCO2": normalizedCO2Index, "success": message["success"], "actual value": message["value"] } }
Click Save.
- Connect the DataHub Subscribe processor (tag: co2emission) to the Tengo script processor with a wire and enter the definition of co2emission for the value-type connection.
- On the analytics canvas, click Save.
The configured analytics flows should look like the following:
- 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 speed tag.
- Click Save.
- Repeat steps 2-5 to add another DataHub Subscribe node. Select the alias topic for the temperature and units respectively.
- Click Add processor again and select Tengo script processor. The following information defines this function:
- Timeout: Enter waiting period in ms in the system before a specific event occurs.
- Code Block: Copy and paste the Multiple Inputs User Scenario code example. // Multiple inputs example result := 0 for i, message in values{ // Adjusting conveyor speed based on output rate if i == "A"{ // 'value' is the conveyor speed, adjust it based on output rate (C) speedAdjustmentFactor := 1 + (float(values["C"]["value"]) * 0.01) message["adjustedSpeed"] = float(message["value"]) * speedAdjustmentFactor } // Machinery temperature monitoring if i == "B"{ // Convert temperature to a normalized scale for monitoring message["normalizedTemperature"] = float(message["value"]) / 100 } // Monitoring and adjusting production based on output rate if i == "C"{ // Adjusting production rate, 'value' is the number of units produced productionAdjustmentFactor := 1 + (float(message["value"]) * 0.005) message["adjustedProduction"] = float(message["value"]) * productionAdjustmentFactor } result = { "timestamp": values[i]["timestamp"], "original_value": values[i]["value"], "customized_value": message["adjustedSpeed"] || message["normalizedTemperature"] || message["adjustedProduction"], "input_source": i, "success": message["success"] } }
Click Save.
- Connect the DataHub Subscribe processor (tag: speed) to the Tengo script processor with a wire and enter the definition of A for the value-type connection.
- Connect the DataHub Subscribe processor (tag: temperature) to the Tengo script processor with a wire and enter the definition of B for the value-type connection.
- Connect the DataHub Subscribe processor (tag: units) to the Tengo script processor with a wire and enter the definition of C for the value-type connection.
- On the analytics canvas, click Save.
The configured analytics flows should look like the following:
Click the View icon in the Tengo Script processor to view the output values.
The Tengo Script function calculates the air quality index from the defined tag values and gives the output.
The Tengo Script function manipulates the data from the defined tag values and gives the output.