|
Post by papa on Apr 26, 2019 17:18:06 GMT
OpenHAB Timer Rules, IntroductionIn openHAB rules, we can use timers to start & stop devices or processes. We can approach timers in various ways depending on what we want to accomplish & avoid. In this thread, I will give at least timer rule examples with at least three approaches. The rules in this thread assume some device has been programmed to be Node05 (RFM69 or ESP8266). They also assume that an Actuator that is connected to an LED or relay is controlled by Node05's Channel16 (an on/off switch channel). They further assume that an openHAB item has been created that links to Node05's Channel16. Let's assume the item is Act_Node05 For more on configuring the above, see posts starting here. ^^ link updated, August 29, 2021 Next, First Example of Timer Rules
|
|
|
Post by papa on Apr 27, 2019 12:00:05 GMT
First Example of Timer Rules
An openHAB rule must have a "when" clause at its beginning which defines when the rule runs. The when clause may be conditional on a state of an openHAB item or a time cron expression. The two rules in this post each "fire" (execute) every day & once per day
These rules will turn the item Act_Node05 on & off daily at specific times. The rule-given state of Act_Node05 can be overridden by a push button on the node or by a virtual switch on a user interface or by another rule.
rule "Timer for Output ON at 9:11pm" // (Node05) when // sec min hr dom mon dow yr dom= day of month, mon=month, dow=day of week, yr is optional Time cron "0 11 21 * * ?" // uses 24 hour clock so hour of 9pm = 21 then sendCommand(Act_Node05, ON) end
rule "Timer for Output OFF at 9:27pm" // (Node05) when Time cron "0 27 21 * * ?" then sendCommand(Act_Node05, OFF) end
Next, Second Example of Timer Rules
|
|
|
Post by papa on Apr 28, 2019 16:22:14 GMT
Second Example of Timer RulesThese rules will turn Act_Node05 on & off daily at semi-random times around a seed moment set in the Time cron expression. The rule-given state of Act_Node05 can be overridden by a local push button or a virtual switch on a user interface or by another rule. rule "Random timer for Output ON around 9:16pm" // (Node05) when // sec min hr dom mon dow yr dom= day of month, mon=month, dow=day of week, yr is optional Time cron "0 11 21 * * ?" // rule triggered at 9:11pm (21:11) then // Schedule a random ON event up to 15 minutes from the "now" set by time cron above createTimer(now.plusMinutes((new java.util.Random).nextInt(15)) ) [| sendCommand(Act_Node05, ON) ] end rule "Random timer for Output OFF around 9:32pm" // (Node05) when Time cron "0 27 21 * * ?" // rule triggered at 9:27pm (21:27) then // Schedule a random OFF event up to 15 minutes from now createTimer(now.plusMinutes((new java.util.Random).nextInt(15)) ) [| sendCommand(Act_Node05, OFF) ] end
Third example of Time Rules
|
|
|
Post by papa on Apr 29, 2019 14:22:53 GMT
Third Example of Time Rules
This rule will turn Act_Node05 on & off at specific times defined by early variables. The rule-given state of Act_Node05 can only be temporarily overridden by a local pushbutton or a virtual switch on a user interface or by another rule. Within 5 minutes Act_Node05's state will revert to what this rule assigns. The time window defined by this rule may be contained by one 24 hour day or may cross midnight into another day.
The rule's "when clause" causes the rule to fire every 5 minutes. Why 5 minutes? I wanted to avoid more frequent firing & yet not allow Act_Node05 to have the wrong state for longer than 5 minutes.
Besides the time cron expression in the when clause, this rule uses computer system time gained from the now.get commands.
// The following rule should: // keep the Node05 device ON within 5 minutes of the window boundaries you defined by the global variables below // keep Node05 OFF outside of the window boundaries // For the rule to create an OFF time window & otherwise be ON, reverse ON & OFF in the sendCommand() lines below. // Even if the device is manually toggled (by UI or button or ??), it should return to programmed state within 5 minutes
var Number startHour = 20 // edit for start of ON window, must be 24 hour format. 8pm = 20, var Number startMin = 35 // 8:35pm, make evenly divisible by 5 var Number endHour = 6 // edit for end of ON window, must be 24 hour format. 6am = 6 var Number endMin = 30 // 6:30am, make evenly divisible by 5 // ^^ Global variables (persist after openHAB loads this rule file)
rule "Node05 always-on during defined window" when // sec min hr dom mon dow yr dom= day of month, mon=month, dow=day of week, yr is optional Time cron "0 0/5 * ? * *" // rule fires every 5 minutes, starting at the zeroth minute of an hour then var dayOfyr = now.getDayOfYear // not used, shown as possibility var day = now.getDayOfWeek.getValue // not used, shown as possibility << updated August 29, 2021 var minute = now.getMinuteOfHour var state05 = Act_Node05.state var hour = now.getHourOfDay var hour2 = hour if ( endHour < startHour ) { // Compensate if defined window will cross Midnight. if ( hour < startHour ) hour2 = hour + 24 if ( hour == startHour && minute < startMin ) hour2 = hour + 24 }
// endHour2 as is or + 24 will mark the window's final hour var endHour2 = endHour // if window will cross Midnight, make endHour2 greater than startHour if ( endHour < startHour ) endHour2 = endHour + 24
var Number nowTime = (hour2 * 60) + minute // for comparison, convert representation of present time to minutes & var Number startTime = (startHour * 60) + startMin // convert start time to minutes & var Number endTime = (endHour2 * 60) + endMin // convert representation of end time to minutes
logInfo("time", " Node05 endHour2 = " + endHour2 + " day = " + day + " dayOfyr = " + dayOfyr) // UNcomment these lines for debugging output logInfo("time", " Node05 state = " + state05 + " nowTime = " + nowTime)
logInfo("time", " startTime = " + startTime + " endTime = " + endTime)
// Is nowTime (in minutes) between startTime (in minutes) & endTime (in minutes)? If Yes, turn ON Actor if ( nowTime >= startTime && nowTime < endTime ) { Act_Node05.sendCommand(ON) } else { Act_Node05.sendCommand(OFF) } // if No, turn OFF Actor end
|
|