# More if´s in a rule

**URL:** <https://community.openhab.org/t/more-if-s-in-a-rule/38809>\
**Category:** Scripts & Rules\
**Created:** [January 15, 2018, 8:34pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809 "2018-01-15T20:34:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamzon](https://community.openhab.org/user_avatar/community.openhab.org/zamzon/32/17184_2.png) [@zamzon](https://community.openhab.org/u/zamzon)\
**Post date:** [January 15, 2018, 8:34pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/1 "2018-01-15T20:34:42Z")

</div>

Hi

I have to make a rule that sends a message to an item depending on what another item string has of value

```php
rule Start_w2811

when
  Item w2811ts changed

then

if (w2811ts == START) {
w2811.sendCommand("{\"state\": \"ON\"}")
}
if (w2811ts == STOP) {
w2811.sendCommand("{\"state\": \"OFF\"}")
}
if (w2811ts == PAUSE) {
w2811.sendCommand("{\"state\": \"PAUSE\"}")
}
if (w2811ts == FORWAD) {
w2811.sendCommand("{\"state\": \"FORWAD\"}")
}
if (w2811ts == BACK) {
w2811.sendCommand("{\"state\": \"BACK\"}")
}

end

```

I know a can’t write “IF” so many times, but how do I write this rule if I need more if’s

---

<div class="post-metadata">

**Author:** ![Willemde20](https://community.openhab.org/user_avatar/community.openhab.org/willemde20/32/72801_2.png) [@Willemde20](https://community.openhab.org/u/Willemde20)\
**Post date:** [January 15, 2018, 8:43pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/2 "2018-01-15T20:43:54Z")

</div>

You should use a switch case construction.

```php
rule Start_w2811

when
  Item w2811ts changed

then
    switch{
        case w2811ts == START:
            w2811.sendCommand("{\"state\": \"ON\"}")
            break;
        case w2811ts == STOP:
            w2811.sendCommand("{\"state\": \"OFF\"}")
            break;
        case w2811ts == PAUSE:
            w2811.sendCommand("{\"state\": \"PAUSE\"}")
            break;
        case w2811ts == FORWAD:
            w2811.sendCommand("{\"state\": \"FORWAD\"}")
            break;
        case w2811ts == BACK: 
            w2811.sendCommand("{\"state\": \"BACK\"}")
            break;
        default:
            break;
    }

end

```

Now you can add more cases to it.

---

<div class="post-metadata">

**Author:** ![zamzon](https://community.openhab.org/user_avatar/community.openhab.org/zamzon/32/17184_2.png) [@zamzon](https://community.openhab.org/u/zamzon)\
**Post date:** [January 15, 2018, 8:56pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/3 "2018-01-15T20:56:14Z")

</div>

Hi  
I get this error running the rule

2018-01-15 21:54:31.588 [WARN] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘sendmqtt2.rules’ has errors, therefore ignoring it: [8,9]: mismatched input ‘case’ expecting ‘}’

[8,14]: missing ‘{’ at ‘w28112’

[8,21]: no viable alternative at input ‘==’

[8,29]: mismatched input ‘:’ expecting ‘end’

```auto
when
  Item w28112 changed

then
    switch{
        case w28112 == START:
            w2811.sendCommand("{\"state\": \"ON\"}")
            break;
        case w28112 == STOP:
            w2811.sendCommand("{\"state\": \"OFF\"}")
            break;
        case w28112 == PAUSE:
            w2811.sendCommand("{\"state\": \"PAUSE\"}")
            break;
        case w28112 == FORWAD:
            w2811.sendCommand("{\"state\": \"FORWAD\"}")
            break;
        case w28112 == BACK:
            w2811.sendCommand("{\"state\": \"BACK\"}")
            break;
        default:
            break;
    }

end

```

---

<div class="post-metadata">

**Author:** ![Willemde20](https://community.openhab.org/user_avatar/community.openhab.org/willemde20/32/72801_2.png) [@Willemde20](https://community.openhab.org/u/Willemde20)\
**Post date:** [January 15, 2018, 9:02pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/4 "2018-01-15T21:02:02Z")

</div>

try this

```php
rule Start_w2811

when
  Item w2811ts changed

then
    switch (w2811ts.state){
        case START:{
            w2811.sendCommand("{\"state\": \"ON\"}")
            break;
        }
        case STOP:{
            w2811.sendCommand("{\"state\": \"OFF\"}")
            break;
        }
           
        case PAUSE:{
            w2811.sendCommand("{\"state\": \"PAUSE\"}")
            break;
        }
        case FORWAD:{
            w2811.sendCommand("{\"state\": \"FORWAD\"}")
            break;
        }
        case BACK:{ 
            w2811.sendCommand("{\"state\": \"BACK\"}")
            break;
        }
        default:{
            break;
        }
            
    }

end

```

---

<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:** [January 15, 2018, 9:10pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/5 "2018-01-15T21:10:26Z")

</div>

> [@zamzon](#):
>
> I know a can’t write “IF” so many times

But you can write that many if() as you wish.

You can also build if-else, like so

```php
if (w2811ts.state == "START") {
    w2811.sendCommand("{\"state\": \"ON\"}")
} else if (w2811ts.state == "STOP") {
    w2811.sendCommand("{\"state\": \"OFF\"}")
} else if (w2811ts.state == "PAUSE") {
    w2811.sendCommand("{\"state\": \"PAUSE\"}")
} else {
    logInfo("whoops", "unexpected value")
}

```

EDIT @Josar is quite right, we must look at the Item .state. Fixed my example!

---

<div class="post-metadata">

**Author:** ![Josar](https://community.openhab.org/user_avatar/community.openhab.org/josar/32/13357_2.png) [@Josar](https://community.openhab.org/u/Josar)\
**Post date:** [January 15, 2018, 9:18pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/6 "2018-01-15T21:18:22Z")

</div>

@zamzon [xtend](https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html) expressions.

```auto
switch w2811ts {
        // if w2811ts is a stringItem the string is in the state, you want to compare the state 
        case w2811ts.state == "START": w2811.sendCommand("{\"state\": \"ON\"}") // no break xtend does not fall trough
        case ...
        default:
    }

```

---

<div class="post-metadata">

**Author:** ![zamzon](https://community.openhab.org/user_avatar/community.openhab.org/zamzon/32/17184_2.png) [@zamzon](https://community.openhab.org/u/zamzon)\
**Post date:** [January 17, 2018, 6:17pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/7 "2018-01-17T18:17:19Z")

</div>

> [@Willemde20](#):
>
> rule Start\_w2811
> 
> when  
> Item w2811ts changed
> 
> then  
> switch (w2811ts.state){  
> case START:{  
> w2811.sendCommand(“{"state": "ON"}”)  
> break;  
> }  
> case STOP:{  
> w2811.sendCommand(“{"state": "OFF"}”)  
> break;  
> }
> 
> ```
> case PAUSE:{
> w2811.sendCommand("{\"state\": \"PAUSE\"}")
> break;
> }
> case FORWAD:{
> w2811.sendCommand("{\"state\": \"FORWAD\"}")
> break;
> }
> case BACK:{ 
> w2811.sendCommand("{\"state\": \"BACK\"}")
> break;
> }
> default:{
> break;
> }
> 
> }
> 
> ```
> 
> end

Hi

If I send START to the string w2811ts, I get the following error in the log

```auto
2018-01-17 19:14:55.600 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Start_w28112': The name 'START' cannot be resolved to an item or type; line 8, column 14, length 5

```

---

<div class="post-metadata">

**Author:** ![zamzon](https://community.openhab.org/user_avatar/community.openhab.org/zamzon/32/17184_2.png) [@zamzon](https://community.openhab.org/u/zamzon)\
**Post date:** [January 17, 2018, 6:23pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/8 "2018-01-17T18:23:25Z")

</div>

@Josar

If Í insert your code I get the following error

```auto
2018-01-17 19:21:17.614 [INFO] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sendmqtt.rules'

2018-01-17 19:21:17.640 [WARN] [el.core.internal.ModelRepositoryImpl] - Configuration model 'sendmqtt.rules' is either empty or cannot be parsed correctly!

```

---

<div class="post-metadata">

**Author:** ![Willemde20](https://community.openhab.org/user_avatar/community.openhab.org/willemde20/32/72801_2.png) [@Willemde20](https://community.openhab.org/u/Willemde20)\
**Post date:** [January 17, 2018, 6:39pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/9 "2018-01-17T18:39:38Z")

</div>

there need to me " " around the START, STOP

it needs to look like

```php
case "START":{
 // code here
}

```

---

<div class="post-metadata">

**Author:** ![zamzon](https://community.openhab.org/user_avatar/community.openhab.org/zamzon/32/17184_2.png) [@zamzon](https://community.openhab.org/u/zamzon)\
**Post date:** [January 17, 2018, 6:53pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/10 "2018-01-17T18:53:26Z")

</div>

It works  
Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Sharpy](https://community.openhab.org/user_avatar/community.openhab.org/sharpy/32/26469_2.png) [@Sharpy](https://community.openhab.org/u/Sharpy)\
**Post date:** [January 17, 2018, 7:36pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/11 "2018-01-17T19:36:18Z")

</div>

why cant you use if like that ?

---

<div class="post-metadata">

**Author:** ![Willemde20](https://community.openhab.org/user_avatar/community.openhab.org/willemde20/32/72801_2.png) [@Willemde20](https://community.openhab.org/u/Willemde20)\
**Post date:** [January 17, 2018, 9:03pm UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/12 "2018-01-17T21:03:50Z")

</div>

You can use the if(…){…} if(…){…}… for ever. in theory.

But it is smarter to do it with a switch case like

```php
Switch(item.state){
   case "option 1":{
  //do stuff
  break;
}case "option 2":{
      //do stuff
      break;
    }
}

```

Or using a if else construction like this:

```php
if(option=1){
 //do stuff
}else if(option == 2){
 //do other stuff
}else{
 //do stuff that can happen to any situation other then in the if above 
}

```

So it stops going through the option after it has doen the option it needed to do when the condition has been met. the execution time will be shorter.

You can do it the way as in using if(…){…} and repeating that. but it is smarter and easier in my opinion to think like:

"if **my light is off** then **turn it on** else if **is it dark in the room** then **turn the light on**"

For me it’s easier to think that way and write the code for it. 🙃

Hope i have made things easier to understand.

Greeting Willem

---

<div class="post-metadata">

**Author:** ![Josar](https://community.openhab.org/user_avatar/community.openhab.org/josar/32/13357_2.png) [@Josar](https://community.openhab.org/u/Josar)\
**Post date:** [January 18, 2018, 12:26am UTC](https://community.openhab.org/t/more-if-s-in-a-rule/38809/13 "2018-01-18T00:26:22Z")

</div>

The rules are not java they are [Xbase and therefore share a lot with xtend](https://docs.openhab.org/configuration/rules-dsl.html#the-syntax).

And using xtend as it is supposed to, makes it a lot easier to write instead of using lots of brackest and uneccessary breaks. And it is easier to read.

```php
switch w2811ts.state {
        case "START": w2811.sendCommand("{\"state\": \"ON\"}") // no break xtend does not fall trough
        case "STOP": ...
        default: ...
    }

```

And you may do thinks witch you would not be able to do with a java switch case. see the link posted above.
