Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Getting Started

Lesson 3 exercises

  • Last UpdatedJan 15, 2026
  • 6 minute read

Your assignment is to develop a client application that allows users to read data from and write data to the Data Archive.

You will use the Green Power Company database on your local PI AF server to perform the following tasks:

  • Write several methods that retrieve historical data from Data Archive.

  • Print the results to the console.

  • Implement methods to write data.

Before starting these exercises, get familiar with the Green Power Company database in PI System Explorer.

For this lesson, implement the following methods inside Program3.cs and run the application with the arguments provided for each of the following methods in Main().

Exercise 1

Create a method to retrieve interpolated values for the Energy Usage attribute for the specified meter element. The startTime and endTime should be in PI Time syntax. Print the following information for each returned AFValue: Timestamp (Local time), Value in kilowatt hours.

void PrintInterpolated(AFDatabase database, string meterName,
string startTime, string endTime, TimeSpan timeSpan)

When the method is run, it should produce the following output:

Print Interpolated Values - Meter: Meter001, Start: *-30s, End: *
Timestamp (Local): 7/6/2017 4:14:29 PM, Value: 271.21 kWh
Timestamp (Local): 7/6/2017 4:14:39 PM, Value: 270.92 kWh
Timestamp (Local): 7/6/2017 4:14:49 PM, Value: 270.92 kWh
Timestamp (Local): 7/6/2017 4:14:59 PM, Value: 270.92 kWh

Exercise 2

Create a method to print the hourly time-weighted average for the Energy Usage attribute for the specified meter element. The startTime and endTime should be in PI Time syntax with time range larger than one hour. Print the following information for each returned AFValue: Timestamp (Local time), Value in kilowatt hours.

void PrintHourlyAverage(AFDatabase database, string meterName, string startTime, string endTime)

When the method is run, it should produce the following output:

Print Hourly Average - Meter: Meter001, Start: y, End: t
Timestamp (Local): 2017-07-05 00h, Value: 219.71 kWh
Timestamp (Local): 2017-07-05 01h, Value: 269.12 kWh
Timestamp (Local): 2017-07-05 02h, Value: 287.00 kWh
Timestamp (Local): 2017-07-05 03h, Value: 321.93 kWh
Timestamp (Local): 2017-07-05 04h, Value: 304.84 kWh
Timestamp (Local): 2017-07-05 05h, Value: 319.51 kWh
Timestamp (Local): 2017-07-05 06h, Value: 312.74 kWh
Timestamp (Local): 2017-07-05 07h, Value: 307.56 kWh
Timestamp (Local): 2017-07-05 08h, Value: 326.19 kWh
Timestamp (Local): 2017-07-05 09h, Value: 283.09 kWh
Timestamp (Local): 2017-07-05 10h, Value: 325.61 kWh
Timestamp (Local): 2017-07-05 11h, Value: 319.55 kWh
Timestamp (Local): 2017-07-05 12h, Value: 308.70 kWh
Timestamp (Local): 2017-07-05 13h, Value: 241.24 kWh
Timestamp (Local): 2017-07-05 14h, Value: 291.32 kWh
Timestamp (Local): 2017-07-05 15h, Value: 300.86 kWh
Timestamp (Local): 2017-07-05 16h, Value: 314.04 kWh
Timestamp (Local): 2017-07-05 17h, Value: 297.21 kWh
Timestamp (Local): 2017-07-05 18h, Value: 343.67 kWh
Timestamp (Local): 2017-07-05 19h, Value: 281.79 kWh
Timestamp (Local): 2017-07-05 20h, Value: 273.63 kWh
Timestamp (Local): 2017-07-05 21h, Value: 289.00 kWh
Timestamp (Local): 2017-07-05 22h, Value: 282.71 kWh
Timestamp (Local): 2017-07-05 23h, Value: 310.37 kWh

Exercise 3

Create a method to retrieve the archived event at the given timestamp for the Energy Usage attributes for all meters. The method should use the AFAttributeList object. Print the following information for each meter: Meter name, Timestamp (Local time), Value in kilowatt hour.

void PrintEnergyUsageAtTime(AFDatabase database, string timeStamp)

When the method is run, it should produce the following output:

