I would love to get feedback from the group whether the ActivityManager I am proposing here does not yet exist and solves the problem of activity management.
@rlkoshak perfectly summarized the design pattern to handle basic motion in Design Pattern: Motion Sensor Timer with the central idea of having an activity item that is ON while there is motion and OFF if there is no motion. This idea extends nicely to a large set of triggers well beyond motion. Rich and many follow up contributions demonstrate how rules are powerful to implement broad functionality.
However, in my own rules implementation I found the code hard to debug as so many variations and timers are involved. I therefore implemented a proof of concept ActivityManager on top of the JSR223 engine that can be tested through comprehensive unit tests. The unit tests allow me to modify the manager quickly and with confidence.
The ActivityManager consistently manages the ON/OFF state of activity items according to the activity item’s configuration. The ON state can be triggered by item state changes such as motion, door contact open or manually (e.g.via a light switch for manual on/auto-off). The OFF state is set based on expired time (optionally reset if re-triggered), a set time of day or a manual trigger. As any item state change can trigger an activity item we can use an activity item that turns off as the trigger for another activity item (e.g. if motion expires in my kitchen I don’t turn off all lights, but instead trigger a second activity item that leaves some standby lights on). All activity items can be overridden manually by other items - optionally for a limited amount of time (e.g. if a user turns off a motion controlled light then motion sensing will be disabled for a certain amount of time). The ActivityManager can create a report of its state (in particular reporting expiration timers and override timers).
With the help of the ActivityManager I can condense complex activity scenarios with diverse underlying triggers into simple and consistent activity items that are straight forward to reason about. In simple cases the activity item itself can represent a physical item such as a light (light is on when the activity item is on and off when the activity item is off). But more often than not you will want a more sophisticated mechanism to manage resulting actions based on time of day, luminance and other things. For clear separation of duty ActivityManager does NOT offer those services. Instead use any complementary solution such as Area Triggers and Actions, which groups activity items, leverages Mode (Time of Day) and supports actions defined by mode. Note that ActivityManager does optionally support trigger configuration by mode (time of day), further aligning with “Area Triggers and Actions”. Additionally ActivityManager’s support for manual overrides will eliminate the need for using a dedicated intensity level such as 99 to override automation in “Area Triggers and Actions”. Finally note that “Area Triggers and Actions” offers basic timer support for delayed actions. These timers operate on the actions, but not on the underlying items and thus can’t participate in the underlying activity items and groups. If this functionality is used to enhance activity items then ActivityManager offers a cleaner solution so there is a slight overlap of functionality.
Obviously these ideas are not new and I have found various bits and pieces implemented, but I haven’t found a comprehensive implementation of the above functionality. I noticed @rlkoshak’s PR for a TimerManager and checked if I could implement ActivityManager on top of it, but didn’t see a fit as TimerManager deals with adding timers across items.
Would love to hear your feedback. Should I clean up my prototype and add it as a Community Project under the openHAB helper libraries for JSR223?