What approach is better for timers - using createTimer or .updatedSince?
I ready use mysql binding and all motion sensors are persisted.
What way is better (faster)?
if(!ESP_Bath_Motion.updatedSince(now.minusMinutes(5) )){ ... }
or
timer_bath = createTimer(now.plusMinutes(5)) [| ... ]
These do quite different things, the updatedSince test will only fire under certain conditions. (I donāt think your example would ever fire, it is looking to see if the Item has been updated since 5 minutes into the future, which can never happen) The Timer will fire under any conditions, at the given time. Both have uses.
Can you explain what you mean by āfasterā in the context of a timer?
It greatly depends on the context. And to further complicate things, there is also Thread::sleep which pauses a rule for a given number of milliseconds.
None of the options are faster than the others. They all do something different and are intended for different uses within your rules.
So here are some rules of thumb I use to choose between them.
updateSince/changedSince/lastUpdate
If Iām in a rule for another reason (e.g. Time triggered, triggered by another Item, triggered by a Group, etc.) and need to check whether an Item has changed or been updated within or beyond a certain amount of time these methods are what I would use.
For example, to get the most recently updated Item out of a Group:
val lastItem = gMyGroup.members.sortBy[lastUpdate].tail
The big distinguishing characteristic is that Iām already in the rule right now for another reason and Iām looking into the past to determine the behavior of my rule.
Thread::sleep
If Iām in a rule for any reason and I want to stop the execution of the rule for a period of time before continuing execution of the rule use Thread::sleep. For example, Iām in a rule that was triggered because a computer went offline. I want to turn off the computerās plug, wait for a few seconds, then turn the outlet back on again.
MyComputerOutlet.sendCommand(OFF)
Thread::sleep(20000)
MyComputerOutlet.sendCommand(ON)
The distinguishing characteristic is that it stops execution of a rule for a period of time.
creatTimer
When you are in a rule and you want to schedule a bit of code to execute sometime in the future without waiting around for the code to execute use a Timer. In a timer you pass it a function (block of code to execute) and a time for that code to execute. Once a Timer is created, the rule will continue to execute and exit but the Timer will still be hanging around waiting to execute at the scheduled time.
There are a lot of uses for a Timer. For example, Timers are great for scheduling some code to execute after a motion sensor dies or to generate an alert if you do not receive an event after a certain period of time (e.g. a sensor that sends a value every five minutes does not send a value after 6 minutes). I use Timers to wait five minutes after the last person leaves the house before setting the house to Away mode.
The distinguishing characteristics of Timers is that they do not block the rule from continuing to execute and exit and they can be rescheduled or cancelled.
So, if Iām in a rule for some other reason and whether an Item has changed in the recent past changes the behavior of the rule Iāll use updatedSince/changedSince/lastUpdate.
If Iām in a rule and I want to delay for a little bit before continueing the rule I will use Timer::sleep.
If Iām in a rule and I want to schedule some code to execute sometime in the future with the option to reschedule it or cancel it I will use a Timer.
Thank you for sharing your time and knowledge.