This is an ARTIK cloud services sample that creates a machine learning Rule with a prediction condition.
This sample will demonstrate the following:
- Making an API call to the Rules service endpoint:
/rules
- Payload required to create a Rule
Prerequisites
This was run using the following:
- node version >= 9.8.0
- npm version >= 5.8.1
Check your version by typing:
%> node --version
v9.8.0
%> npm --version
v5.8.1
- Create a new device type for this sample (or skip this step if you wish to use an existing device already in your account). If you wish to create a new device type: upload the
sample-manifest-smart-lock.json
file to create your Manifest. - Add a device (of above device type) into your account (or use an existing device already in your account). Browse your devices and obtain the device ID for the device.
- Log into your application to obtain your user token for the application. If you have not yet created an application, instructions for our OAuth2 Authorization flow is here. Using the user token, you can obtain your user ID by calling the
/users/self
API call.
- Open the sample Node.js script
create-prediction-rule.js
and fill in the information:
var your_user_id = 'your_user_id_here';
var your_user_token = 'your_user_token_here';
var your_device_id = 'your_device_id_here';
- This sample will monitor the
state
field from your device. If you wish to monitor a different field, replace "state" with another field name you want to monitor:
rule_body.rule.if = {
"field": "state",
//...
Additionally, we set the Rule transformer to a "prediction" type condition. (See the possible types of machine learning conditions.) The below Rule triggers if the predicted value you defined is "true" in one hour.
rule_body.rule.if = {
"field": "state",
"operator": "=",
"operand": {"value": true},
"transformer": {
"type": "prediction",
"parameters": {
"predictIn": 3600
}
//...
-
In this sample, if the predicted value matches the condition you specified, this will trigger the
setOn
Action on the device. If you wish to trigger a different Action on the device, replace the "setOn" Action with a different value. -
We will also make this Rule accessible only by the application associated with your user token:
"rule": {
"scope": "thisApplication", //rule is visible only to this application
"description": "sample prediction rule"
//....
}
- Finally install any dependencies by typing:
%> npm install
###1. Run sample
Run the script via command line:
%> node create-prediction-rule.js
Here is the successful response data:
{
"data": {
"uid": "cccc111222333ffffffffffffffffff",
"id": "c043a0142e5e4975aa5488d59d6a6861",
"aid": "aaaa111222333ffffffffffffffffff",
"name": "Sample Prediction Rule",
"languageVersion": 1,
"rule": {
"if": {
"sdid": "95c2fb05044749b7bf1966be7e0c6237",
"field": "state",
"operator": "=",
"operand": {
"value": true
},
"transformer": {
"type": "prediction",
"parameters": {
"predictIn": 3600
}
}
},
"then": [
{
"ddid": "95c2fb05044749b7bf1966be7e0c6237",
"action": "setOn",
"parameters": {}
}
]
},
"enabled": true,
"index": 10,
"createdOn": 1524702558737,
"modifiedOn": 1524702558737,
"isTestable": true,
"scope": "thisApplication",
"description": "Sample Prediction Rule",
"invalidatedOn": 1524702558795,
"error": {}
}
}
The Rule is now active and will trigger the Action specified if the prediction value matches the value you specified in the operand.
You can verify the Rule is added to your account by visiting the Rules dashboard. Check ("show private rules …") to view any additional Rules that are only accessible by the application.
This sample only demontrates creating the Rule in your account.
Test the prediction by sending a message to the device. If the prediction condition is matched, the Rule will trigger the Action you specified.
Here are a few options to send a message:
- Send message to device using the Device Simulator (https://my.artik.cloud/devices -> Show Simulator)
- Send message to device using API Console (https://developer.artik.cloud/api-console)
- Send message to device using any of our other Code Samples
-
The Rule engine allows a maximum of 10 machine learning models per application per user.
"errors": [{ "messages": "10 models can be created per app per user. Contact business team if you need more." //...
-
The device must have at least 3 data points for training. A maximum of the last 30 days of data is used for continuous training.
If you are not familiar with ARTIK cloud services, we have extensive documentation at https://developer.artik.cloud/documentation
The full ARTIK cloud services API specification can be found at https://developer.artik.cloud/documentation/api-reference/
Check out advanced sample applications at https://developer.artik.cloud/documentation/tutorials/code-samples/
To create and manage your services and devices on ARTIK cloud services, visit the Developer Dashboard at https://developer.artik.cloud
Licensed under the Apache License. See LICENSE.
Copyright (c) 2018 Samsung Electronics Co., Ltd.