More then 5 timers in rules at one time

Hello,

i have heard, that at one time there is a limit of 5 executing rules.

Please, clear me next situation:
I have some rules where i have 2-3 hours timers. I mean that the timer can be active 2-3 hours (for example google speakers repeats every 30 minutes that the post came, or washingmachine finished and so on - till the action is complete - postbox is checked, or wasningmachine is off…).

Am i right, that if 5 timers like this in rules will work at same time, so no other rule can be executet till this timers will be canceled?

Timers like this

	timer = createTimer(now.plusHours(hours), [|

	])

If so, how can this situation be solved?

Thanks!

No that the Benefit of Timers between thread::sleep … they don’t block anything und can be cancelt or rescheduled :slight_smile:

1 Like

I use the Expire Binding for Repeat anything …

val java.util.Map<String, org.eclipse.smarthome.model.script.actions.Timer> tfkWarnTimers = newHashMap

val Functions$Function6<ContactItem,StringItem,RollershutterItem,java.util.HashMap<String,Timer>,String,Integer,Boolean> fsWarnLogic = [
	fsTFK_Item,fsTFK_Status_Item,RL_Item,
	java.util.Map<String, org.eclipse.smarthome.model.script.actions.Timer> timers,
	String fsTFK_name, int timeout |

  if (fsTFK_Item.state == OPEN){
    if (fsTFK_Status_Item.state != "Geöffnet > " + timeout + " Minuten"){
			fsTFK_Status_Item.postUpdate("Geöffnet")
		}
    timers.get(fsTFK_name)?.cancel
    timers.put(fsTFK_name, createTimer(now.plusMinutes(timeout)) [|
			if (fsTFK_Status_Item.state != "Geöffnet > " + timeout + " Minuten"){
    		sendTelegram("gBot", fsTFK_name + " ist länger als " + timeout + " Minuten geöffnet\nBitte schliessen!!!")
				fsTFK_Status_Item.postUpdate("Geöffnet > " + timeout + " Minuten")
			}
			//Akustische Warnung, wenn der Gefrierschrank länger als "timeout" geöffnet ist!
			if (fsTFK_Item.name.toString.equals("TFK_Gefrierschrank")){
				if (Presence_Start_Up.state == ON || Presence.state == ON && Presence_Start_Up.state == OFF) {
					//Timer (Expire Binding) starten, damit die Warnung wiederholt wird, bis der Gefrierschrank geschlossen ist!
					//Wiederholung der Warnung errechnet sich aus Timeout + Expire
					Gefrierschrank_Warn_Timer.sendCommand(ON)
					//Akustische / Optische Warnung
					Funkgong_LS_Kanalaktion.sendCommand("1,1,108000,010")
					Thread::sleep(1000)
					Funkgong_LED_Kanalaktion.sendCommand("1,20,108000,17,18,17,18,17,18,17,18,0,0")
				}
			}
			timers.remove(fsTFK_name)
			])
		//Prüfen ob Rolladen übergeben wurde
		if (RL_Item !== null){
			if (RL_Item.state > 80) RL_Item.sendCommand(80)
    }
  }
  else{
    if (fsTFK_Item.state == CLOSED){
      timers.get(fsTFK_name)?.cancel
      timers.remove(fsTFK_name)
      fsTFK_Status_Item.postUpdate("Geschlossen")
			//Prüfen ob Rolladen übergeben wurde
			if (RL_Item !== null){
				if (Nacht_Modus.state == ON){
		      if (RL_Item.state < 100) RL_Item.sendCommand(DOWN)
		      }
		   }
			 if (fsTFK_Item.name.toString.equals("TFK_Gefrierschrank")){
	 			Gefrierschrank_Warn_Timer.postUpdate(OFF)
			}
    }
  }
	true
]
when
  Item TFK_Gefrierschrank changed from CLOSED to OPEN or
  Item TFK_Gefrierschrank changed from OPEN to CLOSED or
	Item Gefrierschrank_Warn_Timer changed from ON to OFF
then
    fsWarnLogic.apply(TFK_Gefrierschrank, FS_Gefrierschrank_Status, null, tfkWarnTimers, "Gefrierschrank", 2)
end

Thanks!

So what does mean - limit of 5 rules at one time? )))

There are two separate thread pools. There is the Rule execution thread pool that is used when a Rule is triggered by an event (e.g. received command). This thread pool was of size five by default. I’m not sure if that has changed.

The second thread pool is used by Timers, certain bindings like Astro, and Time triggered Rules. This thread pool used to be of size 2 but it is now of size 10.

The thread pool size means that is the maximum number of Rules or Timers that can actively be executing at any given time. A Timer is only using a thread from the pool when it is actively executing the code in the timer. When it is waiting to trigger it is not consuming any threads at all.

Therefore, if you have a while loop with Thread::sleep to check whether the action is complete, you have problems because that means you are tying up a thread the whole time you are waiting for the action to complete. But if you have a Timer or a Rule that triggers, checks if the action is complete and if not reschedules itself to run again later than you are only using a thread while it is checking. The rest of the time it is not consuming a thread.

For those using Scripted Automation, the limits of the tread pools are not applicable. There only limit to the number of Rules or Timers you can have executing at the same time is RAM.

1 Like

Rich, as always thanks for explanation in details!
So am i right that the rule with timer like this

timer = createTimer(now, [|
     someCommandToExecute()
     timer.reschedule(now.plusMinutes(30))
])

uses thread only in time of timer creation and then one time in 30 minutes when triggering and executing the command?

Thanks!

Correct. But of course that particular Timer will never end. You may as well create a Time cron triggered rule to run every 30 minutes and save the extra work of creating and managing the Timer.

1 Like

Yes it was example, of course i cancel timer sometime.
First of all i thought also about cron and expire binding, but i have different durations of timers, so cron and expire will be a little bit more complicated i think.