Create a Logic Function

  1. Expand the User Functions menu section and click Functions. Click on the +New Function button and enter the name logic-simple-thermal.
  2. Select Python 3 as the template type and select the FMU Logic Function example ("A helper function that calls another FMU-type function and processes its response") from the drop-down menu.
    This will pre-fill the required libraries and code.
  3. Make sure to correctly set the following variables in the Parameters section:
    • API_HOST
    • SPACE
    • THING_UID

    All other variables can be left as default, providing the names used in this tutorial were kept (such as the name of the FMU simulation function).

    Figure 1.


    The FMU simulation function keeps state, including the stop time. In this tutorial you will use two methods to run the simulation, that is, invoke the function:

    1. When telemetry comes in for the input (inputHeatFlow property of the Thing)
    2. On a cron schedule
  4. In the MQTT Triggers section add the same topic that you used to create the MQTT Trigger.
  5. In the Cron Expression field enter */5 * * * * to have the logic function execute every 5 minutes.
  6. Click Save to start building the Function.
    Figure 2.


    The logic Function consists of several functions/methods that are called from the main callback handler. The following steps are performed:

    • Set a flag that indicates if the function was triggered by telemetry
    • Retrieve the Access Control client credentials from the variable
    • Initialize the `stop time` and `last run` using the property status of the Thing
    • Retrieve the Properties history since the last run time for the input variable as stored on the Thing
    • Create data payload matrix based on the input Property history and determine the new stop time
    • Create the invoke payload for the FMU simulation
    • Invoke the FMU simulation and retrieve the data from the response
    • Process the data and store the output as Properties history on the Thing

    The code in this logic function is an example of a flow to interact with an FMU. The simulation by an FMU can be configured by changing the variables in the request body. There are many options. These have not been addressed in this tutorial.