Adding If-else to rule

Hi friends,
I have been running OpenHAB on a Raspberry Pi for almost 2 years and It’s been working great.
I set up a few rules to automatically close and open all my roller-shutters and lights at night and in the morning.

I would now like to add to my Good Morning rule an if-else clause that indicates that if a specific roller-shutter is already partially open before I ran the Good Morning rule, then the rule should skip opening this roller-shutter.

I tried to create this rule, but it didn’t work:

sendCommand(BedRight_Dimmer3, 72)
  Thread::sleep(300)
sendCommand(SalonWindow_Dimmer3, 72)
  Thread::sleep(300)
if {
  (SalonBalcony_Dimmer2 < 50)
  Thread::sleep(300)
}
Else {
  sendCommand(SalonBalcony_Dimmer2, 71)
  Thread::sleep(300)
}
sendCommand(SalonDesk_Dimmer3, 69)
  Thread::sleep(300)
sendCommand(Kitchen_Dimmer
3, 67)
  Thread::sleep(300)
sendCommand(Balcony_Dimmer3, 100)

I was hoping that if the roller-shutter SalonBlinds_Dimmer2 is already set to less than 50% then the open to 71% command will be skipped. And if this dimmer is set to more than 50%, it will be opened to 71%.

I tried to imitate this if-else command from other if-else rules I saw on the community, but I guess I didn’t create the rule correctly, because it doesn’t work.

Can anyone please help me write this command properly?

When you’re copying from other rules you find in the community, make sure to match the syntax. You’ve left out .state and used brackets in the wrong places.

if (item.state < 50)
    {
    command 1
    command 2
    etc.
    }
else if (some other condition)
    {
    commands
    }
else //no conditions
    {
    commands
    }
1 Like

Also, it’s else not Else.

The general concept is: if X then do Y else do Z. That’s what we call pseudo code, it’s not real syntax in any programming language.

So here we have:

  • X = SalonBlinds_Dimmer2.state <= 50
  • Y = noop // a fancy way to say do nothing
  • Z = SalonBlinds_Dimmer2.sendCommand(71)

OK, getting closer but usually you would flip the condition. Instead of testing to see if the current state is <= 50, test to see if it’s > 50. Then we don’t have to worry about the do nothing part.

  • X = SalonBlinds_Dimmer2.state < 50
  • Y = SalonBlinds_Dimmer2.sendCommand(71)

Translating that to Rules DSL syntax for if/else statements:

if(SalonBlinds_Dimmer2.state < 50) {
    SalonBlinds_Dimmer2.sendCommand(71)
}
// else do nothing

Notice the placement of the { }, ( ), getting the state of the Item instead of trying to just compare to the Item itself (which consists of a whole lot more than just its state). Also notice the use of the sendCommand method on the Item rather than the call to the Action which has been the recommendation in the docs for a very long time.

1 Like

Thank you very much to both of you @rpwong @rlkoshak :pray: :pray:
Your replies were very helpful and I managed to set up the rule as I wanted :slight_smile:

When writing simple rules like that you may also want to look at blockly because it avoid syntax issues like the above and you don’t need a lot of programming experience. Maybe you give it a try.

There is also a comprehensive introduction tutorial series on youtube

1 Like