Design Pattern: Conditional Sequence Trigger (CST)
Problem:
You want to execute a sequence of actions only when a complex combination of conditions is met, e.g., multiple sensor states, time of day, and presence detection.
Solution:
-
Define a trigger item or a group that represents all relevant conditions.
-
Use a rule to check whether all conditions are satisfied simultaneously.
-
If conditions are met, execute the defined sequence of actions.
-
If any condition changes and the combination is no longer satisfied, reset the sequence.
1. Rule DSL Example
Items
Group gCST "Conditional Sequence Trigger"
Switch motionSensor "Motion Sensor" (gCST)
Switch presenceSensor "Presence Sensor" (gCST)
DateTime currentTime "Current Time" (gCST)
Switch light "Light"
Rule
rule "Conditional Sequence Trigger"
when
Member of gCST changed
then
// Check if all conditions are satisfied
if(motionSensor.state == ON && presenceSensor.state == ON && now.getHourOfDay >= 18 && now.getHourOfDay <= 22) {
// Execute the action sequence
logInfo("CST", "All conditions met – starting sequence")
light.sendCommand(ON)
// Additional actions can follow here
// ...
} else {
logInfo("CST", "Conditions not met – resetting sequence")
light.sendCommand(OFF)
// Optionally cancel intermediate steps
}
end
Explanation:
-
Member of gCST changedtriggers the rule whenever any item in the group changes. -
Conditions can be extended to include more sensors, states, or time windows.
-
The
elsebranch resets actions if the combination is no longer met.
2. JSR223 Python (Jython) Example
Items
from core.rules import rule
from core.triggers import when
from core.log import logging
log = logging.getLogger("org.openhab.rules.CST")
motionSensor = ir.getItem("motionSensor")
presenceSensor = ir.getItem("presenceSensor")
light = ir.getItem("light")
Rule
@rule("Conditional Sequence Trigger")
@when("Member of gCST changed")
def conditional_sequence_trigger(event):
hour = now.getHourOfDay()
if motionSensor.state == ON and presenceSensor.state == ON and 18 <= hour <= 22:
log.info("All conditions met – starting sequence")
light.sendCommand(ON)
# Additional actions here, e.g., start timers, control more devices
else:
log.info("Conditions not met – resetting sequence")
light.sendCommand(OFF)
# Optionally cancel intermediate steps
Explanation (Python/Jython):
-
now.getHourOfDay()retrieves the current hour. -
Additional sensors or complex conditions can easily be added using
andoperators. -
The action sequence can include lights, blinds, notifications, timers, etc.
Advantages of CST
-
Clean handling of complex, interdependent conditions.
-
Easy to extend by adding more sensors or conditions.
-
Flexible reset mechanism if conditions are no longer met.
-
Works in both Rule DSL and Python/Jython environments.


