There is a famous quote in computer science credited to Donald Knuth (). " Premature optimization is the root of all evil." Put another way, don’t solve a problem until you know you have a problem. In home automation in general, and in OH 3 in particular, performance almost never matters. You simply are not doing enough to actually experience a performance problem. So don’t try to preemptively fix a performance problem you may never have if for no other reason than you won’t know if you’ve actually fixed it because you’ve no way to test it.
In this case, as rossko57 points out, your attempt to fix a perceived performance problem with Timers (a problem that in fact doesn’t exist) would actually have been far worse from a performance perspective than making no change at all.
That’s why we usually ask for the root problem you are trying to solve and I’m glad you told us.
Now, if you have a bunch of these motion sensor and light combos, you can handle them all using just one rule using the Design Pattern: Associated Items design pattern.
It would be something like
import java.util.Map
val Map<String,Timer> timers = newHashMap
rule "Motion sensor lights"
when
Member of MotionSensors received update 1
then
if(timers.get(triggeringItem.name) === null) {
timers.put(triggeringItem.name, createTimer(now.plusSeconds(10), [ |
sendCommand(triggeringItem.name.replace("Pir", "Llums"), "OFF")
timers.put(triggeringItem.name, null)
])
}
else {
timers.get(triggeringItem.name).reschedule(now.plusSeconds(10))
}
end