If Then Else Rule Not Working

Hello All,

I recently joined OpenRemote and trying to customize my first UI and control my devices.

Till now all goes well, but the “If Then Else” rule is not working and after my several troubleshooting i noticed when i update my local controller it wrote that “syntax error on token “else”, delete this token”.

I followed exactly the example in OpenRemote manual but still the same error pup up.

And here is below the rule that i use:

package org.openremote.controller.protocol

global org.openremote.controller.statuscache.CommandFacade execute;

global org.openremote.controller.statuscache.SwitchFacade switches;

global org.openremote.controller.statuscache.LevelFacade levels;

import org.openremote.controller.protocol.*;

rule “Turn Boiler On” when

Event( source == “Boiler Sensor”, value == “on” )

then

execute.command( “Boiler On” );

else

execute.command( “Boiler off” );

end

Can someone advise please ?

Thank you in advance.

There is no 'else' in rules. Only 'then'. For making on/off like in your example you need two rules, one which is triggered by 'on' and the other by 'off' sensor values.

This is correct if you think about it. A rule is triggered by its when statement. With 'else' it would mean that the else branch must be triggered almost continuously except those rare moments than then is executed. This is how Boolean logic works.

Thank you Michal for your reply. Actually i already did 2 rules, but in the examples of Opemremote they mentioned the 'If Then Else" and makes me confused and curious about the way that i should do it.

Anyway i have several applications that i would like to do it with OpenRemote but there is no a clear manual for all the syntax that i can do. Do you recommend any clear an correct manual for the rules?

Thank you in advance.

Well, there is no clear manual of OpenRemote's Drools implementation. AFAIK the best one is Google. I've already written lot of rules that I probably know almost everything of what is possible and impossible there. For my limited knowledge of home automation and other IoT systems this one is the most powerful.

Hi

Just to throw a curve ball in...

This set of rules with "else" sections works perfectly for me :slight_smile:

It's purpose is to monitor the state of two thermostats and toggle two IP relays.

    package org.openremote.controller.protocol;
    
    global org.openremote.controller.statuscache.CommandFacade execute;
    
    import java.util.*;
    
    // SEND COMMAND IN RESPONSE TO BUTTON STATUS CHANGE
    
    rule "GPO48-Heater To Relay1"
    when
        $evt:Event(source matches "GPO48-HeaterStatus", $source : source) // NOTE SENSOR NAME IS WHAT IS BEING COMPARED HERE
    then
        String buttonStatus = $evt.getValue().toString();
    boolean isActive = buttonStatus != null && buttonStatus.equalsIgnoreCase("PRESSED");
    
        System.out.println("Button Status Change '" + $source + "': " + buttonStatus);
    
        // EXECUTE ANY COMMANDS BY USING THEIR NAME AND VALUE TO SET
        // ADD ADDITIONAL COMMANDS AS REQUIRED
    
    if (isActive)
        {
        execute.command("Relay 01 On", ""); // Uncomment to send command
        execute.command("Relay 01 Virtual", "ON"); // This is just a virtual command to fake relay feedback
    }
        else
        {
            execute.command("Relay 01 Off", ""); // Uncomment to send command
            execute.command("Relay 01 Virtual", "OFF"); // This is just a virtual command to fake relay feedback
    }
    end

    rule "GP2-52-Heater To Relay2"
    when
        $evt:Event(source matches "GP2-52-HeaterStatus", $source : source) // NOTE SENSOR NAME IS WHAT IS BEING COMPARED HERE
    then
        String buttonStatus = $evt.getValue().toString();
        boolean isActive = buttonStatus != null && buttonStatus.equalsIgnoreCase("PRESSED");
    
        System.out.println("Button Status Change '" + $source + "': " + buttonStatus);
    
        // EXECUTE ANY COMMANDS BY USING THEIR NAME AND VALUE TO SET
        // ADD ADDITIONAL COMMANDS AS REQUIRED
    
        if (isActive)
        {
            execute.command("Relay 02 On", ""); // Uncomment to send command
    // execute.command("Relay 02 Virtual", "ON"); // This is just a virtual command to fake relay feedback
        }
        else
        {
            execute.command("Relay 02 Off", ""); // Uncomment to send command
    // execute.command("Relay 02 Virtual", "OFF"); // This is just a virtual command to fake relay feedback
        }
    end