Get Started with Accessing Data from AVEVA PI Asset Framework
This example shows how to connect to an AVEVA® PI Asset Framework (AF) server, locate asset information, and read data from specified AF attributes.
This example connects to an AF server on the local network and uses a sample AF database provided by AVEVA. You can download the database from AVEVA sharefile.
Connect to Asset Framework and Access AF Database
Connect to the AVEVA PI Asset Framework server using the afclient function. This example uses the Windows® computer name as the Asset server name because the server is hosted in the local machine. The name might vary depending on your PI System configuration.
host = getenv("COMPUTERNAME");
client = afclient(host);Select the database that contains the relevant assets and data.
selectDatabase(client,"Database2")Search for Elements or Attributes in AF Database
Find an element in the database with a specified name using the findElementByName function. This example finds the generator element named GU1 Generator.
generatorElement = findElementByName(client,"GU1 Generator")generatorElement =
Element with properties:
Name: "GU1 Generator"
Path: "\\VDI-BGLWDP-192\Database2\Flynn River Hydro\Flynn I\GU1\GU1 Generator"
Database: "Database2"
Categories: ""
Template: "Generator"
Description: ""
NumChildren: 0
NumAttributes: 220
To find an attribute of an element using partial name, perform a wildcard search using the getAttributes function. This example searches for attributes with the prefix Active.
activePowerAttribute = getAttributes(generatorElement,"Active*")activePowerAttribute =
Attribute with properties:
Name: "Active Power"
Path: "\\VDI-BGLWDP-192\Database2\Flynn River Hydro\Flynn I\GU1\GU1 Generator|Active Power"
Categories: "Electrical Data;"
Description: "Active Power generated by the generation unit."
ElementName: "GU1 Generator"
ServerDataType: "Double"
DefaultUnit: "megawatt"
ReadAccess: 1
WriteAccess: 0
HasTimeSeriesData: 1
Alternatively, if you know the attribute path, use the findAttributeByPath function to find the attribute. The following code searches the AF database and creates an attribute object in the MATLAB® workspace when the specified path is valid.
attributePath = "\\VDI-BGLWDP-192\Database2\Flynn River Hydro\Flynn I\GU1\GU1 Generator|Active Power";
activePowerAttribute = findAttributeByPath(client,attributePath);
Tip: To graphically browse the element hierarchy and select attributes, use the Asset Framework Browser.
Read Current Value of Attribute
Read the current value of the Active Power attribute using the read function. The output displays the attribute value in its default unit of measure.
currentValue = read(activePowerAttribute)
currentValue=1×5 table
Attribute Element Value Unit Status
______________ _______________ ________ __________ ______
"Active Power" "GU1 Generator" {[1014]} "megawatt" "Good"
To query for the list of supported units for an attribute, use listSupportedUnits.
listSupportedUnits(activePowerAttribute)
ans = 12×1 string
"gigawatt"
"megawatt"
"million British thermal unit per day"
"million British thermal unit per hour"
"watt"
"British thermal unit per hour"
"calorie per second"
"horsepower"
"joule per second"
"kilowatt"
"megajoule per hour"
"million calorie per hour"
You can now retrieve the data in one of the supported units. This example retrieves the current value of the attribute in kilowatt.
currentValue = read(activePowerAttribute,Unit="kilowatt")currentValue=1×5 table
Attribute Element Value Unit Status
______________ _______________ ___________ __________ ______
"Active Power" "GU1 Generator" {[1014000]} "kilowatt" "Good"
Read Historical Data for Attribute Over Specified Time Range
Read historical data stored in the Active Power attribute over a specified time range using readHistory. You can also use this function to read calculated values over defined intervals. This example reads averaged power data in gigawatts at 4-hour intervals within a specified 10 day period.
starttime = datetime("01/01/2020",'InputFormat','dd/MM/uuuu'); endtime = starttime + days(10); historicalData = readHistory(activePowerAttribute,starttime,endtime,Unit="gigawatt",Interval=hours(4),AggregateFcn="average")
historicalData=61×5 timetable
Time Attribute Element Value Unit Status
______________________________ ______________ _______________ __________ __________ ______
01-Jan-2020 00:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.0320]} "gigawatt" "Good"
01-Jan-2020 04:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[0.9700]} "gigawatt" "Good"
01-Jan-2020 08:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[0.9540]} "gigawatt" "Good"
01-Jan-2020 12:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.0070]} "gigawatt" "Good"
01-Jan-2020 16:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[0.9940]} "gigawatt" "Good"
01-Jan-2020 20:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.1310]} "gigawatt" "Good"
02-Jan-2020 00:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.0220]} "gigawatt" "Good"
02-Jan-2020 04:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[0.9230]} "gigawatt" "Good"
02-Jan-2020 08:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.2370]} "gigawatt" "Good"
02-Jan-2020 12:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.4470]} "gigawatt" "Good"
02-Jan-2020 16:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.3180]} "gigawatt" "Good"
02-Jan-2020 20:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.2870]} "gigawatt" "Good"
03-Jan-2020 00:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.0750]} "gigawatt" "Good"
03-Jan-2020 04:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[0.9620]} "gigawatt" "Good"
03-Jan-2020 08:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.2510]} "gigawatt" "Good"
03-Jan-2020 12:00:00 UTC+05:30 "Active Power" "GU1 Generator" {[1.4840]} "gigawatt" "Good"
⋮