Check for status before Triggering Command

Hello OpenHAB community - This is my first post, as an OpenHAB NooB…So go easy ;-).

I am trying to get my head around a problem I am having, and thought someone out there could probably answer this pretty quickly.

I have successfully automated my garage door using a Fibaro Z-Wave Switch and Sensor. The Garage Motor design is pretty basic…i.e. its not a roller shutter type mechanism, it just responds to a button push.

Press Once -> Open (until fully open) - > Press Again - > Stop -> Press Again - > Close (until fully closed).

the Fibaro switch works excellently and the Sensor tells me if the door is open or closed. Brilliant.

I have created a rule, which closes the garage door at 10pm, every night, if I have left it open (which is a god-send when I am tucked up in bed in my PJ’s at which point my wife sais…“Is the Garage closed?”

I have integrated into HomeKit, and I can ask Siri to Open or Close the door. All works flawlessly.

However, here is the problem.

I want to check the status of the door, before triggering the OPEN (or Close) command. But the way rules are constructed, I can’t work out how to do this.

Basically I want to put an “IF” statement into the “when” block of the rule. to say something like.

when
If (GarageDoorSensor.State == OPEN)
then
Item OpenGarage received Command ON IGNORE.

Does that make sense?

What I am trying to get the logic to perform is “if the door is open, don’t open it again”. (and vice versa).

However, the way the rule logic seems to work is you can only apply the “then” statement after the action has been triggered.

This is what I currently have:

when
Item GarageDoorOpen received command ON // AT which point, the door has been triggered and the Garage door is starting to Close

then
if(Garage_Door_State.state == ON) { //check the status of the door

                    sendCommand(Garage_Door,ON) // stops the door.

            }

end

Which is what I currently have. This works (to an extent), but not before the Garage door has started to close, and I would prefer it to Ignore the command altogether, if the state is OPEN.

This actually brings me on to a sub-problem in that - this code will only executes twice, then stops working altogether, until I edit and re-save the default.rules file. Then it will work twice again…and no more. Weird. I am sure it has something to do with the way I have coded this.

The action I am trying to perform in itself is pretty simple - the logic, is pretty simple - but it seems very tricky to implement.

Any advice greatly appreciated. Its not the end of the world if I can’t get this working - as it just means I need to check the app for the status of the door, before I issue a command. But, it would be a nice feature.

Thanks,
Stallie.

You can’t. (rule triggers are events, not steady states)

Set the rule up to trigger when required, and do the test in the then block.
psuedo rule -

when
time is 10pm
then
if door is open
close door
else
do nothing
end

I think what I would do to try and create a glitch free solution, is to use a virtual item to represent the door. Maybe a string, so it can have more than two states OPEN CLOSED MOVING AJAR. Use autoupdate=false so that you can control its state by rules

Write rules that trigger on commands (from UI or other rules) sent to virtual item, and interpret those into switch ‘pushes’

when
door received command OPEN
then
if door state is CLOSED
then
send switch push (for start open)
postupdate door (MOVING)
wait
send switch push off
else ignore

when
door received command STOP
then
if door state is MOVING
then
send switch push (for stop)
postUpdate door AJAR
wait
send switch push off
else ignore

etc

More rules should update the virtual state

when
sensor changed to OPEN
then
postUpdate door OPEN

etc.

Don’t be afraid to make a half dozen rules to manage the real door. Then have other rules e.g. the 10pm trigger, send simple commands to the virtual door.

Thanks Rossko57, I think I understand what your saying! Needs a read through a couple of times, but I get the gist…

I had the auto update=false in the back of my mind so will explore that too.

Thanks

Let the rule trigger and then make the logic have a look here for a simple setup.

when
     Item OpenGarageCommand changed from OFF to ON
then
    if(   GarageDoorSensor.state == OFF ){ 
      // I expect the sensor to be ON when the door ist closed 
      DoSomeCommand
     }else{
     // Do nothing or state it was already closed.
     }
End

I hope I have understand the problem and this helps.