There are a couple options for a rule system with the Things Gateway from Mozilla. First, there is a rule system built into the Web GUI, accessed via the Rules option in the drop down menu. Second, there is the Web Things API that allows programs external to the Things Gateway to automate the devices that make up a smart home. Most people will gravitate to the former built-in system, as it is the most accessible to those without predilection to writing software. This blog post is going to focus on the rules system native to the Things Gateway.
The Rule System is an example of a graphical programming system. Icons representing physical devices in the Smart Home are dragged onto a special display area and attached together to form a rule. Rules are composed of two parts: a predicate and an action.
The predicates are logical conditions like "the bedroom light is on" or "somebody pushed the button marked ' do not press '". These logical conditions can be chained together using operations like "AND" and "OR": "somebody is in the room AND the television is on".
The actions consist of telling a set of devices to take on specific states. These actions can be as simple as "turn the heater on" or "turn the light on and set the color to red".
Throughout the history of graphical user interfaces, there have been many attempts to create graphical, drag and drop, programming environments. Unfortunately, most fail when the programming goal rises above a certain threshold of complexity. From the perspective of a programmer, that threshold is depressingly low. As such, the Things Gateway Rules System doesn't try to exceed that threshold and is suitable only for simple rules with a restricted compound predicate and a set of actions. Other more complex programming constructs such as loops, variables, and functions are intentionally omitted.
If a desired predicate/action is more complex than the Rules System GUI can do, there is always the option of falling back to the Web Thing API and any programming language that can speak HTTP and/or Web Sockets. Some of my previous blog posts use that Web Thing API: see the Tide Light or Bonding Lights Together for examples.
Let's start with a simple example: we've got four Philips HUE light bulbs. We'll create a rule that designates bulb #1 as the leader and bulbs #2, #3, and #4 as followers.
We start by navigating to the rules page ( ≡ ⇒ Rules ) and making a new rule by pressing the "+" button on the rules page. Then drag and drop the first bulb to left side of the screen. This is where the predicates will live. Then select the property "ON". Notice that on the top of the screen in the red area, a sentence is forming. " If Philips HUE 01 is on, ??? ". This is an English translation of the selections that you've made to create your rule. As you create your rule, use this sentence as a sanity check to make sure that your rule does what you want it to do.
Next, drag each of the three other lights on the right half of the screen and select their "ON" properties.
Finally, give your rule a name. I'm choosing " 01 leads, others follow ". Make sure you hit "Enter" after typing the name.
Now click the " ← " to return to the rules page. Then return to the "Things" page ( ≡ ⇒ Things ). Turn on "Philips HUE 01" by clicking on the bulb. All four of the bulbs will light up.
Now click on the "Philips HUE 01" bulb again to turn the light off and watch what happens.
The other lights stayed on. If you've used older versions of the Things Gateway rules, this will surprise you. With the latest release (0.5.x), there are now two types of rules: "If" rules and "While" rules. The "If" rules are just a single shot - if the predicate is true, do the action once. There is no automatic undo when the predicate no longer is true.
"While" rules, on the other hand, will automatically undo the action when the predicate is no longer true. This can be best understood by reading the rule sentence out loud and imagine giving it as a command to a servant. " If light 01 is on, turn on the other lights " implies doing a single thing with no follow up. A "While" rule, though, implies sticking around to undo the action when the predicate is no longer true. Say it out loud and the difference becomes clear immediately. Paraphrasing: " While light 01 is on, turn on the other lights ". The word "While" implies passing time.
The Things Gateway rules system can do both kinds of rules. Let's go back and make our rule into a "While" rule. Return to the Rules page ( ≡ ⇒ Rules ) and move your mouse over the rule then press the "Edit Rule" Button.
Take a close look at the sentence at the top of the screen. The symbol under the word "If" is an indication of a word selection drop down menu . Click on the word "If" and you'll see that you can change the word "If" to "While". Do it.
Exit from the rule and go back to Things page. Turn all the lights off. Then turn on the leader light, in my case, "Philips HUE 01". All the lights turn on. Turn off the leader, and the action is undone: the rest of the lights go off.
Here's a video demonstrating the difference in behavior between the "If" and "While" forms of the rules.
Earlier I stated, the Things Gateway Rule System doesn't try to exceed the complexity threshold where visual programming paradigms start to fail. However, the system as it stands right now is not without some troubles.
Consider a rule that uses the clock in the predicate. That could result in a rule that reads like this: " If the time of day is 10:00, turn Philips HUE 01 on ". The interpretation of this is straight forward.
However, what if you change the "If" to "While"? " While the time of day is 10:00, turn Philips HUE 01 on ." Since the resolution of the clock is only to the minute, the clock considers it to be 10:00 for sixty seconds. The light stays on for one minute. It is not particularly useful to use the clock with the "While" form of a rule. The Rule System needs some sort of timer object so a duration can be set on a rule.
How would you make a rule to turn a light on at dusk and then turn it off at 11PM? Currently, the clock does not support the concepts of dawn and dusk, so that rule just can't be done within the Things Gateway. However, with some programming, it would be possible to add a NightThing that could accomplish the task.
In many of these blog posts, I predict what I'm going to talk about in the next posting. I've got a completely rotten record of actually following through with my predictions. However, I hope in my next posting to write about how to implement the rules above using the Web Things API and the Python language.