Create 60 timers

I plan to create about 50-60 timers. Does anybody know if this will work or is there anything I need to consider?

Socondly I just like to confirm: If the timer needs to survive a restart of openhab, I need to store the timer in a prvate cache, right?
And what are the mechanics behind it - if I delete the cache variable, the timer gets deleted, too?

There shouldn’t be any major problem with that. However, if these are created from the same rule note that only the rule itself or eactly one of the timers can actually be running at any one time. So if you have a bunch of timers that go off at the same time, there may be some latency between when the timer was scheduled to run and when it actually gets a chance to run.

No, you will lose all timers on a restart of OH under all circumstances. You’ll need to store information about when these timers need to go off somewhere (Items, Item metadata, a config file, persistence, etc) and recreate the Timers from a system started rule when OH comes bcak up.

What the private cache will do for you is when your rule gets unloaded, OH will cancel all the timers stored in the cache to avoid orphaned timers.

I have adding this to openhab_rules_tools on my list but it’s way down on my list. You’ll have to do that part yourself.

No, you still need to cancel the timer if you really want the timer to go away. The cache only exists in memory and it only stores Objects. It’s nothing more than that.

You don’t mention the language but if this is JS, openhab_rules_tools has TimerMgr which handles all the book keeping involved with working with lots of timers. Usually the use case is one timer per Item that triggers the rule but it works anywhere you have some unique way to identify each individual timer. It’s mature and well tested and I’ve used it with dozens of Timers (not 50-60 though).

It’s used like this:

var {TimerMgr} = require("openhab_rules_tools");

var timers = cache.private.get("timers", () => { TimerMgr() }; // creates the manager if it doesn't already exist

timers.check(event.itemName, "PT1M", timerFunction, true, flappingFunc, name);

The arguments are:

  1. key: in this case the name of the Item that triggered the rule
  2. when: anything supported by time.toZDT() and when the timer should run
  3. timerFunc: the function called when the timer runs
  4. reschedule: when true, if a timer already exists at that key reschedule it with when, when false do nothing
  5. flappingFunc: if the timer already exists but hasn’t run yet, call this function
  6. name: if there is an error the timer function, this name is used in the error logs to help identify where the problem occurred.

Only the first two arguments are required and one or both of 3 and 4 are required.

Real use example from my Debounce rule template.

var debounce = () => {
  console.debug('Debounce:', event.type, 'item:', event.itemName);

  // Initialize the timers, config and end debounce function
  const timers = cache.private.get('timerMgr', () => TimerMgr());
  const cfg = getConfig(event.itemName);
  const endDebounce = endDebounceGenerator(event.itemName, event.itemState, cfg.proxy, cfg.command);

  // If there are no states in the debounce metadata or if the new state is in the list of debounce states
  // set a timer based on the timeout parameter
  if(cfg.states.length == 0
     || cfg.states.includes(event.itemState.toString())) {
    console.debug('Debouncing ', event.itemName, "'s state", event.itemState,
                 'using proxy', cfg.proxy, 'timeout', cfg.timeout,
                 'command ', cfg.command, 'and states ', cfg.states);
    timers.check(event.itemName, cfg.timeout, endDebounce);
  }
  // If this is not a debounced state, immediately forward it to the proxy Item
  else {
    console.debug(event.itemName, 'changed to', event.itemState, 'which is not among the debounce states:', cfg.states);
    timers.cancel(event.itemName);
    endDebounce();
  }
};

You don’t say what these timers are doing or what they are for. It might be that you don’t need to code anything at all and you can use Threshold Alert and Open Reminder [4.0.0.0;4.9.9.9]. This template handes cases when an Item doesn’t change for a time, remains in a given state for a time, or meets some criteria for a time (e.g. >= 45 %).

It’s useful for things like:

  • controling a light with a motion sensor
  • controlling a humidifier based on a sensor reading
  • getting an alert when a door is left open for a given time
  • getting an alert when a sensor ssstops reporting for a time
    Maybe that can be useufl for your use case.
1 Like

I would also see if you can use the parameter “expire” on items:
Openhab Items