There are two ways to create a rule via JSON. You can create a rule by template, available in the system or compose it by instructions:
Creating a Rule by Template
To define a JSON that creates a rule by template just pass values for the template variables into configuration and to provide the template UID as templateUID properties in JSON. The following rule is an example for creating a rule by using the provided configuration values and the template mentioned in Template JSON Examples:
[
{
"uid":"OnOffRule",
"name":"On-Off Rule",
"description":"This rule configures OnOffTemplate to perform toggle operation on Functional Item with UID 'demo:fim:BinarySwitchExt:1' when the state of Functional Item with UID 'demo:fim:BinarySwitch:1' is changed",
"templateUID":"OnOffTemplate",
"configuration":{
"triggerFIName":"demo:fim:BinarySwitch:1",
"toggledFiName":"demo:fim:BinarySwitchExt:1"
}
}
]
Turn Off Heating Rule Example
Below is the JSON representation of a Turn off Heating rule, created by template:
[
{
"uid": "rule.from.template.turn.off.heating",
"triggers": [
{
"id": "triggerDailyTimer",
"handlerUID": "trigger.RepeatingTime",
"description": "Triggers every work day at the setted time.",
"bindings": [
{
"id": "seconds",
"type": "CONSTANT",
"source": [
0
]
},
{
"id": "hours",
"type": "CONSTANT",
"source": [
8
]
},
{
"id": "daysOfWeek",
"type": "CONSTANT",
"source": [
"Monday",
"Wednesday",
"Friday",
"Sunday"
]
},
{
"id": "minutes",
"type": "CONSTANT",
"source": [
0
]
},
{
"id": "frequency",
"type": "CONSTANT",
"source": "daily"
}
]
}
],
"flow": [
{
"id": "actionTurnOffHeating",
"handlerUID": "action.ItemInvokeOperation",
"description": "Turns off the heating.",
"bindings": [
{
"id": "name",
"type": "CONSTANT",
"source": "Heating Switch"
},
{
"id": "operationName",
"type": "CONSTANT",
"source": "off"
}
]
}
]
}
]
Creating a Rule by Instructions
To define a JSON that creates a rule by set of instructions just include the instructions that you need in the correct properties of the rule JSON definition and define their input bindings if there are any:
{
"uid":"TutorialRule",
"name":"Tutorial Rule",
"description":"Example of how to define a rule by set of instructions.",
"triggers":[
{
"id":"RuleTrigger",
"type":"ConsoleTrigger",
"bindings": [
{
"id": "eventTopic",
"source":"tutorial/topic",
"type":"CONSTANT"
},
{
"id":"keyName",
"source":"value",
"type":"CONSTANT"
}
]
}
],
"flow":[
{
"id":"RuleCondition",
"type":"CompareCondition",
"bindings": [
{
"id": "operator",
"source":"=",
"type":"CONSTANT"
},
{
"id":"constraint",
"source":10,
"type":"CONSTANT"
},
{
"id":"inputValue",
"source":"RuleTrigger/outputValue",
"type":"INSTRUCTION"
}
]
},
{
"id":"RuleAction",
"type":"ConsolePrintAction",
"bindings":[
{
"id":"inputValue",
"source":"RuleTrigger/outputValue",
"type":"INSTRUCTION"
}
]
}
]
}