# Rule with Timer for electric windows

**URL:** <https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885>\
**Category:** Scripts & Rules\
**Created:** [July 12, 2019, 6:03am UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885 "2019-07-12T06:03:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 12, 2019, 6:03am UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/1 "2019-07-12T06:03:09Z")

</div>

Hey Guys,

ive got 2 electric windows connected to a 230V-Contact-Actuator.  
Im using 2 contacts of that Device (thing): one to open both windows and one to close both windows.

If i like to operate the windows, i need to set the corresponding signal (to open or to close) for about 30 seconds.  
Ideally i would save the position somewhere as i dont have any hardware-contacts at these windows. But thats just a nice-to-have.

I found some logic and changed it a bit to fit my needs, but it doesnt work. For test i put only 5 seconds.  
The plan is to check, if one of the 2 contacts (of my switch actuator) are set. If they are set, start a timer. If the timer has passed, reset the signal to avoid that one of the 2 switch-contacts are set too long (window runs about 20-25 Seconds per way).

**Things:**

> ```
> Type switch : 1_1_12_Kanal_S_Schalten "Kanal S" [ga="2/0/6+<2/0/8"]
> Type switch : 1_1_12_Kanal_T_Schalten "Kanal T" [ga="2/0/7+<2/0/9"]
> 
> ```

**Items:**

> ```
> Switch 1OG_Wohnzimmer_Fenster_Oeffnen "1.OG Wohnzimmer Fenster Öffnen" <switch> ["Switchable"] { channel="knx:device:bridge:Schaltaktor_230V_1:1_1_12_Kanal_S_Schalten" }
> Switch 1OG_Wohnzimmer_Fenster_Schliessen "1.OG Wohnzimmer Fenster Schliessen" <switch> ["Switchable"] { channel="knx:device:bridge:Schaltaktor_230V_1:1_1_12_Kanal_T_Schalten" }
> 
> ```

**Sitemap:**

> ```
> Switch item=1OG_Wohnzimmer_Fenster_Oeffnen label="Fenster öffnen" icon=contact
> Switch item=1OG_Wohnzimmer_Fenster_Schliessen label="Fenster schliessen" icon=contact
> Text item=1OG_Wohnzimmer_Fenster_Oeffnen label="Status" icon=contact
> 
> ```

**knx.rules:**

> //Logik für Fensterantriebe  
> var Timer timer\_1  
> var Timer timer\_2
> 
> rule “rule\_Fensterantriebe\_Oeffnen”  
> when  
> //Item 1OG\_Wohnzimmer\_Fenster\_Oeffnen changed  
> Thing knx:device:bridge:Schaltaktor\_230V\_1:1\_1\_12\_Kanal\_S\_Schalten changed  
> then  
> if(1OG\_Wohnzimmer\_Fenster\_Oeffnen.state==1)  
> {  
> timer\_1 = createTimer(now.plusSeconds(5))  
> [  
> sendCommand(1OG\_Wohnzimmer\_Fenster\_Oeffnen, OFF)  
> ]  
> }  
> else  
> {  
> if(timer!=null)  
> {  
> timer.cancel  
> timer = null  
> }  
> }  
> end
> 
> rule “rule\_Fensterantriebe\_Schliessen”  
> when  
> //Item 1OG\_Wohnzimmer\_Fenster\_Schliessen changed  
> Thing knx:device:bridge:Schaltaktor\_230V\_1:1\_1\_12\_Kanal\_T\_Schalten changed  
> then  
> if(1OG\_Wohnzimmer\_Fenster\_Schliessen.state==1)  
> {  
> timer\_2 = createTimer(now.plusSeconds(5))  
> [  
> sendCommand(1OG\_Wohnzimmer\_Fenster\_Schliessen, OFF)  
> ]  
> }  
> else  
> {  
> if(timer!=null)  
> {  
> timer.cancel  
> timer = null  
> }  
> }  
> end  
> //;

If you need specific system information, please give me a note 🙂

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 12, 2019, 8:36am UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/2 "2019-07-12T08:36:16Z")

</div>

Why not have a third Switch Item, linked to nothing, representing the (hoped for) window open/close state. That goes on your sitemap.

Then you can have rules like (psuedocode)

```
when
   Item dummy received command ON
then
  send contact x on
  timer for contact x off

when
   Item dummy received command to OFF

```

etc.

---

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 12, 2019, 10:23am UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/3 "2019-07-12T10:23:48Z")

</div>

I am at the very beginning of OpenHab, so not sure how i could use your code. Maybe you can precise it a bit more 🙂

