Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Scripts & Data EnrichEnrichment (Tutorial) teaches you to:

  • Create alarm groups and items
  • Configure alarm conditions
  • Set up notifications
  • Enable audit trail for FDA 21 CFR Part 11 compliance

Prerequisites:

  • calculated tags using expressions
  • Build reusable logic with Script Classes
  • Automate processes with Script Tasks
  • Handle events and data transformations
  • Monitor script performance and debugging

Prerequisites:

In this page:

Table of Contents
maxLevel2
minLevel2
indent10px
excludeSteps
stylenone




Scripts → Tutorial | Concept | How-to Guide | Reference



Simple

Create Tag Expressions

  1. Navigate to Scripts → Expressions
  2. Add calculated tag:

Temperature Conversion:

Create TagName:
  1. temperature conversion:
FieldValue
ObjectTag.Tank1_TempF (
use New Tag on top toolbar to create the Tag, type: double
create as Double)
Expression
: @Tag
Tag.Tank1_
TempC * 9/5 + 32
Temp * 1.8 + 32
ExecutionOnChange
DomainServer

Add status logic:

FieldValue
ObjectTag.

Conditional Logic:

Create TagName:
System_Status (
type: Text
create as String)
Expression
: @Tag.Motor1_Running AND @Tag.Pump1_Running ?
TIF(Tag.Tank1_Status = 2, "Running"
:
, "Stopped")
ExecutionOnChange
DomainServer

Create Script Task

  1. Go to Scripts → Tasks
  2. Add new task:
Production Counter
  1. Click plus icon, name: ShiftCounter
  2. Use Server.DateTimeInfo.Hour as Trigger
  3. Double-click to open Code Editor
  4. Enter code (no method wrapper):

csharp

// Reset counter at shift change
DateTimeint nowhour = DateTime@Server.DateTimeInfo.NowHour;
if (now.Hourhour == 6 || now.Hourhour == 14 || now.Hourhour == 22)
{
    if (@Tag.ShiftReset == false)
    {
        @Tag.Units_Previous = @Tag.Units_Produced;
        @Tag.Units_Produced = 0;
        @Tag.ShiftReset = true;
    }
}
else
{
    @Tag.ShiftReset = false;
}

Configure:

  • Trigger: Timer
  • Rate: 60000ms (1 minute)
  • Domain: Server

    Build Script Class

    1. Navigate to Scripts → Classes
    2. Create class:
    TankCalculations
    1. TankCalculations
    2. Enter methods (no class declaration):

    csharp

        public static double CalculateVolume(double level, double diameter)
        {
            double radius = diameter / 2;
            return Math.PI * radius * radius * level;
        }
        
        public static double FlowRateCalculateFlowRate(double volumeStart, double volumeEnd, int minutes)
    {
        {
    if (minutes == 0) return 0;
        return (volumeEnd - volumeStart) / minutes;
        }
        
        public static string GetAlarmTextGetStatusText(double value, double limit)
        {
            if (value > limit)
                return $"High: {value:F2} (Limit: {limit:F2})";
            else
                return "Normal";
        }
    

    Use Class in Expression

    1. Go to Scripts → Expressions

    In tag expressions:

    csharp

    @Script
    1. Create volume calculation:
    FieldValue
    ObjectTag.Tank1_Volume
    ExpressionScript.Class.TankCalculations.CalculateVolume(
    @Tag
    Tag.Tank1_Level,
    10.5)
    Execution

    Python Integration

    1. Scripts → Settings
    2. Enable Python
    3. Create Python script:

    python

    OnChange

    Note: No @ symbol needed in expressions


    Event-Driven Task

    1. Create task: TemperatureMonitor
    2. Configure trigger on tag change:

    csharp

    import numpy as np
    from datetime import datetime
    
    def calculate_statistics(values):
        """Calculate statistical values"""
        arr = np.array(values)
        return if (@Tag.Tank1_Temp > @Tag.HighLimit)
    {
            'mean': np.mean(arr),
            'std': np.std(arr),@Tag.Alarm_Active = true;
            'max': np.max(arr),
            'min': np.min(arr)
        }
    
    def predict_value(history, hours_ahead):
        """Simple linear prediction"""
        # Implementation here
        return predicted_value
    
    # Set tag values
    tags['Stats_Mean'] = calculate_statistics(history)['mean']

    Event-Driven Scripts

    Create event handler:

    csharp

    // On tag change event
    public void OnTemperatureChange()
    {
        if (@Tag.Tank1_Temperature > @Tag.HighLimit)
        {
            @Tag.Alarm_Active = true;
        @Tag.Alarm_Message = $"High Temperature: {@Tag.Tank1_Temp:F1}°C";
        @Tag.Alarm_Time = DateTime.Now;
        
        // Log to historian
        @Script.Class.NotificationsServerMain.SendEmail(
                "operator@company.com",
        LogEvent(
            "High Temperature AlertAlarm",
                $"Tank 1: {@Tag.Tank1_Temperature}°C"
        Temp
        );
        }
    }

    Bind to tagConfigure:

    • Trigger: Tag: .Tank1_TemperatureTemp
    • Event: OnValueChange
    • Script: OnTemperatureChange

    Test Scripts

    1. Start Runtime with debugger
    2. Set breakpoints in scripts
    3. Monitor script execution:
      • Scripts → Monitor
      • Check execution time
      • View error messages

    Next Steps

  • [SQL Database Query] - Database integration
  • [Reports Module] - Automated reporting
  • [Create Dashboards] - Display calculations
    • Domain: Server

    Add External References

    1. Go to Scripts → References
    2. Add DLL if using external libraries
    3. Open Code Editor
    4. Click Namespace Declarations
    5. Add required namespaces

    Test and Debug

    1. Build verification:
      • Check green checkmark in BuildStatus
      • Review BuildErrors if red X
    2. Runtime monitoring:
      • Go to Scripts → Monitor
      • Check ExecutionCount
      • Review LastCPUTime
    3. Debug with breakpoints:
      • Enable debug in Runtime → Build and Publish
      • Set breakpoints in Code Editor
      • Attach debugger during runtime



    Step-by-step guide to add calculations, automation, and data enrichment to your TankFarm solution using Scripts module capabilities.


    In this section...

    Page Tree
    root@parent
    spaces93DRAF