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

To solve this, we create the following Python expression:
{'Low':100, 'Medium':400, 'High':800}.get(Setpoint.value, 0)
Python DatatypeNameDescription
Expression InputsVariableStringSetpointRepresents the given ordinal string value we wish to transform.
Expression ResultSingleFloat 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

Using the created expression, we construct the following JSON:
{
 "script":"{'Low':100,'Medium':400,'High':800}.get(Setpoint.value, 0)",
 "parameters":[
  {   
  "name":" Setpoint",
  "source":{"address":"TemperatureSetpoint", "dataType":"VariableString"}
  }
 ]
}
We then transform the above into a minified JSON string:
{"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.)

The file contents look like this:
[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
Note the following:
  • The CalcType 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 specified in File Collector.