Processing of data values

Most protocols and systems use well-defined data types for data point values. Most of them are simple one likes numbers (signed, unsigned) or boolean values (on/off). In such cases, the incoming values can simply me mapped to native data types that are supported by our server.

However, there are use case where data point values are provided by more complex types. A typical examples are systems that provide JSON or XML objects that encode the required values. In order to use data from these complex objects, a pre-processing for incoming or post-processing of outgoing data is necessary. 

Within this article, it is assumed that an MQTT broker is providing the current weather information via MQTT topic. However, instead of providing weather values like wind speed or current outside temperature as native data, it provides a JSON object as string value. If the MQTT topic is configured correctly (for more information about MQTT see the following article), the JSON object will be shown within the Item Tree as follows:

The full example of this JSON object looks as follows:

{
  "current":
  {
    "outside_temperature" : 21.5,
    "wind_speed" : 10
  }
}

To extra data from this JSON string, the logic module can be used. To get the outside temperature, the following parsing definition has to be added:

  • Item suffix: Outside
    This means that the item ID of the virtual item that will be created by the Logic module will have the suffix Outside. This suffix will be appended to the original item ID followed by a dot. In our example the item ID will be NETx\XIO\Mqtt\MyBroker\weather\current.Outside
  • Base path or Item ID: empty
    If multiple items shall be recalculated, a base path for start searching for items can be specified here. If only 1 single item is selected, this parameter is ignored.
  • Selector: NETx\XIO\Mqtt\MyBroker\weather\current
    Here the source item is specified. If multiple items shall be recalculated, the selector for pattern matching must be specified. 
  • Data type: float
    Here the data type of the created virtual item is defined. In our case we will take a real since the temperature can be a decimal.
  • In processing type: JSON
    Here the type of the incoming data must be specified. In our example, JSON has to be chosen.
  • In processing resource: $.current.outside_temperature
    Depending on the incoming data type, the processing function has to be defined. For JSON, a valid JPATH expression has to be specified here – for XML, an XPATH expression has to be used. In our example, we are using the JPATH $.current.outside_temperature. $ selects the root element, .current selects the child named current and .outside_temperature the child for .current named outside_temperature. More information about JPATH can be found here.

After having saved the definition, the following item will appear within the item tree:

The pre- and post-processing can also be combined with calculation functions. To show the value in Fahrenheit, you can use the following definition:

The result of this definition is a virtual item that shows the extracted temperature in degrees Celsius.Â