Idea:
Cyclic switching lights on/off to simulate presence of people in the house to prevent burglary
Main Concept:
Define a reasonable sequence (cycle) of commands for your lights in the house that simulates presence of people. Run this cycle as often as needed during absence of people in the house. A switch switches the cycles on and off.
The following example is a .rules file. It uses a sample of a (for testing seconds-based) sequence for one lights-plug [item ShellyPlug1_Power]. Instead of plusSeconds you could use plusMinutes as well. For switching the cycles on and off, the switch item TimersTest is used.
This setup is running at my openHAB without any problems.
OpenHAB Version 2.5
Feedback welcome.
import java.util.List
import java.util.ArrayList
val List<Timer> TimerList = new ArrayList<Timer>()
// you need to have a Switch item e.g. as follows: Switch TimersTest "Timers Test"
// the Switch item is used to switch the timers sequence ON and OFF
rule "Add Timers to List"
when Item TimersTest received command ON
then
// Set timer events for whatever timeframes and items
// use an ascending order of timers for more clarity
TimerList.add(createTimer(now.plusSeconds(5), [|ShellyPlug1_Power.sendCommand(ON)]))
TimerList.add(createTimer(now.plusSeconds(10), [|ShellyPlug1_Power.sendCommand(OFF)]))
TimerList.add(createTimer(now.plusSeconds(15), [|ShellyPlug1_Power.sendCommand(ON)]))
TimerList.add(createTimer(now.plusSeconds(20), [|ShellyPlug1_Power.sendCommand(OFF)]))
TimerList.add(createTimer(now.plusSeconds(25), [|ShellyPlug1_Power.sendCommand(ON)]))
TimerList.add(createTimer(now.plusSeconds(30), [|ShellyPlug1_Power.sendCommand(OFF)]))
TimerList.add(createTimer(now.plusSeconds(35), [|ShellyPlug1_Power.sendCommand(ON)]))
TimerList.add(createTimer(now.plusSeconds(40), [|ShellyPlug1_Power.sendCommand(OFF)]))
TimerList.add(createTimer(now.plusSeconds(45), [|ShellyPlug1_Power.sendCommand(ON)]))
TimerList.add(createTimer(now.plusSeconds(50), [|ShellyPlug1_Power.sendCommand(OFF)]))
TimerList.add(createTimer(now.plusSeconds(55), [|ShellyPlug1_Power.sendCommand(ON)]))
// the last (last by time) timer should do some cleanup and restart the whole sequence
TimerList.add(createTimer(now.plusSeconds(60), [|
// do cleanup of all timers by cancelling all of them
TimerList.forEach[timer|timer?.cancel]
// do some cleanup of the TimerList by removing all list-entries
TimerList.removeAll()
// do cleanup item states by switching all items to their default (e.g. OFF)
ShellyPlug1_Power.sendCommand(OFF)
// restart the whole sequence
TimersTest.sendCommand(ON)
]))
end
rule "Cancel Timers of List"
when Item TimersTest received command OFF
then
// do cleanup of all timers by cancelling all of them
TimerList.forEach[timer|timer?.cancel]
// do some cleanup of the TimerList by removing all list-entries
TimerList.removeAll()
// do cleanup item states by switching all items to their default (e.g. OFF)
ShellyPlug1_Power.sendCommand(OFF)
end