How to make rules easy?

First, some approaches for reference.

Rollershutter Rules

Let’s:

  • collapse the three rules into one rule
  • apply an approach where we calculate the thing to be done then do the thing to structure the rule
rule "RolladenLiving command"
when
    Item FF_LivingRoom_Roll received command
then
    // Calculate what to do
    var String pulse = null
    var String power = null
    switch receivedCommand {
        case UP: {
            pulse = "Pulsetime1%20128"
            power = "Power1%20TOGGLE"
        }
        case DOWN: {
            pulse = "Pulsetime2%20128"
            power = "Power2%20TOGGLE"
        }
        case STOP: {
            pulse = "Power1%20OFF"
            power = "Power2%20OFF"
        }
    }

    // do it
    sendHttpGetRequest("http://myIP/cm?cmnd="+pulse)
    sendHttpGetRequest("http://myIP/cm?cmnd="+power)
end

That’s a little better but we can make it a bit shorter. Let’s see if we can improve that initialization part.

rule "RolladenLiving command"
when
    Item FF_LivingRoom_Roll received command
then
    // Calculate what to do
    var String pulse = if(receivedCommand == STOP) "Power1%20OFF" else "Pulsetime1%20128"
    var String power = if(receivedCommand == STOP) "Power2%20OFF" else "Power1%20TOGGLE"

    if(receivedCommand == DOWN){
        pulse.replace("1", "2")
        power.replace("1", "2")
    }

    // do it
    sendHttpGetRequest("http://myIP/cm?cmnd="+pulse)
    sendHttpGetRequest("http://myIP/cm?cmnd="+power)
end

That is definitely shorter though it takes a bit more effort to understand how it works. For the record: initialize pulse and power using the command for the OFF case if the receivedCommand is STOP otherwise initialize it to the UP case. Check to see if the command was DOWN and if so modify pulse and power to cover that case.

You can make this rule generic by creating a mapping between your Item names and IP address (I assume each rollershutter has its own IP) and using the triggeringItem implicit variable:

rule "RolladenLiving command"
when
    Item FF_LivingRoom_Roll received command or
    Item Some_Other_Roll received command or
    ...
then
    // Calculate what to do
    val ip = transform("MAP", "rollershutters.map", triggeringItem.name)

    var String pulse = if(receivedCommand == STOP) "Power1%20OFF" else "Pulsetime1%20128"
    var String power = if(receivedCommand == STOP) "Power2%20OFF" else "Power1%20TOGGLE"

    if(receivedCommand == DOWN){
        pulse.replace("1", "2")
        power.replace("1", "2")
    }

    // do it
    sendHttpGetRequest("http://"+ip+"/cm?cmnd="+pulse)
    sendHttpGetRequest("http://"+ip+"/cm?cmnd="+power)
end

You will need to install the Map tranform and create a file rollershutters.map in your transform folder that looks like the following:

FF_LivingRoom_Roll=192.168.1.123
Some_Other_Roll=192.168.1.124

Yes, but your rules as written won’t work. Please post your Items so I can see what you are trying to do. First of all you need to call .state to get the state of an Item.

switch(FF_Dining_Mode.state)

Secondly, either FF_Dining_Mode is a Number in which case the case part of the switch statement is right and the sendCommand part is wrong, or it is a String Item and it is vise versa. You cannot send a String to a Number Item and you cannot use a Number to compare to the state of a String Item.

Thirdly it is always better to use the method instead of the action for sendCommand and postUpdate.

FF_Dining_Mode.sendCommand("AUTOMATIC")

Finally, assuming all of the above were not a problem and this rule actually ran, you will have created an infinite loop.

  1. FF_Dining_Mode changes
  2. Rule triggers
  3. sendCommand(FF_Dining_Mode, “some new state”)
  4. FF_Dining_Mode changes
  5. Rule triggers
    and so on

Or you have a situation where nothing happens.

  1. FF_Dining_Mode changes
  2. Rule triggers
  3. sendCommand(FF_Dining_Mode", “the same state it already is in”)

And nothing happens. The only thing the rule does is send a command and since the command is the same as what the Item already is in this rule does effectively nothing.

The Item that triggers the Rule must be different than the Items you are sending commands to when the Rule triggers or else you end up doing nothing at all or stuck in an infinite loop.

1 Like