Help with rules syntax

Thanks for all your replies and suggestions.
Working on it and trying hard to understand it all.
My major problem is I don’t speak the code-language. So I don’t know where to put what.

For example can anyone guide me where I can find some documentation about all the ( ) { and}
Where and when do I need to put all these things?

rule "Licht schuur beweging"
when 
   Item Beweging_Schuur changed
then
      if Item Beweging_Schuur.state == (ON)
      sendCommand(Verlichting_Schuur, ON)
      
   }
end

as this doesn’t work :wink:

Thank u

rule "Licht schuur beweging"
when 
   Item Beweging_Schuur changed
then
      if Item Beweging_Schuur.state == (ON)
      sendCommand(Verlichting_Schuur, ON)
      
   }
end

And an if there is a test. In your case you want to know if Beweging_Schuur is ON
The test must be between ()
After an if your instruction put these between {}
The first one say Here starts the if instruction
The last one } say, end of instructions

To send a command do this Verlichting_Schuur.sendCommand(ON)

So now you rule is:

rule "Licht schuur beweging"
when 
   Item Beweging_Schuur changed
then
    if (Item Beweging_Schuur.state == ON) {
        Verlichting_Schuur.sendCommand(ON)
   }
end
1 Like

Oke thanks so much.
But why can’t it be like this:

rule "Licht schuur beweging"
when 
   Item Beweging_Schuur changed
then
    if (Item Beweging_Schuur.state == ON) 
        {Verlichting_Schuur.sendCommand(ON)}
   
end

The brace brackets { } show what is covered by the if statement

1 Like

It can. Have you tried it? Rules DSL is pretty flexible about how you present stuff for your own reading clarity. Not at all flexible about having brackets and braces in pairs, though.

1 Like

It can but it is easier to read the way I wrote it

Okay so meaning after the last
} comes what to do … right?

The what to do if the if condition is true is BETWEEN the {}
After the } resumes the rule

Right thanks. I think I got it finally :slight_smile:

@Koensk if you are thinking about {} curly brackets, these are not essential, as long as only one command is executed.

rule testrule // quotes are optional, as long as the rule name is only one word
when
    Item myItem changed  // every time, the state of myItem changed
then
    logInfo("test","test line 1")
    if(myItem.state == OFF)
        logInfo("test","test line 2")
    logInfo("test","test line 3")
    if(myItem.state == ON) {
        logInfo("test","test line 4")
        logInfo("test","test line 5")
    }
end

When the rule is triggered,

the first log line is executed.
the second log line is only executed if the status of myItem is OFF.
the third log line will be executed, because the if statement will only apply to the first following line.
the fourth and fifth log line will only be executed if myItem is of state ON

So {} is used to build a block of commands.

Hope this didn’t add any confusion just thought it was something you might want to know.
For if and else statments here is a good reference. I keep it bookmarked.:smile:

1 Like