Example Using a Built-In Python Function

Problem Definition

In this example, we want to calculate the maximum of two temperature values to be collected.

Expression

To solve this, we create the following Python expression:
max(ThermocoupleA.value, ThermocoupleB.value)
Python DatatypeNameDescription
Expression InputsSingleFloatThermocoupleARepresents a temperature value.
SingleFloatThermocoupleBRepresents a temperature value.
Expression ResultSingleFloat Not Applicable Represents the maximum of the two given temperature values

Python Modules to Import for this Expression

None. A built-in Python module from the Python Standard Library is used.

(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":"max(ThermocoupleA.value,ThermocoupleB.value)",
 "parameters":[
  {   
  "name":"ThermocoupleA",
  "source":{"address":"Simulation00001","dataType":"SingleFloat"}
  },
  {   
  "name":"ThermocoupleB",
  "source":{"address":"Simulation00002","dataType":"SingleFloat"}
  }
 ]
}

Note that the address parameter is stipulated in the context of the chosen collector, which must be on the list of collectors 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:
{"script":"max(ThermocoupleA.value,ThermocoupleB.value)","parameters":[{"name":"ThermocoupleA","source":
{"address":"Simulation00001","dataType":"SingleFloat"}},{"name":"ThermocoupleB","source":
{"address":"Simulation00002","dataType":"SingleFloat"}}]}

Adding the Expression Tag to Historian

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

Ensure the following for your Python Expression Tag:
  • 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.
Otherwise, the tag is added in exactly the same way as for a regular tag.