My adaption of this rule unfortunately has some issues, mainly
- it sometimes works and sometimes doesn’t
- It switches lights off after 23.00h that are already off.
The first problem probably has something to do with my var setup and using both rules in one file. Unfortunately I am more of a customiser than a developer, so a solution is beyond my skills. Can anyone please help me?
var Timer tRandomLights1 = null
var Timer tRandomLights2 = null
// -————————————————————————————
// RandomLights On between 7-22 when nobody at home
// -————————————————————————————
rule "Randomly turn on off lights 7-22"
when
Time cron "42 3/20 7-22 * * ?"
then
if ((JemandZuHause.state == OFF) && (Lock_EG_Danalock.state != OFF) && (Dark.state == ON)) {
// Only turn a light on/off ocasionally
if ((new java.util.Random).nextInt(2) == 1) {
// Create a timer with a random value
var int randomTime = (new java.util.Random).nextInt(600)
logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
tRandomLights1 = createTimer(now.plusSeconds(randomTime)) [|
var randLightIndex = (new java.util.Random).nextInt(gRand.members.size)
var randLightStateCurrent = gRand.members.get(randLightIndex).state
var randLightStateNew = if (randLightStateCurrent == ON) OFF else ON
logInfo("org.openhab","Switching light " + gRand.members.get(randLightIndex).name + " from " + randLightStateCurrent + " to " + randLightStateNew)
sendCommand(gRand.members.get(randLightIndex), randLightStateNew)
]
}
}
end
// -————————————————————————————
// All Lights randomly Off after 23.00 when nobody at home
// -————————————————————————————
rule "Randomly turn on off lights after 23"
when
Time cron "42 4/6 23 * * ?"
then
if ((JemandZuHause.state != ON) && (Lock_EG_Danalock.state != OFF) && (Dark.state == ON)) {
// Only turn a light off ocasionally
if ((new java.util.Random).nextInt(2) == 1) {
// Create a timer with a random value
var int randomTime = (new java.util.Random).nextInt(240)
logInfo("org.openhab","Setting random lights timer to " + randomTime + " seconds.")
tRandomLights2 = createTimer(now.plusSeconds(randomTime)) [|
var randLightIndex = (new java.util.Random).nextInt(gLights.members.size)
var randLightStateCurrent = gLights.members.get(randLightIndex).state
var randLightStateNew = if (randLightStateCurrent == ON) OFF else OFF
logInfo("org.openhab","Switching light " + gLights.members.get(randLightIndex).name + " from " + randLightStateCurrent + " to " + randLightStateNew)
sendCommand(gLights.members.get(randLightIndex), randLightStateNew)
]
}
}
end