Example Using a Historian Python Module

Problem Definition

In this example we want to convert a temperature value from Fahrenheit to Celsius.

Expression

To solve this, we create the following Python expression:
uom.to_Celsius(Thermocouple.value, uom.Temperature.Fahrenheit)
Python DatatypeNameDescription
Expression InputsSingleFloatThermocoupleRepresents the temperature value in degrees Fahrenheit
Expression ResultSingleFloat Not Applicable Represents the temperature value in degrees Celsius

Python Modules to Import for this Expression

uom module. This Python module is shipped with Historian.

(Only modules contained on the list of supported modules are available to this expression.)

Constructing the JSON

Using the created expression, we construct the following JSON:
{
 "imports":["uom"],
 "script":"uom.to_Celsius(Thermocouple.value, uom.Temperature.Fahrenheit)",
 "parameters":[
  {   
  "name":" Thermocouple",
  "source":{"address":"Simulation00001", "dataType":"SingleFloat"}
  }
 ]
}

Note that the address parameter is stipulated in the context of the chosen collector supporting Python Expression Tags. In this example, we have used the Simulation Collector. Your collector might use a different source address.

We then transform the above into a minified JSON string:
{"imports":["uom"],"script":"uom.to_Celsius(Thermocouple.value,uom.Temperature.Fahrenheit)","parameters":
[{"name":"Thermocouple","source":{"address":"Simulation00001","dataType":"SingleFloat"}}]}

Adding the Expression Tag to Historian

For this example, we choose to add a Python Expression tag to the Historian using the File Collector to import a CSV file. (We could also have added the tag via the Historian Excel Add-In.)

The file contents look like this:
[Tag]
Tagname,CollectorName,CalcType,SourceAddress,DataType,DescriptionConvertedTempTag,SimulationCollector,PythonExpr,
"{""imports"":[""uom""],""script"":""uom.to_Celsius(Thermocouple.value,uom.Temperature.Fahrenheit)"",
""parameters"":[{""name"":""Thermocouple"",""source"":{""address"":""Simulation00001"",""dataType"":
""SingleFloat""}}]}",
SingleFloat,Python Expression Tag example
Note the following: The CalcType header is included and set to PythonExpr.
Note the following:
  • The CalcType header is included and is set to PythonExpr.
  • The SourceAddress contains the JSON configuration.

  • The CollectorName is set to the name of the chosen collector, which must be on the list of collectors supporting Python Expression Tags. Your collector might be called by a different name.
  • The quotation marks within the JSON string are escaped with other quotation marks in the CSV file.

    For more information, see File Collector > CSV File Formats.

We then import the file, following the instructions in the File Collector ebook.