KickAss Scripts and Programs
Timers have multiple uses. One interesting way to use them is as flags. If you want a flag to be true for only a short time, you can use a timer instead. Set the timer to 30 seconds and then have your rule SC test for whether the timer is running. When the timer expires, nothing else need happen, but subsequent evaluations of the Rules that use its status as an SC will fail because it returns false when stopped. Timers can also be modified while running.
The traditional use for a timer is something like this. Turning on a device, starting a timer for, say 30 minutes, and then writing a rule to turn the device off when the timer expires, will ensure the device is turned off. The timer can also be tested to determine that the device is on. If the timer is running, it’s presumed on. Detecting motion from a motion sensor near the device can reset the timer to the maximum value, ensuring it stays on as long as it is being used.
Other HAL Program Elements, such as counters, and variables, can also be used in Programs. They are less useful and we will not dwell on them at this time.
HAL Programs vs. HAL Scripts
When we think of a program, we generally think of a text file that contains a list of logic statements that are executed as a whole. We think of programs as being written in a computer language, such as Perl, or any of countless other computer languages that have come and gone over the years. That text file is then compiled, or perhaps simply interpreted at runtime. The resulting action follows the logic steps carefully laid out in the text file. Comments in the text file serve to clarify any subtleties and obtuse actions.
HAL Programs are not programs in this sense. The “language” is the language of HAL Rules and Macros. Hal Programs are then merely a collection of related Rules and Macros. Listing and documenting HAL programs is difficult because HAL makes no provision to group related Rules and Macros together. No text file, not even something as simple as a folder to place related Rules and Macros together in. There is no provision to insert comments into the HAL Database either. So the connections between disparate Rules and Macros are often not obvious and understanding the logic flow and interactions between rules is difficult. We are left to our own ingenuity to devise ways to organize them into a coherent program.
HAL Programs may be organized using two simple tools. (1) Keywords help keep related program elements visually together and (2) Parallel Documentation organizes program flow and comments.
These tools are merely cosmetic, but they do a lot to help organize the thought processes
and keep bugs away from your program code. How much benefit you get from them is directly proportional to how carefully you use them.
While the use of rule and macro keywords helps keep related rules and macros together in the Home Automation Setup Wizard, the real benefit comes from keeping parallel documentation of the logic as you create it. You can create flow charts, add comments to the flow, and even provide significant prose describing the program functions. The example program for managing a bathroom light and motion sensor is an extreme example of parallel documentation.
|