Sleep vs timers

Hiya!

I am just wondering if it is ok to use the php sleep function in scripts? I have noticed that most examples found on this forum use timers to perform delays in actions. So is it ok to do something like this in an event handler:


sleep(60); //sleep for 1 minute
//do something

Or will this cause some malfunction in IPS?

This is a script I have written where the timer has no other function than to delay dimming of lights - the deley is set on the timer. Could I have used sleep instead with no penalty?

<?
//Trigger: Variable_Change Intensity: ""
$LigthtNorm = 50;
$first_run = GetValue ( 34179  /*[Scenarios\Light Controls\Dim Down\first_run]*/ );

if( $IPS_EVENT == 50690  /*[Scenarios\Light Controls\Dim Down]*/ ){
	$LightHigh = GetValue ( 23679 /*[First floor\Entry\Stair+ / Entry roof\Intensity]*/  );
	If ( $LightHigh > $LightNorm && $first_run) {
		IPS_SetEventActive ( 45188  /*[Scenarios\Light Controls\Dim Down]*/, true );
	} else {
		SetValue (34179 , true);
	}
}

if($IPS_EVENT == 45188  /*[Scenarios\Light Controls\Dim Down]*/) {
	if($first_run == true) {
		SetValue (34179 , false);
		IPS_SetEventActive ( 45188  /*[Scenarios\Light Controls\Dim Down]*/  , false );
		MXC_DimSet(52221 /*[First floor\Entry\Stair+ / Entry roof]*/, 50);
	}
}
?>

Thanks in advance :slight_smile:

Using the sleep command will use 1 PHP thread during the sleep time. Having only 10 threads (default setting) available you will need to be sure that at no given time 10 scripts with delays will block all threads.

On the other hand you need to check the maximum execution time for PHP. Default is 150 seconds which you aren’t allowed to exceed.

Therefore it is common to use timer which are far more efficient and the better solution.

paresy

Thanks for your prompt reply, I was not aware of the thread limit.

Is the way I have done it in my example script the correct way to use timers? As you understand I am an IPS newbie and have been reading through heeps of examples on this forum. Unfortunatly I have no knowledge in german, so have to guess and trace the scripts to give them meaning.

Thanks for your patience :slight_smile:

Looks like it could work.

You might have a look at the IPS_SetScriptTimer command. It is similar and maybe easier to use.

paresy

Thanks for the tips paresy, after playing with the IPS_SetScriptTimer command my test script ended like this.

<?
//Trigger: Variable_Change Intensity: ""
define ( "DelayOn" , 1200 , true ); // (in sec.)
$threshold = 50;

if( $IPS_EVENT == 50690 /*[Scenarios\Light Controls\Dim Down\]*/  ){
   $Intensity = GetValueInteger ( 23679 /*[First floor\Entry\Stair+ / Entry roof\Intensity]*/  );
	If ( $Intensity > $threshold ) {
		IPS_SetScriptTimer ( $IPS_SELF , DelayOn );
	} else {
      If ( $Intensity <= $threshold ) {
			IPS_SetScriptTimer ( $IPS_SELF ,  0 );
		}
	}
}
if($IPS_SENDER == "TimerEvent" ) {
		MXC_DimSet(52221 /*[First floor\Entry\Stair+ / Entry roof]*/, $threshold);
		IPS_SetScriptTimer ( $IPS_SELF ,  0 );
	}
?>

Looks good. That’s the way i would have done it :wink:

paresy