Example using Array/Table Lookup
Problem Definition
In this example we want to translate a string representing order of magnitude into a corresponding numerical value using array/table lookup.
This example will be explained by means of a hypothetical collector called PlantSensorCollector
that is a Python Expression enabled collector. The collector collects a source tag with address TemperatureSetpoint
of type VariableString
, having values 'Low', 'Medium', and 'High'
.
Expression
{'Low':100, 'Medium':400, 'High':800}.get(Setpoint.value, 0)
Python Datatype | Name | Description | |
---|---|---|---|
Expression Inputs | VariableString | Setpoint | Represents the given ordinal string value we wish to transform. |
Expression Result | SingleFloat | Not Applicable | Represents the numerical value corresponding to the ordinal string input. |
Python Modules to Import for this Expression
None
(Only modules contained on the list of supported modules are available to this expression.)
Constructing the JSON
{
"script":"{'Low':100,'Medium':400,'High':800}.get(Setpoint.value, 0)",
"parameters":[
{
"name":" Setpoint",
"source":{"address":"TemperatureSetpoint", "dataType":"VariableString"}
}
]
}
{"script":"{'Low':100,'Medium':400,'High':800}.get(Setpoint.value,0)","parameters":
[{"name":"Setpoint","source":{"address":"TemperatureSetpoint","dataType":"VariableString"}}]}
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,DescriptionNumericalTagDerivedFromOrdinalVal,PlantSensorCollector,PythonExpr,
"{""script"":""{'Low':100,'Medium':400,'High':800}.get(Setpoint.value,0)"",""parameters"":
[{""name"":""Setpoint"",""source"":{""address"":""TemperatureSetpoint"",""dataType"":""VariableString""}}]}",
VariableString, Python Expression Tag example
- The
CalcType
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 specified in File Collector.