Hello!
Here’s the way I’ve been using OpenHAB for anti-burglar lights:
-
First, I have two items for the sunrise (General_Sunrise_State) and the sunset (General_Sunset_State), and two global timer variables:
var Timer GeneralAntiBurglarLightsTimer
var Timer GeneralAntiBurglarLightsOffTimer
-
Using two rules, I change the state of the General_Night_State:
–
rule "General - Sunrise - State"
when
Item General_Sunrise_State changed to ON
then
sendCommand(General_Night_State, OFF)
end
–
rule "General - Sunset - State"
when
Item General_Sunset_State changed to ON
then
sendCommand(General_Night_State, ON)
end
- Besides that, I have alarm state item (General_Alarm_State) with 4 values:
0) Off
- Home
- Short-term away
- Long-term away (used for anti-burglar lights),
and Alarm_Detection_State (value set to 1, one minute after an item General_Alarm_State received values 2 or 3)
After setting all this up, there are two rules - one for turning the lights on/off at a semi-random interval (when all the conditions are met), and one for turning a random lights timer off (at specific time of a day, with random offset)
First rule, for turning lights on/off (60% chance of turning light on, 40% chance of turning light - semi-random interval between 5 and 60 minutes):
rule "General - Night - State"
when
Item General_Night_State received update
then
if ((General_Night_State.state == ON) && (General_Alarm_State.state == 3) && (General_Alarm_Detection_State.state == 1)) {
var Integer RandomInterval = (Math::floor((Math::random * (60 - 5) + 5).doubleValue).intValue)
GeneralAntiBurglarLightsTimer = createTimer(now.plusMinutes(RandomInterval)) [|
Dimmers?.members.forEach(dimmer|
sendCommand(dimmer, if(Math::random > 0.6) 0 else 100))
postUpdate(General_Night_State, ON)
]
}
if ((General_Night_State.state == OFF) && (General_Alarm_State.state == 3) && (General_Alarm_Detection_State.state == 1)) {
if(GeneralAntiBurglarLightsTimer != null) {
GeneralAntiBurglarLightsTimer.cancel
GeneralAntiBurglarLightsTimer = null
}
sendCommand(Dimmers, 0)
}
end
You can change minimum and maximum interval values in this part of code (5 minutes minimum, 60 minutes maximum):
Math::floor((Math::random * (60 - 5) + 5).doubleValue
Percentage of on/off ratio could be changed here (> 0.6 meaning 60% chance for on, 40% chance for off):
Math::random > 0.6
Second rule uses cron expressions as trigger for turning anti-burglar lights off (with 5-30 minutes random offset):
rule "General - Anti-burglar - Disarm"
when
Time cron "0 0 1 ? * MON-FRI *" or
Time cron "0 0 3 ? * SAT *" or
Time cron "0 30 0 ? * SUN *"
then
if ((General_Alarm_State == 3) && (General_Alarm_Detection_State == 1)) {
if (GeneralAntiBurglarLightsTimer != null) {
GeneralAntiBurglarLightsTimer.cancel
GeneralAntiBurglarLightsTimer = null
}
var Integer RandomInterval = (Math::floor((Math::random * (30 - 5) + 5).doubleValue).intValue)
GeneralAntiBurglarLightsOffTimer = createTimer(now.plusMinutes(RandomInterval)) [|
sendCommand(Dimmers, 0)
]
}
end
You can change offset (set randomly between 5 and 30 minutes) here:
Math::floor((Math::random * (30 - 5) + 5).doubleValue
This is just my way of dealing with this, but maybe someone will find it useful.
Best regards,
Davor