Help with another simple rule (SOLVED)

Hi everyone im having some trouble with a simple rule

what im trying to do is create a rule that checks every two mins if lights and motion detectors report as off if they are all off set a switch too OFF if any are on set the switch to ON my attempt below

rule "Motion Detection"
when
Time cron "	0 0/2 * 1/1 * ? *" //Every 2 mins
then
if( BULB1DSKITCHEN_Brightness.state == 0 ) {
if( Lamp1_Brightness.state == 0 ) {
if( BULB3LAMAIN_Brightness.state == 0 ) {
if( BULB4DSFDMAIN_Brightness.state == 0 ) {
if( BULB5TOMAIN_Brightness.state == 0 ) {
if( BULB7DSBACKDOOR_Brightness.state == 0 ) {
if( BULB8USBATHROOM_Brightness.state == 0 ) {
if( HUE_Motion.state == OFF ) {
if( HUE_Motion2.state == OFF ) {
if( HUE_Motion3.state == OFF ) {
if( HUE_Motion4.state == OFF ) {
if( HUE_Motion5.state == OFF ) {
    House_Motion.sendCommand ("OFF") }
else {House_Motion.sendCommand ("ON")}
}}}}}}}}}}}}
end

Put the 7 Brightness items in a group

Group:Number:SUM gTotalBrightness

Put the 5 Hue motions in a group:

Group:Switch:OR(ON, OFF) gHueMotions

Now your rule is:

rule "Motion Detection"
when
    Time cron "0 0/2 * 1/1 * ? *" //Every 2 mins
then
    if ((gHueMotions.state == OFF) && ((gTotalBrightness.state as Number) == 0)) {
        House_Motion.sendCommand ("OFF")
    } else {
        House_Motion.sendCommand ("ON")
    }
end
2 Likes

Is there a limit too the amount of groups an item can be a part of

Or at least use the && operator.

if (BULB1DSKITCHEN_Brightness.state == 0 &&
Lamp1_Brightness.state == 0 &&
...
1 Like

Not that I know of

Im still pretty newish too this I didn’t know there was an && operator the problem is with the switch part of the rule not the checking of item states I mainly make do with just using the if operator

What do you mean?
Do you have problems with triggers?

&& means AND, it’s used when you want to check multiple conditions in one if-statement instead of nesting multiple ifs.

1 Like

The part that at the bottom that says if these are all off switch motion_home to off and if any are on set it too on

I will update some of my rules and use that it makes it simple with less brackets

Actually we can make that even a bit better:

rule "Motion Detection"
when
    Time cron "0 0/2 * 1/1 * ? *" //Every 2 mins
then
    if ((gHueMotions.state == OFF) && ((gTotalBrightness.state as Number) == 0)) {
        House_Motion.sendCommand(OFF)
    } else {
        House_Motion.sendCommand(ON)
    }
end

What was wrong with my version of the rule apart from it being long

There was 1 } too many at the end

1 Like

The problem is the else-part, which will only be triggered if every if-statement except the last evaluates to true.

Haha there is alot of them it’s the only way I knew how too do what I wanted I have used the same rule slightly different before works perfect just couldent get it running for this use case

So as soon as it found a light on or motion on it ends the rule and never gets far enough too change the switch to ON

@Sharpy

Have a look at:

It checks you syntax for you and much more!! Brilliant tool

Correct. If you put all conditions in one if-statement with && between them all must be true for the code to run, if only one of them is false the else-code is run instead.

I use vscode to edit my files it has made it alot easier to create these files but I have been getting an error so its not working properly Im not sure what’s wrong and I just havent got around too finding the problem it says there is a server error check openhab log

Thanks for that it’s better to learn what you were doing wrong than letting others just post code for me

1 Like