Tutorial: Adding a constant

In this tutorial, you are adding a constant and using it in a batch data metric. Streaming data metrics do not use constants.

Before you begin

  1. Install Python 3.9.x in your environment.
  2. Install IoT Functions in your local environment. Follow the instructions in the IoT Functions readme.
    Alternatively, to install IoT Functions and its dependencies, you can follow the steps in the Before you begin section of Tutorial: Adding a custom function. Clone the HelloWorld starter package and import the starter package into PyCharm. Install the requirements from PyCharm.
  3. Complete Tutorial: Adding expressions to add a distance metric to the robot device type in Maximo Monitor.

About this task

Note: To complete this tutorial, you use a sample device type template and data. Sample device types do not use the latest version of device types and do not support streaming data metrics. For more information, see Getting started for users.

Adjust distance to account for a delay.

Steps

Complete these steps to apply a calculation that references a constant value to sample data.

Step 1: Define your calculation

Define 'distance_adjusted_by_constant' as follows:

df['distance_adjusted_by_constant'] = df['distance']*delay

Step 2: Save your credentials to a file

Set credentials to connect to Maximo Monitor.

  1. Download the credentials_as.json file.
  2. Replace the variables with your data and then save the file to your local machine.
Note: Take care not to save the credentials file to any external repository.

Step 3: Create global constant

Run the POST constant API with the below payload

[
    {
        "name": "ArrayConstant",
        "enabled": true,
        "value": {
            "value": [
                "A",
            ]
        },
        "metadata": {
            "jsonSchema": {
                "$schema": "http://json-schema.org/draft-07/schema#",
                "type": "array",
                "minItems": 1,
                "items": {
                    "type": "string"
                }
            },
            "dataType": "ARRAY",
            "values": [
                "A",
                "B",
                "C"
            ],
            "description": "Sample multi-valued array",
            "dataTypeForArray": [
                "LITERAL"
            ],
            "type": "CONSTANT",
            "required": true,
            "tags": []
        },
        "resourceId": null,
        "resourceUUID": null,
        "isBuiltin": false
    },
    {
        "name": "alpha",
        "enabled": true,
        "value": {
            "value": 4
        },
        "metadata": {
            "dataType": "NUMBER",
            "values": null,
            "description": "general purpose numeric constant",
            "type": "CONSTANT",
            "required": true,
            "tags": []
        },
        "resourceId": null,
        "resourceUUID": null,
        "isBuiltin": false
    },
    {
        "name": "delay",
        "enabled": true,
        "value": {
            "value": 120
        },
        "metadata": {
            "dataType": "NUMBER",
            "values": null,
            "description": "Adjustment to output due to delay",
            "type": "CONSTANT",
            "required": true,
            "tags": []
        },
        "resourceId": null,
        "resourceUUID": null,
        "isBuiltin": false
    },
    {
        "name": "text",
        "enabled": true,
        "value": {
            "value": "ABC"
        },
        "metadata": {
            "dataType": "LITERAL",
            "values": null,
            "description": "This is string text",
            "type": "CONSTANT",
            "required": true,
            "tags": []
        },
        "resourceId": null,
        "resourceUUID": null,
        "isBuiltin": false
    },
    {
        "name": "json",
        "enabled": true,
        "value": {
            "value": {
                "A": "B",
                "B": "A",
                "C": {
                    "A": "B",
                    "B": "A"
                }
            }
        },
        "metadata": {
            "dataType": "JSON",
            "values": null,
            "description": "This is JSON text",
            "type": "CONSTANT",
            "required": true,
            "tags": []
        },
        "resourceId": null,
        "resourceUUID": null,
        "isBuiltin": false
    },
    {
        "name": "Gamma",
        "enabled": true,
        "value": {
            "value": 5
        },
        "metadata": {
            "dataType": "NUMBER",
            "values": null,
            "description": "general purpose numeric constant",
            "type": "CONSTANT",
            "required": true,
            "tags": []
        },
        "resourceId": null,
        "resourceUUID": null,
        "isBuiltin": false
    },
]

Step 4: Set the value of the constant from the UI

  1. On the Setup page, on the Devices tab, select a device type and click Set up device type.
  2. Click the Configure device type icon and then select Manage constraints.
  3. For the delay constraint, click the Edit icon.
  4. Assign a value of 2 and click Save.

Step 5: Reference the constant in an expression

  1. From the Data tab, click Create metric
  2. Select the PythonExpression function from the catalog.
  3. Set the scope and then click Next.
  4. Enter or paste in the following expression:
        df['distance']*c['delay']
  5. Click Next.
  6. Specify distance_adjusted_by_factor as an output.
  7. Click Create
  8. View the new distance_adjusted_by_factor metric data.

Wait up to 5 minutes for Maximo Monitor to evaluate the expression against the sample data.

Steps to update, revert and delete constant values

Complete these steps to update, revert values for constants or delete a constant

Update global default value

  1. On the Setup page, on the Devices tab, select a device type and click on it.
  2. Click the Configure device type icon and then select Manage constraints.
  3. On the Constants dialog box, hover over the constant, select more actions and then choose Change global default value
  4. Assign a value and click Save.

Update resource level constants

  1. On the Setup page, on the Devices tab, select a device type and click on it.
  2. Click the Configure device type icon and then select Manage constraints.
  3. On the Constants dialog box, hover over the constant, select Edit
  4. Assign a value and click Save.

Revert the resource level constant to global default value

  1. On the Setup page, on the Devices tab, select a device type and click on it.
  2. Click the Configure device type icon and then select Manage constraints.
  3. On the Constants dialog box, hover over the constant, select Edit
  4. Choose the option Revert to the global default value.
  5. Chose Revert to confirm your choice.

Optional Step : Unregister the constant

Write a unregister_constant.py script and run it. Un-registering the constant impacts any calculation that uses this constant.

  #Import packages and libraries

  import logging
  logging.basicConfig(level=logging.DEBUG)
  logger = logging.getLogger(__name__)
  import pandas as pd
  import json
  #Import these classes from IoT Functions
  from iotfunctions.db import Database
  from iotfunctions.ui import UISingle,UIMulti

  #Connect to the Maximo Monitor
  with open('credentials_as.json', encoding='utf-8') as F:
    credentials = json.loads(F.read())
  db_schema = None
  db = Database(credentials=credentials)

  #Connect to the Maximo Monitor
  db = Database(credentials = credentials)

  #Unregister the constant using the database object
  db.unregister_constants(['delay'])

Optional Step: Delete the resource level or global level constant

  1. On the Setup page, on the Devices tab, select a device type and click on it.
  2. Click the Configure device type icon and then select Manage constraints.
  3. On the Constants dialog box, hover over the constant, select more actions and then choose Delete constant
  4. Chose Remove to confirm your choice.