Example Using a Historian Python Module
Problem Definition
In this example we want to convert a temperature value from Fahrenheit to Celsius.
Expression
uom.to_Celsius(Thermocouple.value, uom.Temperature.Fahrenheit)
Python Datatype | Name | Description | |
---|---|---|---|
Expression Inputs | SingleFloat | Thermocouple | Represents the temperature value in degrees Fahrenheit |
Expression Result | SingleFloat | 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
{
"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.
{"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.)
[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.
- The
CalcType
header is included and is set toPythonExpr
. -
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.