Filter results by

Use machine learning in a Rule

This article outlines the high-level steps to programmatically write a Rule that utilizes machine learning on ARTIK cloud services. It also walks through an example Rule.

Machine learning trains ARTIK cloud services to predict or interpret your device's data usage. Rules can be written that test the results of machine learning, just as they would normally test data messages sent by devices.

You can also use the web UI at My ARTIK Cloud to write a Rule that uses machine learning.

Before reading this How To, please review the following article, which describes the principles of developing Rules.

A Rule triggers Actions on devices based on incoming data messages. We assume you have already connected a device type that can send data and a device type that can receive Actions. Prototype and deploy your device explains how to develop these devices.

Use cases

There are two types of conditions that use machine learning.

  • A prediction condition tests predicted values for the specified field of the source device, instead of the actual values of incoming data messages.
  • An anomaly detection condition tests the results of detecting anomalies in the incoming field values or in the patterns of incoming data messages.

You might use a prediction condition in the following scenarios:

  • Alert a caretaker when a medical device predicts that its wearer will experience a medical emergency in the next 30 minutes
  • Turn on the air filter when the room's pollution level is predicted to exceed a given threshold in the next 2 hours
  • Turn on the hallway lights each morning when a family member is predicted to wake up

You might use an anomaly detection condition in the following scenarios:

  • Receive a text alert when a smart lock demonstrates unusual behavior
  • Log a series of timestamps that show when a prototype device's sensors are not recording expected values

Step 1: Choose a source device and field

Call the API to create a Rule. The API requires that you pass a Rule name and Rule body.

The Rule body is a JSON payload that contains an "if" object and a "then" object.

The Rule condition is defined in the "if" object. When writing a Rule condition, first specify the following.

Step 2: Apply machine learning to the Rule

Now add a "transformer" object to the "if" object. The transformer specifies one of the two condition types, and includes required parameters specific to each type.

A prediction condition must include a value for predictIn, a time in seconds from now that the value will be predicted.

"Now" is the time when the Rule is evaluated by ARTIK cloud services. This can be when a new message is received for the device, or when the Rule has been scheduled to run.

An anomaly detection condition must include a value for anomalyDetectionSensitivity, which specifies how many anomalies should be detected by ARTIK cloud services. This ranges from 0 (very few) to 100 (many).

See examples in Apply machine learning.

Step 3: Define how the condition is evaluated

A Rule condition is completed with an operator and operand.

  • The operator tests whether or not the condition is true. Possible operators depend on the field type.
  • The operand is the value compared by the operator to the predicted value or anomaly detection result, as specified in Step 2.

Now your condition is complete. See some example Rule conditions, including prediction and anomaly detection conditions.

Step 4: Define an Action to send

If the Rule condition is met, the Rule will send an Action to a destination device. These are added in the "then" object of the Rule body.

Step 5: Test your Rule

It takes some time to train the machine learning model. If ARTIK cloud services has not yet learned your device's data usage, the APIs will return a warning or error instead of the Rule body. See Invalid Rules for an example.

In the meantime, call the API to test the Actions you included in your Rule. Note that this will actually send the Actions to the destination devices!

An example Rule

Here is a simple example that demonstrates how machine learning works in a Rule. For simplicity, we use the ARTIK cloud services web tools to create a device type and Rule.

In the Developer Dashboard, create a new device type called Window.

Add a field to the Window device type called Status, and assign it the possible values Open and Closed. This field will be evaluated in our Rule.

How to use machine learning in a Rule

Connect a device of the Window device type to your account.

Create a Rule that tests for anomalies in the behavior of Window. In the UI, this Rule is set to detect unexpected values from the Status field.

How to use machine learning in a Rule

When the Rule detects an unexpected value, it sends an email to you.

How to use machine learning in a Rule

When the Rule is created, it will note that machine learning is currently in progress. Let's train the machine learning model by sending device values from our Window.

How to use machine learning in a Rule

Use the Online Device Simulator to send Open at 10 am and Closed at 5 pm on Days 1-4.

How to use machine learning in a Rule

On Day 5, use the Online Device Simulator to send an Open value at 9 am.

The Rule should detect an anomaly in this behavior, and send you an email that the window is open.

That's it!

We have applied machine learning to Rules just by adding a single step to standard Rule creation. If you are interested in doing more with machine learning, see the full APIs.