Simplify rule with cross checking of state

I have a simple 3 strip led above kitchen counter top controilled by dimmers (k_hood_ltv k_hood_lmain k_hood_lplita).
One of the strips is located just above the tv (NH_LG) - so i am switching it off when tv is on and vice versa.
Light is switched on from the button - (K_hood_bl).
I managed to write the rules and everything works - but to me it seems i made it cumbersome and there must be a better way/ Any ideas?

rule "Kitchen light on" when Item K_hood_bl changed from OFF to ON then if (NH_LG.state==ON) { k_hood_lplita.sendCommand(100) k_hood_lmain.sendCommand(100) } if (NH_LG.state==OFF) { k_hood_lplita.sendCommand(100) k_hood_lmain.sendCommand(100) k_hood_ltv.sendCommand(100) } end rule "Kitchen light off" when Item hood_but_light changed from ON to OFF then { k_hood_ltv.sendCommand(0) k_hood_lmain.sendCommand(0) k_hood_lplita.sendCommand(0) } end rule "LGTV light off" when Item NH_LG changed from OFF to ON then if (k_hood_lplita.state==100) { k_hood_ltv.sendCommand(0) } end rule "LGTV light on" when Item NH_LG changed from ON to OFF then if (k_hood_lplita.state==100) { k_hood_ltv.sendCommand(100) } end

NOTE: It is much easier to read code when you use code formatting in your posts. The easiest way is to wrap the code in triple backticks.

```
your code goes here
```

Some ideas:

  • You basically have a state machine here so you can use Groups to store all of the items that need to be modified based on the new state.

  • You can merge some of these rules if you just use a changed trigger and use an if to see if it changed to ON or OFF.

But for the most part this doesn’t look too bad to me. Without messing with Groups I would write it something like the following

rule "Kitchen light"
    when Item K_hood_bl changed
then 
    if(K_hood_bl.state == ON) {
        k_hood_lplita.sendCommand(100)
        k_hood_lmain.sendCommand(100)	
        if(NH_LG.state == OFF) k_hood_ltv.sendCommand(100)
    }
    else {
        k_hood_ltv.sendCommand(0)
        k_hood_lmain.sendCommand(0)
        k_hood_lplita.sendCommand(0)
    }
end

rule "LGTV lights"
    when Item NH_LG changed
then
    if(NG_LG.state == ON) {
        if(k_hood_lplita.state == 100)  k_hood_ltv.sendCommand(0)
    }
    else if(NG_LG.state == OFF) {
        if (k_hood_lplita.state==100) k_hood_ltv.sendCommand(100)
    }
end