How to define a new rule and retrieve an existing one.
A rule allows you to start a non-scene command and start, stop, pause, resume, or quickrun a scene command.
Defining a Rule for a Non-Scene Command
To create a new rule for a non-scene command:
The following code snippets (Java and JavaScript) demonstrate how to create and save a rule that links a LogCommand with ID "LogCommand1" and a condition with ID "EventCondition1":
HomeAutomationAdmin hamAdmin = (HomeAutomationAdmin) service;
//create a rule
Rule myrule = hamAdmin.createRule();
//configure its properties(in this case command and condition)
myrule.setCommand(LogCommand.TYPE, "LogCommand1");
myrule.setCondition(EventCondition.TYPE, "EventCondition1");
myrule.save();
String ruleID = myrule.getId();
//configure the properties of the rule
//(in this case the command and condition)
ruleprops = new Object()
ruleprops.command_type="LogCommand"
ruleprops.command_id="LogCommand1"
ruleprops.condition_type="EventCondition"
ruleprops.condition_id="EventCondition1"
//create the rule
var xhr = new XMLHttpRequest();
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/createRule", "params":'+
JSON.stringify(ruleprops)+', "id": 1}')
myruleID=JSON.parse(xhr.responseText)
Defining a Rule for a Scene Command
To create a new rule for a scene command:
The following code snippets (Java and JavaScript) demonstrate how to create and save a rule that starts a SceneCommand with ID "SceneCommand1" upon triggering a condition with ID "EventCondition1":
HomeAutomationAdmin hamAdmin = (HomeAutomationAdmin) service;
//create a rule
Rule myrule = hamAdmin.createRule();
//configure the properties of the rule(in this case a scene command,
//a condition and a property specifying start scene action)
myrule.setCommand(SceneCommand.TYPE, "SceneCommand1");
myrule.setCondition(EventCondition.TYPE, "EventCondition1");
Hashtable ruleProps = new Hashtable ();
ruleProps.put(Scene.SCENE_ACTION, Scene.SCENE_ACTION_START);
myrule.setProperties(ruleProps);
myrule.save();
String ruleID = myrule.getId();
//configure the properties of the rule
//(in this case the command and condition)
ruleprops = new Object()
ruleprops.command_type="SceneCommand"
ruleprops.command_id="SceneCommand1"
ruleprops.condition_type="EventCondition"
ruleprops.condition_id="EventCondition1"
ruleprops.scene_action="start"
//create the rule
var xhr = new XMLHttpRequest();
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/createRule", "params":'+
JSON.stringify(ruleprops)+', "id": 1}')
myruleID=JSON.parse(xhr.responseText)
Editing a Rule
To edit and existing rule:
The following code examples (Java and JavaScript) demonstrate how to retrieve the rule, created in the previous example and change its command with another command from the same type.
//Retrieve the rule
Rule myrule1 = hamAdmin.getRule(ruleID);
//change a property
myrule.setCommandId("LogCommand2");
//apply the changes
myrule.save();
//retrieve the rule
var xhr = new XMLHttpRequest();
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/getRule", "params": ["'+
myruleID+'" ], "id": 1}')
myrule=JSON.parse(xhr.responseText).result
//change the ID of the command
myrule.command_id = "LogCommand2"
//apply the changes
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/updateRule", "params": ["'+
myruleID+'", '+JSON.stringify(myrule)+' ], "id": 1}')