4 Replies Latest reply on Aug 20, 2015 1:13 PM by Kenji Hashimoto

    Creation of PI Tags using afsdk in AF Server



      I have some 50 tags which needs to be created dynamically from afsdk, and each one of them will have different uom and data type. Help me in finding a solution where in i decide with uom type and value type based on the text.



      Dehumidification Pump Frequency Feedback- uom is percent

      Regeneration Tank Outlet Water Temp- uom is temperature

      Regeneration Pump Start/Stop-  value type is boolean


      what might be the best solution for this?


      All these are dynamic creation.

        • Re: Creation of PI Tags using afsdk in AF Server
          Kenji Hashimoto

          Following help explains UOM.

          UOM Class


          I use C# and following code works in my machine.

          using OSIsoft.AF.UnitsOfMeasure;
          UOMDatabase myUOMDB = myAFServer.UOMDatabase;
          UOMClass RatioUOMClass = myUOMDB.UOMClasses["Ratio"];
          UOM percentUOM = RatioUOMClass.UOMs["percent"];
          myAttribute.DefaultUOM = percentUOM;

          In this example, I set default uom to atteribute but you can use Element Template as well. (If there are a lot of elements with the same attributes, then it is good to use Element Template)

          I abbreviated myAFServer and myAttribute in my source code but I hope you can find how to use UOM.


          For using the other UOMClass, you can refer to PI System Explorer > Unit of Measure

          It shows UOM class and UOM name.

            • Re: Creation of PI Tags using afsdk in AF Server

              Thank you for your quick response.

              i got this solution but i need it for many attributes dynamically ex: how will i decide with the uom with a string.

              Currently i am using


              if (Regex.IsMatch(<attribute_name>,  @"([temperature][temp])", RegexOptions.IgnoreCase))


                   // setting the uom as degree celcius;



              Is there any other alternative for this??

                • Re: Creation of PI Tags using afsdk in AF Server
                  Marcos Vainer Loeff

                  Hi Aruna,


                  You can create a method to add an attribute or attribute template to an element or element template as shown below:


                       private void CreateAttributeTemplate(AFElementTemplate myCityElement, string AttributeName, Type myType, UOM myUOM, AFCategory AttributeCategory)
                              AFAttributeTemplate myAttribute = myCityElement.AttributeTemplates[AttributeName];
                              if (myAttribute == null)
                                  myAttribute = myCityElement.AttributeTemplates.Add(AttributeName);
                              myAttribute.Type = myType;
                              myAttribute.DefaultUOM = myUOM;
                              if (myAttribute.Categories[AttributeCategory.Name] == null)


                  Then you can easily create attribute templates on your element template, for instance:


                              UOM degree = myPISystem.UOMDatabase.UOMClasses["Plane Angle"].UOMs["degree"];
                              UOM degreeC = myPISystem.UOMDatabase.UOMClasses["Temperature"].UOMs["degree Celsius"];
                              UOM speedKmph = myPISystem.UOMDatabase.UOMClasses["Speed"].UOMs["kilometer per hour"];
                              UOM speedMps = myPISystem.UOMDatabase.UOMClasses["Speed"].UOMs["meter per second"];
                              UOM millimeter = myPISystem.UOMDatabase.UOMClasses["Length"].UOMs["millimeter"];
                              UOM millibar = myPISystem.UOMDatabase.UOMClasses["Pressure"].UOMs["milibar"];
                              UOM percent = myPISystem.UOMDatabase.UOMClasses["Ratio"].UOMs["percent"];
                              UOM kilometres = myPISystem.UOMDatabase.UOMClasses["Length"].UOMs["kilometer"];
                              UOM kWh = myPISystem.UOMDatabase.UOMClasses["Energy"].UOMs["kilowatt hour"];
                              UOM kW = myPISystem.UOMDatabase.UOMClasses["Power"].UOMs["kilowatt"];
                              CreateAttributeTemplate(CityElementTemplate, "Cloud Cover", typeof(int), percent, WeatherHist);
                              CreateAttributeTemplate(CityElementTemplate, "Feels Like", typeof(int), degreeC, WeatherHist);
                              CreateAttributeTemplate(CityElementTemplate, "Humidity", typeof(double), percent, WeatherHist);


                  Note that the inputs of this method are: element template, attribute name, attribute type, attribute UOM and attribute category. I am sure that there are other ways to do it but hopefully this is what you are looking for.

                  • Re: Creation of PI Tags using afsdk in AF Server
                    Kenji Hashimoto

                    There is String.Indexof method.

                    String.IndexOf Method (System)

                    It returns number if the string matches.


                    For performance, The link says IndexOf method is faster than Regex.IsMatch function.

                    Performance Test – String.Contains vs String.IndexOf vs Regex.IsMatch | theburningmonk.com

                    Though of cause if you don't face any performance issues using Regex.IsMatch function, then it is no problem to use it.