Rule with Timer for electric windows

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 :slight_smile:

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.

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 :slight_smile:

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.

Item

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

Rule

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

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

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

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.

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.

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’

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’.

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.

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.

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?

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.

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

“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

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

This online cron describer
https://www.freeformatter.com/cron-expression-generator-quartz.html
(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.