How to send a command every x sec. during the next x minutes?

Hi,

I would like to have a rule where it sends a command every 30 seconds during the next 5 minutes from the time the rule receives the status update.

I have this

rule "Light ON"
when
		Item ZigbeeSensorOneOccupancy changed from "true" to "false"
then
	 {
	   Badkamer_lmp_Brightness.sendCommand(80)
	 }
end

I guess it has to be a cron like

Time cron "0/30 0-5 * ? *"

but then only when the ZigbeeSensorOneOccupancy status is changed.

Can someone help me?

See Design Pattern: Looping Timers. Take a timestamp then create a looping timer. In the timer send the command and then check the timestamp. If five minutes haven’t passed, reschedule the timer for another 30 seconds. If it has set the timer variable to null.

Thank you for your quick reply Rich!
I’ll look into this today or tomorrow.

I now have this rule, but it doesn’t work. Can you tell me what’s wrong?

var Timer ZigbeeTimer = now

   rule "Light 3 min. ON"
   when
	Item ZigbeeSensorOneOccupancy changed from "true" to "false"
   then
    if(ZigbeeTimer !== null) return;

    ZigbeeTimer = createTimer(now, [ |
      if(ZigbeeTimer < now.plusMinutes(3))
	   {
	    Badkamer_lmp_Brightness.sendCommand(80)
            ZigbeeTimer.reschedule(now.plusSeconds(30))
	   }
      else
         {
            if(ZigbeeTimer > now.plusMinutes(3) || ZigbeeSensorOneOccupancy.state == "false")
	    { Badkamer_lmp_Brightness.sendCommand(0) }
	 ZigbeeTimer = null
	 }
    ])
   end

Within the 3 minutes of the state change of the sensor from ‘true’ to false, it will send a command to the light to keep it ON (80).
After the 3 minutes it checks to see if the sensor state is ‘false’ and then send a command to put the light OFF (0).

This will probably not work. I guess now is some kind of DateTime like type, so you can’t assign it to a Timer type value.

Even if it would work, then if(ZigbeeTimer !== null) return; would always return since ZigbeeTimer is no longer null because you already assigned it a value.

This is a good example of the XY Problem.

What you really want is a light that stays on a certain amount of time after the last sensor reading. This is a common problem and well solved problem. For one example see Design Pattern: Motion Sensor Timer.

This approach is very similar to what you have. Perhaps comparing them will reveal the problem. I don’t see anything besides the problem Marcel pointed out. You should be seeing errors on that line from VSCode and in openhab.log on that line.

I guess you’re right @marcel_erkel

Thank you for pointing me to this Rich. I’ll have a look into it.
You are a real wizard in this …