But if i understand correct, i can add Items which are not linked to a thing?!  
That would be like internal tags to set and reset (true/false) via rules?!

In that case, i could simulate the status opened/closed, but still i need to reset the order (order to close contact and “send” power to the windows) after 30 Seconds.

---

<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:** [July 12, 2019, 2:52pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/4 "2019-07-12T14:52:41Z")

</div>

> [@Saarlaender](#):
>
> I am at the very beginning of OpenHab, so not sure how i could use your code. Maybe you can precise it a bit more 🙂

Item

```
Switch dummy // define the label and all the rest as desired

```

Rule

```php
rule "Trigger the window"
when
    Item dummy received command ON
then
    1OG_Wohnzimmer_Fenster_Oeffnen.sendCommand(ON)
    createTimer(now.plusSeconds(25), [ | 1OG_Wohnzimmer_Fenster_Oeffnen.sendCommand(OFF)
end

```

---

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 14, 2019, 1:18pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/5 "2019-07-14T13:18:11Z")

</div>

Thanks.

So i will create a virtual ITEM → switch (without {channel=“blabla”}) ← and ill put this virtual switch ITEM to my SITEMAP.  
If i trigger that switch, it will start my timer and send an ON-Command to the “real” ITEM which is linked to a THING and will switch my KNX-Device.  
After timer passed, it sends OFF-Command to the “real” ITEM and it will switch off the KNX-Device

What would be the way to only check the “real signal” of my KNX-Device regardless of who sent ON-Command?

I mean, to check the state of the ITEMS “1OG\_Wohnzimmer\_Fenster\_Oeffnen” and “1OG\_Wohnzimmer\_Fenster\_Schliessen” which both are connected to a THING (which represents my KNX-Device).

I would just have to change the rule like following or do i have to fulfil additional steps? Im asking because im quite sure i did exactly the following and it didnt work. But i didnt put the “|” in between the “[” and “1OG\_” … what does it do?:

> rule “Reset ON-Command after 25 Seconds”  
> when  
> Item 1OG\_Wohnzimmer\_Fenster\_Oeffnen received command ON  
> then  
> createTimer(now.plusSeconds(25), [ | 1OG\_Wohnzimmer\_Fenster\_Oeffnen.sendCommand(OFF)  
> end

---

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 14, 2019, 1:36pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/6 "2019-07-14T13:36:42Z")

</div>

Ok it seems that the rule isnt called at all. I put it into the “rules” folder and the name was knx.rules - i renamed to default.rules but nothing happens still. I dont see anything in the events.log

I also dont see anything regarding rules in paperui

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 14, 2019, 1:53pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/7 "2019-07-14T13:53:02Z")

</div>

When xxx.rules files are loaded you will see a message in `openhab.log`  
This is different from ` events.log` and you should be looking in both while developing.

> [@Saarlaender](#):
>
> But i didnt put the “|” in between the “[” and “1OG\_” … what does it do?:

It does have a function, it marks off the “lambda” - the block of code that is not to be run now, but later when the timer expires.

Please show us your current rule - the text you posted last does have the | but is missing a ]  
It won’t work if you get the syntax wrong.  
Most syntax errors get reported to you in `openhab.log`

Let’s worry about state when this part is working.

---

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 14, 2019, 2:15pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/8 "2019-07-14T14:15:16Z")

</div>

OK here you go - the following is a test-rule based on my other thread. Should fire each minute, didnt work. I changed to every 5 seconds - doesnt work still

Ill put this in the “default.rules”

> rule “window demo”  
> when  
> Time cron “0/5 0 0 ? \* \* \*” // each 5 seconds  
> then  
> if (myWindow.state==OPEN) {  
> myWindow.postUpdate(CLOSED)  
> }  
> end

I tried this with “if (myWindow == OPEN)” and “if (myWindow.state==OPEN)”

Openhab.log

> 2019-07-14 13:45:58.571 [INFO] [el.core.internal.ModelRepositoryImpl] - Loading model ‘default.rules’
> 
> 2019-07-14 13:47:26.435 [INFO] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘default.rules’
> 
> 2019-07-14 13:49:21.194 [INFO] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘default.rules’

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 14, 2019, 2:34pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/9 "2019-07-14T14:34:12Z")

</div>

