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

**URL:** <https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062>\
**Category:** Scripts & Rules\
**Created:** [February 8, 2019, 3:40pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062 "2019-02-08T15:40:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jorg](https://community.openhab.org/u/Jorg)\
**Post date:** [February 8, 2019, 3:40pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/1 "2019-02-08T15:40:57Z")

</div>

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

```php
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

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

```

but then only when the ZigbeeSensorOneOccupancy status is changed.

Can someone help me?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 8, 2019, 5:27pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/2 "2019-02-08T17:27:00Z")

</div>

See [Design Pattern: Looping Timers](https://community.openhab.org/t/design-pattern-looping-timers/47699). 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.

---

<div class="post-metadata">

**Author:** ![Jorg](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jorg](https://community.openhab.org/u/Jorg)\
**Post date:** [February 9, 2019, 7:45am UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/3 "2019-02-09T07:45:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jorg](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jorg](https://community.openhab.org/u/Jorg)\
**Post date:** [February 10, 2019, 12:25pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/4 "2019-02-10T12:25:58Z")

</div>

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

```php
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).

---

<div class="post-metadata">

**Author:** ![marcel\_erkel](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@marcel\_erkel](https://community.openhab.org/u/marcel_erkel)\
**Post date:** [February 10, 2019, 12:49pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/5 "2019-02-10T12:49:52Z")

</div>

> [@Jorg](#):
>
> var Timer ZigbeeTimer = now

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.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 10, 2019, 4:58pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/6 "2019-02-10T16:58:50Z")

</div>

This is a good example of the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378).

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](https://community.openhab.org/t/design-pattern-motion-sensor-timer/14954).

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.

---

<div class="post-metadata">

**Author:** ![Jorg](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jorg](https://community.openhab.org/u/Jorg)\
**Post date:** [February 10, 2019, 6:20pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/7 "2019-02-10T18:20:37Z")

</div>

I guess you’re right @marcel_erkel…

---

<div class="post-metadata">

**Author:** ![Jorg](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/6de8d8/32.png) [@Jorg](https://community.openhab.org/u/Jorg)\
**Post date:** [February 10, 2019, 6:22pm UTC](https://community.openhab.org/t/how-to-send-a-command-every-x-sec-during-the-next-x-minutes/67062/8 "2019-02-10T18:22:00Z")

</div>

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