To execute a command automatically, set the template of the command and the condition in the trigger. See the below examples.
Registering One-Time Schedule Triggers
We will show how to register a trigger to turn on an air conditioner (with the preset temperature and fan speed set to 25 degrees and 5, respectively) 1 hour from now.
This is what is happening in the sample code:
Create actions of the command to be executed by the trigger as actions. Here, we are setting three actions. How the command is set is the same as that in the code of Executing Commands.
Create a TriggerCommandObject with the schema information and actions as the command to be executed by the trigger.
Set the execution time in a ScheduleOncePredicate as the execution condition.
Create a PostCommandTriggerRequest with the command and execution condition and register it with the postCommandTrigger method as the trigger.
The function set in then of the promise will be called when Thing Interaction Framework accepts the trigger.
Registering Recurring Schedule Triggers
We will show how to register a trigger to turn on an air conditioner (with the preset temperature and fan speed set to 25 degrees and 5, respectively) at 9:00 a.m. every day.
The basic steps are the same as the case of a one-time schedule trigger. The only difference is how the execution condition is initialized.
Set the execution time in a SchedulePredicate as the execution condition. See Recurring Schedule to learn how to set the time.
Registering State Condition Triggers
The next example shows how to register a trigger with a simple condition. We will show how to register a trigger to turn on an air conditioner (with the preset temperature and fan speed set to 25 degrees and 5, respectively) when "the room temperature goes over 30 degrees".
The basic steps are the same as the case of schedule triggers. The only difference is how the execution condition is initialized.
Create a trigger condition. First, set the comparison condition in a Condition. In this example, we are creating the condition that will become true when currentTemperature defined in State is greater than or equal to 30 degrees.
Create a StatePredicate to finalize the trigger condition. In this example, we are specifying CONDITION_FALSE_TO_TRUE because we want to execute the command when the room temperature goes up from below 30 degrees to above 30 degrees.
Setting a Trigger with Multiple Conditions
The next example shows how to concatenate conditions with AND. In this example, we will set a trigger to set the fan speed to 10 when "the power is on" and "the room humidity is above 80".
The basic steps are the same as the case of a single condition trigger. The only difference is how a Condition is initialized. In this example, we first create two conditions "power is equal to true" and "currentHumidity is greater than 80", concatenate them with an And, and then set the concatenated condition as the comparison condition.
Registering Advanced Triggers
You can register advanced triggers as below by specifying detailed information.
A trigger which sends a command to a different thing
You can register a trigger that sends a command to a different thing when a state condition is met on the thing to which you register the trigger.
For example, if you manage a thermometer and an air conditioner as separate things, you can register a trigger to "turn on the air conditioner when the thermometer rises to a certain temperature".
A trigger with details of the trigger and the command in it
You can set the title, description, and metadata of the trigger and the command in it.
The following processes are executed in the code:
Specify the command to be executed when the condition is met as a TriggerCommandObject. Create this instance from the information below.
Specify the thing to which the trigger sends the command in target. This sample code assumes that the thermometer and the air conditioner are managed with the ThingIFAPI instances, api and api2, respectively. When the state of api (thermometer) meets the trigger condition, the trigger sends a command which turns on the air conditioner to target obtained from api2 (air conditioner). You need to have instantiated and onboarded api and api2 with the same owner.
Specify actions to be executed by the command in actions.
Specify the command details in commandTitle, commandDescription, and commandMetadata. You can freely use these fields according to your mobile app specification as explained in Command Details. See Command Details also for limits such as the maximum length of each field.
Create the execution condition as a StatePredicate.
Specify the above TriggerCommandObject, StatePredicate, and trigger details in a PostCommandTriggerRequest. You can set the trigger details for your mobile app as you can for commands. The same limits such as the maximum length apply to triggers and commands.
Finally, register the new trigger by calling the postCommandTrigger method with the created PostCommandTriggerRequest.