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
- Install Python 3.9.x in your environment.
- 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. - Complete Tutorial: Adding expressions to
add a
distancemetric 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.
- Download the
credentials_as.jsonfile. - 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
- On the Setup page, on the Devices tab, select a device type and click Set up device type.
- Click the Configure device type icon and then select Manage constraints.
- For the
delayconstraint, click the Edit icon. - Assign a value of 2 and click Save.
Step 5: Reference the constant in an expression
- From the Data tab, click Create metric
- Select the
PythonExpressionfunction from the catalog. - Set the scope and then click Next.
- Enter or paste in the following expression:
df['distance']*c['delay'] - Click Next.
- Specify
distance_adjusted_by_factoras an output. - Click Create
- View the new
distance_adjusted_by_factormetric 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
- On the Setup page, on the Devices tab, select a device type and click on it.
- Click the Configure device type icon and then select Manage constraints.
- On the Constants dialog box, hover over the constant, select more actions and then choose Change global default value
- Assign a value and click Save.
Update resource level constants
- On the Setup page, on the Devices tab, select a device type and click on it.
- Click the Configure device type icon and then select Manage constraints.
- On the Constants dialog box, hover over the constant, select Edit
- Assign a value and click Save.
Revert the resource level constant to global default value
- On the Setup page, on the Devices tab, select a device type and click on it.
- Click the Configure device type icon and then select Manage constraints.
- On the Constants dialog box, hover over the constant, select Edit
- Choose the option Revert to the global default value.
- 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
- On the Setup page, on the Devices tab, select a device type and click on it.
- Click the Configure device type icon and then select Manage constraints.
- On the Constants dialog box, hover over the constant, select more actions and then choose Delete constant
- Chose Remove to confirm your choice.