Print Energy Usage at Time: t+10h
Meter: Meter001, Timestamp (Local): 2017-07-06 10h, Value: 240.79 kWh
Meter: Meter002, Timestamp (Local): 2017-07-06 10h, Value: 320.99 kWh
Meter: Meter003, Timestamp (Local): 2017-07-06 10h, Value: 146.76 kWh
Meter: Meter004, Timestamp (Local): 2017-07-06 10h, Value: 247.14 kWh
Meter: Meter005, Timestamp (Local): 2017-07-06 10h, Value: 300.47 kWh
Meter: Meter006, Timestamp (Local): 2017-07-06 10h, Value: 343.78 kWh
Meter: Meter007, Timestamp (Local): 2017-07-06 10h, Value: 300.69 kWh
Meter: Meter008, Timestamp (Local): 2017-07-06 10h, Value: 368.51 kWh
Meter: Meter009, Timestamp (Local): 2017-07-06 10h, Value: 255.79 kWh
Meter: Meter010, Timestamp (Local): 2017-07-06 10h, Value: 387.83 kWh
Meter: Meter011, Timestamp (Local): 2017-07-06 10h, Value: 254.49 kWh
Meter: Meter012, Timestamp (Local): 2017-07-06 10h, Value: 391.02 kWh

Exercise 4

Create a method that retrieves the daily averages of the Energy Usage attribute for all meters. The startTime and endTime should be in PI Time syntax. The method should use the AFAttributeList object. Print the following information for each meter: Meter name, Timestamp (Local time), Avg. Value in kilowatt hours.

void PrintDailyAverageEnergyUsage(AFDatabase database, string startTime,
string endTime)

When the method is run, it should produce the following output:

Print Daily Energy Usage - Start: t-7d, End: t
Averages for Meter: Meter001
Timestamp (Local): 2017-06-29, Avg. Value: 292.41 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 296.29 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 288.61 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 289.56 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 305.59 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 300.72 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 297.14 kWh

Averages for Meter: Meter002
Timestamp (Local): 2017-06-29, Avg. Value: 290.59 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 301.80 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 313.02 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 296.27 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 296.36 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 294.80 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 290.91 kWh

Averages for Meter: Meter003
Timestamp (Local): 2017-06-29, Avg. Value: 306.56 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 296.34 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 297.13 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 303.96 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 296.18 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 296.94 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 294.30 kWh

Averages for Meter: Meter004
Timestamp (Local): 2017-06-29, Avg. Value: 290.80 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 303.45 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 300.44 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 295.50 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 300.97 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 302.34 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 290.79 kWh

Averages for Meter: Meter005
Timestamp (Local): 2017-06-29, Avg. Value: 295.32 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 296.99 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 306.82 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 305.22 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 304.02 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 301.04 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 306.42 kWh

Averages for Meter: Meter006
Timestamp (Local): 2017-06-29, Avg. Value: 304.98 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 300.31 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 314.78 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 300.96 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 314.68 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 304.36 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 292.20 kWh

Averages for Meter: Meter007
Timestamp (Local): 2017-06-29, Avg. Value: 299.20 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 300.51 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 296.11 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 311.83 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 307.76 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 300.82 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 303.01 kWh

Averages for Meter: Meter008
Timestamp (Local): 2017-06-29, Avg. Value: 298.25 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 304.60 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 294.96 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 308.05 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 299.51 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 295.44 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 304.02 kWh

Averages for Meter: Meter009
Timestamp (Local): 2017-06-29, Avg. Value: 291.10 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 300.96 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 293.08 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 297.69 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 297.84 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 298.78 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 309.21 kWh

Averages for Meter: Meter010
Timestamp (Local): 2017-06-29, Avg. Value: 311.84 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 295.10 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 296.29 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 298.10 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 298.69 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 298.91 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 299.91 kWh

Averages for Meter: Meter011
Timestamp (Local): 2017-06-29, Avg. Value: 297.88 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 303.83 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 299.66 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 303.63 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 297.02 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 299.82 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 306.44 kWh

Averages for Meter: Meter012
Timestamp (Local): 2017-06-29, Avg. Value: 291.31 kWh
Timestamp (Local): 2017-06-30, Avg. Value: 301.20 kWh
Timestamp (Local): 2017-07-01, Avg. Value: 299.30 kWh
Timestamp (Local): 2017-07-02, Avg. Value: 295.43 kWh
Timestamp (Local): 2017-07-03, Avg. Value: 299.08 kWh
Timestamp (Local): 2017-07-04, Avg. Value: 306.22 kWh
Timestamp (Local): 2017-07-05, Avg. Value: 300.89 kWh

Exercise 5

Occasionally, the Energy Usage meter data is incorrect. The values for one meter actually belong to another meter and vice versa. Therefore, implement the method from Exercise 4 which retrieves the names of two meters and start and end times in PI Time syntax. The method should swap the Energy Usage attribute values between the two meters within the given time range. Try to implement this with only two UpdateValues calls. Some questions to consider include the following:

  • What are some tradeoffs when limiting the number of remote calls in this case?

  • How would you ensure there is no data loss?

    void SwapValues(AFDatabase database, string meter1, string meter2,
    string startTime, string endTime)

When the method is run, it should produce the following output:

Swap values for meters: Meter001, Meter002 between y and y+1h

In This Topic
TitleResults for “How to create a CRG?”Also Available in