Filter results by

Your first anomaly detection Rule

This interactive web sample demonstrates how smart lock anomalies can be detected using the Machine Learning API for Anomaly Detection. The sample assists in prepopulating training data to a sample device and walks through setting up a Rule that uses anomaly detection.

The sample code is available on GitHub.


You should know how to perform the following tasks on ARTIK cloud services:

Also see How to use machine learning in a Rule.

We will use Python to run the built-in web container and run the sample locally on http://localhost:8000.

Initial setup

Create a new device type for this sample, using the Developer Dashboard. To facilitate setting up the Manifest, upload the included Manifest file (virtual-lock-manifest.json) after creating.

Connect a device to your account that uses the above device type. Click on the device name to retrieve the device ID and generate a device token.

Run Sample

We will use the built-in web server in Python.

The following command will start startup a local server running on port 8000.

%> python -m SimpleHTTPServer 8000 Serving HTTP on port 8000 ...

Then navigate to http://localhost:8000 using your favorite web browser.

The tutorial is self-contained and contains instructions. Below is an overview of the steps.


These steps refer to the sample web application.

Set up lock device

Go to the Setup a lock device section, and enter the device ID and device Token, and click "Save".

Feed training data

Click on the "Generate data" button. This will feed 30 days of past locked and unlocked events to the device, conforming to the time points specified in the sample.

Machine learning models are continuously trained on a 30-day sliding window of historical data. At this time, a model cannot be trained on a fixed time period. The training uses one message field and one timestamp (ts) value.

Set up anomaly detection Rule

Go to the Rules dashboard to set up a Rule. Detailed instructions are provided in the web application.

We are creating an anomaly detection Rule. We set the confidence level of the Rule to "High" to minimize the chance of receiving a false alert.

Simulate sending a new Lock or Unlock event

In the Send regular data and cause an anomaly section of the web application, click on the Lock or Unlock button to simulate sending new events at different times. If the Rule detects an anomaly based on the trained past data, it will trigger the Rule you created in the previous step (fulfilling the anomaly detection condition of the Rule).

Anomaly detection with machine learning

Anomaly detection works only on incoming data. This page includes the above timetable of locked, unlocked, and unknown states for the device. These states result from the locked and unlocked events that the sample has generated at its predetermined time points.

The machine learning model resamples the last sent data point until a new (opposing) data point is received. For example, if the model receives a "locked" message at 9:15 am, it considers the device to be "locked" until an "unlocked" message is received at 6:00 pm.

During "unknown" (indeterminate) states, a Rule with a high confidence level will not consider a locked or unlocked event as an anomaly.