Enable anomaly detection on sensors across assets
Create a computation model (Amazon CLI)
To create a computation model, use the Amazon Command Line Interface (Amazon CLI). After you define the computation model, train the model and schedule inference to do anomaly detection across assets in Amazon IoT SiteWise.
The following steps explain this process:
-
To set up anomaly detection, use the UpdateAssetModel (Amazon CLI), and meet the following requirements:
-
At least one input property that is of either
DOUBLE
orINTEGER
data type. It is either a measurement or transform property, and is used to train the model. -
A result property of
STRING
data type. It must be a measurement property, and stores the anomaly detection results.
-
-
Create a file
anomaly-detection-computation-model-payload.json
with the following content:Note
Create a computation model by directly providing
assetProperty
as the data source.{ "computationModelName": "name of ComputationModel", "computationModelConfiguration": { "anomalyDetection": { "inputProperties": "${properties}", "resultProperty": "${p3}" } }, "computationModelDataBinding": { "properties": { "list": [ { "assetProperty": { "assetId": "asset-id", "propertyId": "input-property-id-1" } }, { "assetProperty": { "assetId": "asset-id", "propertyId": "input-property-id-2" } } ] }, "p3": { "assetProperty": { "assetId": "asset-id", "propertyId": "results-property-id" } } } }
-
Run the following command to create a computation model:
aws iotsitewise create-computation-model \ --cli-input-json file://
anomaly-detection-computation-model-payload.json
ExecuteAction API payload preparation
The next steps to execute training and inference is performed with the
ExecuteAction
API. Both training and inference are configured with a JSON action payload configuration.
When invoking the ExecuteAction
API, the action payload must be provided as a value with a stringValue
payload.
The payload must strictly adhere to the API requirements. Specifically, the value must be a flat string with no control characters (for example, newlines, tabs, or carriage returns). The following options provides two reliable ways to supply a valid action-payload.
Option 1: Use a clean payload file
The following procedure describes the steps for a clean payload file:
-
Clean the file to remove control characters.
tr -d '\n\r\t' < original-action-payload.json > training-or-inference-action-payload.json
-
Execute the action with the file
@=file://...
.aws iotsitewise execute-action \ --target-resource computationModelId=<
MODEL_ID
> \ --action-definition-id <ACTION_DEFINITION_ID
> \ --action-payload stringValue@=file://training-or-inference-action-payload.json
Option 2: Inline string with escaped quotes
The following steps describes the steps to supply the payload inline, and avoid intermediary files:
-
Use escaped double quotes (
\"
) inside the JSON string. -
Wrap the entire
StringValue=..
expression within double quotes.
Example of an escaped action payload:
aws iotsitewise execute-action \ --target-resource computationModelId=<
MODEL_ID
> \ --action-definition-id <ACTION_DEFINITION_ID
> \ --action-payload "stringValue={\"exportDataStartTime\":1717225200,\"exportDataEndTime\":1722789360,\"targetSamplingRate\":\"PT1M\"}"
Train the Amazon CLI
-
Run the following command to find the
actionDefinitionId
of theAmazon/ANOMALY_DETECTION_TRAINING
action. Replacecomputation-model-id
with the ID returned in the previous step.aws iotsitewise describe-computation-model \ --computation-model-id
computation-model-id
-
Create a file called
anomaly-detection-training-payload.json
and add the following values:Note
The payload must conform to Option 1: Use a clean payload file.
-
StartTime
with the start of the training data, provided in epoch seconds. -
EndTime
with the end of the training data, provided in epoch seconds. -
You can optionally configure Advanced inference configurations.
-
(Optional)
TargetSamplingRate
with the sampling rate of the data. -
(Optional)
LabelInputConfiguration
to specify time periods when anomalous behavior occurred for improved model training. -
(Optional)
ModelEvaluationConfiguration
to evaluate model performance by running inference on a specified time range after training completes.
-
{ "exportDataStartTime": StartTime, "exportDataEndTime": EndTime }
Example of a training payload example:
{ "exportDataStartTime": 1717225200, "exportDataEndTime": 1722789360 }
-
-
Run the following command to start training (without providing asset as a target resource). Replace the following parameters in the command:
aws iotsitewise execute-action \ --target-resource computationModelId=
computation-model-id
\ --action-definition-idtraining-action-definition-id
\ --action-payload stringValue@=file://anomaly-detection-training-payload.json
-
Run the following command to check for status of the model training process. The latest execution summary shows the execution status (
RUNNING
/COMPLETED
/FAILED
).aws iotsitewise list-executions \ --target-resource-type COMPUTATION_MODEL \ --target-resource-id
computation-model-id
-
Run the following command to check the configuration of the latest trained model. This command produces an output only if at least one model has completed training successfully.
aws iotsitewise describe-computation-model-execution-summary \ --computation-model-id computation-model-id
Start and stop inference (Amazon CLI)
After training the model, start the inference, which instructs Amazon IoT SiteWise to begin monitoring your industrial assets for anomalies.
Start inference
-
Run the following command to find the
actionDefinitionId
of theAmazon/ANOMALY_DETECTION_INFERENCE
action. Replacecomputation-model-id
with the actual ID of computation model created earlier.aws iotsitewise describe-computation-model \ --computation-model-id
computation-model-id
-
Create a file
anomaly-detection-start-inference-payload.json
and add the following code. Replace the following parameters as described:Note
The payload must conform to Option 1: Use a clean payload file.
-
DataUploadFrequency
: Configure the frequency at which the inference schedule runs to perform anomaly detection. Allowed values are:PT5M, PT10M, PT15M, PT30M, PT1H, PT2H..PT12H, PT1D
."inferenceMode": "START", "dataUploadFrequency": "
DataUploadFrequency
" -
(Optional)
DataDelayOffsetInMinutes
with the delay offset in minutes. Set this value between 0 and 60 minutes. -
(Optional)
TargetModelVersion
with the model version to activate. -
(Optional) Configure the
weeklyOperatingWindow
with a shift configuration. -
You can optionally configure Advanced inference configurations.
-
-
Run the following command to start inference. Replace the following parameters in the payload file.
-
computation-model-id
with the ID of the target computation model. -
inference-action-definition-id
with the ID of theAmazon/ANOMALY_DETECTION_INFERENCE
action from Step 1.
aws iotsitewise execute-action \ --target-resource computationModelId=
computation-model-id
\ --action-definition-idinference-action-definition-id
\ --action-payload stringValue@=file://anomaly-detection-inference-payload.json
-
-
Run the following command to check if inference is still running. The
inferenceTimerActive
field is set toTRUE
when inference is active.aws iotsitewise describe-computation-model-execution-summary \ --computation-model-id
computation-model-id
-
The following command lists all the inference executions:
aws iotsitewise list-executions \ --target-resource-type COMPUTATION_MODEL \ --target-resource-id
computation-model-id
-
Run the following command to describe an individual execution. Replace
execution-id
with the id from previous Step 5.aws iotsitewise describe-execution \ --execution-id
execution-id
Stop inference
-
Run the following command to find the
actionDefinitionId
of theAmazon/ANOMALY_DETECTION_INFERENCE
action. Replacecomputation-model-id
with the actual ID of computation model created earlier.aws iotsitewise describe-computation-model \ --computation-model-id
computation-model-id
-
Create a file
anomaly-detection-stop-inference-payload.json
and add the following code.{ "inferenceMode": "STOP" }
Note
The payload must conform to Option 1: Use a clean payload file.
-
Run the following command to stop inference. Replace the following parameter in the payload file:
-
computation-model-id
with the ID of the target computation model. -
inference-action-definition-id
with the ID of theAmazon/ANOMALY_DETECTION_INFERENCE
action from Step 1.
Example of the stop inference command:
aws iotsitewise execute-action \ --target-resource computationModelId=
computation-model-id
\ --action-definition-idinference-action-definition-id
\ --action-payload stringValue@=file://anomaly-detection-stop-inference-payload.json
-