> [@Saarlaender](#):
>
> I tried this with “if (myWindow == OPEN)”

Which will never work, assuming myWindow is an Item, it can’t “be” OPEN because it is an object, with several properties like ‘name’, ‘label’, and of course ‘state’.

> [@Saarlaender](#):
>
> “if (myWindow.state==OPEN)”

Okay, so _does_ myWindow ever have the state OPEN?  
If it does not, then your rule will run quite happily and make no visible effect. What makes you think it didn’t work?

You can add some diagnostic tell tales.

```php
rule “window demo”
when
   Time cron “0/5 0 0 ? * * *” // each 5 seconds
then
   if (myWindow.state==OPEN) {
      myWindow.postUpdate(CLOSED)
   } else {
      logInfo ("test" , "Wasn't expecting that " + myWindow.state.toString)
   }
end

```

_Can_ myWindow ever take up the state CLOSED? What type of Item is it?

I’m not sure what this new problem has to do with the rest of the thread.

---

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 14, 2019, 3:25pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/10 "2019-07-14T15:25:37Z")

</div>

This “new topic” is just a more simple test for rules because it looks like that rules are not working at all for me.

The Test-Item (for testing rules) is

> Contact myWindow “Sample Window [%s]”

In Events ive found this

> 2019-07-14 13:24:01.218 [vent.ItemStateChangedEvent] - myWindow changed from NULL to OPEN

So i assume the state is now open. Thats what it displays on the sitemap

Even after using your rule, all i get in openhab.log is this

> 2019-07-14 15:22:05.671 [INFO] [el.core.internal.ModelRepositoryImpl] - Loading model ‘default.rules’
> 
> 2019-07-14 15:22:35.119 [INFO] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘default.rules’

Ive also installed the experimental rule engine, but no change. I removed it again, no change.  
I dont see anything about rules in paperui under configuration - should i?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 14, 2019, 3:43pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/11 "2019-07-14T15:43:59Z")

</div>

> [@Saarlaender](#):
>
> Ive also installed the experimental rule engine

This experimental feature has absolutely nothing to with “conventional” rules as found in `xxx.rules` files. Slow down, randomly doing things isn’t going to help at all.

> [@Saarlaender](#):
>
> I dont see anything about rules in paperui under configuration

Nope. PaperUI has absolutely no idea of anything to do with `xxx.rules` files.

> [@Saarlaender](#):
>
> In Events ive found this … myWindow changed from NULL to OPEN

“Who did that then” I wonder, it wasn’t spontaneous. Anyway, state should then persist until you boot, or reload any Items file.

Okay, so the nub of this cron trigger not working

> [@My first Rule - light on and off every 10 seconds (can't make most basic rule work)](https://community.openhab.org/t/my-first-rule-light-on-and-off-every-10-seconds-cant-make-most-basic-rule-work/42066/7):
>
> I agree with Vincent. Your cron expression doesn’t do what you think it does. “0/10 0 0 ? \* \* \*” means every ten seconds starting at second 0 on minute 00 on hour 00 any day, month, or year. So the cron expression as written will only execute six times between 00:00 and 00:01. I highly recommend using a cron expression builder if you are not familiar with the syntax. I think you are missing the correct interpretation of the starting at. Starting at only applies when you have a / (e.g. 0/10 …

---

<div class="post-metadata">

**Author:** ![Saarlaender](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@Saarlaender](https://community.openhab.org/u/Saarlaender)\
**Post date:** [July 14, 2019, 3:47pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/12 "2019-07-14T15:47:04Z")

</div>

ok ill tested an example from that thread and it seems to work

Interesting, as i used the provided generator and copied the cron…

So…  
not working

> Time cron “0/5 0 0 ? \* \* \*”

working

> Time cron “0/5 \* \* ? \* \* \*”

Ill get back to the initial thing and see if ill get that running, too

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [July 14, 2019, 3:58pm UTC](https://community.openhab.org/t/rule-with-timer-for-electric-windows/77885/13 "2019-07-14T15:58:52Z")

</div>

> [@Saarlaender](#):
>
> Interesting, as i used the provided generator and copied the cron…

This online cron describer

> **[Free Online Cron Expression Generator and Describer - FreeFormatter.com](https://www.freeformatter.com/cron-expression-generator-quartz.html)**
>
> This free online tool lets you build a Quartz Cron expression or converts one in readable text format

(recommend this one as quartz is what OH runs)  
describes “0/5 0 0 ? \* \* \*” as

> Every 5 seconds starting at second 00, at minute :00, at 00am, of every day

which is rather ambiguous and open to interpretation, but describes “0/5 \* \* ? \* \* \*” as

> Every 5 seconds starting at :00 second after the minute

which is different and clearer.  
Anyways, that is all a side show.
