Skip to main content
PI Asset Framework
PI Notifications
PI System Development
OSIsoft AFSDK

How to Retrieve AF Trigger Limits and Set Up Notification Comparison Rules via PI AFSDK

Learn how to use PI AFSDK to retrieve trigger limits (upper/lower bounds) for an AF Attribute or PI Tag and create notification comparison rules programmatically.

Roshan Soni

6 min read

Programmatically Retrieving AF Trigger Details and Limits Using AFSDK

When working with OSIsoft PI AF (Asset Framework), it's common to build solutions that depend on triggering notifications or analyses when certain conditions or thresholds are met. This is especially true in scenarios where you need to monitor the values of PI Tags or AF Attributes and alert on upper or lower bounds. In this post, we'll explore how you can retrieve AF trigger details, such as limits associated with an attribute or tag, and set up comparison triggers programmatically using the AFSDK.

Use Case: Accessing and Setting Attribute Limits for Notification Triggers

Suppose you have a PI Tag or AF Attribute and you need to automate the creation of notification triggers when its value breaches a specified minimum or maximum limit. Additionally, you want to retrieve the AF Element's full path to report or log where in the AF hierarchy the trigger is being set.

Step 1: Efficiently Find the AF Attribute or PI Tag

To work with a specific attribute or tag, you can utilize AFAttribute.FindElementAttributes to search for attributes by name directly, rather than iterating through every element. This improves performance, especially in large databases.

AFAttributeList attribs = AFAttribute.FindElementAttributes(
    afDatabase, // Your AFDatabase
    null,       // AFElement (null for db-wide search)
    string.Empty, // Template, none specified
    null,       // Category, none specified
    null,       // Description, none specified
    AFElementType.Any,
    tagName,    // The PI Tag or Attribute name
    null,       // Path, not specified
    TypeCode.Empty, // Type, any
    true,       // Search all templates
    AFSortField.Name,
    AFSortOrder.Ascending,
    100         // Max number of results
);

Step 2: Retrieve the Full Path of the Element

If you need the full path of the AFElement for the notification, you can easily get it from the attribute's parent element.

foreach (AFAttribute attr in attribs)
{
    string elementPath = attr.Element.GetPath();
    // Use this path as needed
}

Step 3: Accessing and Setting Attribute Limits

In many PI AF templates, limits such as High, HighHigh, Low, and LowLow are often modeled as child attributes or as static values within the attribute configuration. To programmatically get or set these limits:

object lowLimit = attr.Attributes["LowLimit"]?.GetValue()?.Value;
object highLimit = attr.Attributes["HighLimit"]?.GetValue()?.Value;
// If the attribute is linked to a PI Point, you may also check trait values:
object span = attr.GetTrait(PICommonPointTraits.Span);
object zero = attr.GetTrait(PICommonPointTraits.Zero);

Note: The exact method depends on how limits are modeled in your AF structure (as child attributes, traits, or custom data references).

Step 4: Creating Analysis and Notification Triggers Programmatically

Once you have the attribute, its element path, and required limits, you can create an AFAnalysis with comparison rules. Here's a simplified snippet for creating rules that trigger if the attribute's value falls outside specified bounds:

// Assume minVal and maxVal are double limits
AFAnalysis analysis = afDatabase.Analyses.Add("MyAnalysis");
analysis.Target = attr.Element;
analysis.AnalysisRulePlugIn = afServer.AnalysisRulePlugIns["ConditionGroup"];

// Example: LessThan rule
AFAnalysisRule lessThanRule = afServer.AnalysisRulePlugIns["Comparison"].CreateAnalysisRuleInstance(analysis, null);
lessThanRule.ConfigString = $"Operator=LessThan; TargetValue={minVal}; Targetvaluetype=Numeric;";
lessThanRule.MapVariable("input", attr.Name);

// Example: GreaterThan rule
AFAnalysisRule greaterThanRule = afServer.AnalysisRulePlugIns["Comparison"].CreateAnalysisRuleInstance(analysis, null);
greaterThanRule.ConfigString = $"Operator=GreaterThan; TargetValue={maxVal}; Targetvaluetype=Numeric;";
greaterThanRule.MapVariable("input", attr.Name);

analysis.CheckIn();

Additional Tips

  • Hierarchical Memory: When designing notification triggers, always make sure you're associating them with the correct AF path. This traceability is critical for troubleshooting.
  • Modeling Best Practices: Store limits as attributes (or via PI Point traits where possible) to standardize retrieval logic.
  • Efficient Attribute Search: Always prefer bulk search methods like AFAttribute.FindElementAttributes over manual iteration for scalability.

Conclusion

By leveraging PI AF SDK's advanced search and modeling capabilities, you can efficiently retrieve attribute information, limits, and set up powerful, automated notification workflows. This approach saves time, reduces manual configuration, and ensures that alerts are always associated with the correct place in your AF hierarchy.


Have you implemented similar programmatic solutions in your PI AF environment? Share your experiences and lessons learned in the comments below!

Tags

#PI Notifications
#AFSDK
#C#
#PI Tag
#AF Attribute Limits
#AFElement
#AFAttribute.FindElementAttributes
#AF Hierarchy
#Comparison Triggers

About Roshan Soni

Expert in PI System implementation, industrial automation, and data management. Passionate about helping organizations maximize the value of their process data through innovative solutions and best practices.

Sign in to comment

Join the conversation by signing in to your account.

Comments (0)

No comments yet

Be the first to share your thoughts on this article.

Related Articles

Enhancing PI ProcessBook Trends with Banding and Zones: User Needs, Workarounds, and the Road Ahead

A look at the user demand for trend banding/zoning in OSIsoft PI ProcessBook, current VBA workarounds, UI challenges, and how future PI Vision releases aim to address these visualization needs.

Roshan Soni

Migrating PIAdvCalcFilVal Uptime Calculations from PI DataLink to PI OLEDB

Learn how to translate PI DataLink's PIAdvCalcFilVal advanced calculations—like counting uptime based on conditions—into efficient PI OLEDB SQL queries. Explore three practical approaches using PIAVG, PIINTERP, and PICOunt tables, and get tips for validation and accuracy.

Roshan Soni

Understanding PI Web API WebID Encoding: Can You Generate WebIDs Client-Side?

Curious about how PI Web API generates WebIDs and whether you can encode them client-side using GUIDs or paths? This article explores the encoding mechanisms, current documentation, and best practices for handling WebIDs in your applications.

Roshan Soni