Rule Clean up

Wow there are a lot of rules here.

Some basic suggestions:

  • It is redundant to trigger a rule on changed an received update. If you use received update the rule will trigger when the value changes too. Normally I would just use changed because if the temperature hasn’t actually changed you probably just want to keep doing what you were doing. However, since this automation has life or death implications I would recommend just using received update. That way if for some reason things get out of sync it won’t stay out of sync for as long.

  • Consider a single Temperature rule and combine all of the rules that trigger on Aquarium_Sump_Temp into one rule. This will centralize all your temp control logic and make it easier to see and understand what devices get turned on and when. This can be complicated logic so if you can get it all to fit on one screen it is easier to deal with. It also avoids unintended and unexpected interactions between the rules. Right now all of these rules will be running at the same time in an unpredictable order. If you forget that and set one device ON in one rule another may turn it OFF.

Then I’d move the actual setting and logging of the new state to a lambda to simplify your conditions.

NOTE: It is a good habit to use ItemName.sendCommand as opposed to sendCommand() as the former is much better able to handle conversions of data types.


val Functions$Function3 applyState = [SwitchItem sw, State newState, String name |
    if(sw.state != newState){
        logInfo("AquariumSumpTemp", "Acquarium Temperature, turning " + newState.toString.toLower + " the " + name)
        sw.sendCommand(newState)
]

rule "Aquarium Temperature"
when
    Item Aquarium_Sump_Temp received update
then
    // Mark the time of the update
    postUpdate(Aquarium_Sump_Temp_LastUpdate, new DateTimeType())

    // Fan
    if (Aquarium_Sump_Temp.state > 78.1) applyState .apply(Aquarium_Cooling, ON, "fan")
    else if(Aquarium_Sump_Temp.state < 77.7) applyState.apply(Aquarium_Cooling, OFF, "fan") 

    // Secondary Heater
    if (Aquarium_Sump_Temp.state < 77.3 && Aquarium_Manualmode.state == OFF) applyState.apply(Aquarium_Heater_2, ON, "Secondary Aquarium Heater")
    else if (Aquarium_Sump_Temp.state > 77.8 Aquarium_Manualmode.state == OFF) applyState.apply(Aquarium_Heater_2,OFF, "Secondary Aquarium Heater")

    // Primary Heater
    if (Aquarium_Sump_Temp.state < 78 Aquarium_Manualmode.state == OFF) applyState.apply(Aquarium_Heater_1, ON, "Primary Heater")
    else if (Aquarium_Sump_Temp.state > 78.5 && Aquarium_Manualmode.state == OFF ) {
        applyState.apply(
        logInfo("AquariumSumpTemp: ","Aquarium Temperature to High, turning off All Heater")
        Aquarium_Heater_1.sendCommand(OFF)
        Aquarium_Heater_2.sendCommandOFF)
    }

    // Loft Fan
    if ( Aquarium_Sump_Temp.state < 76) applyState.apply(FF_Loft_Fan, OFF, "loft fan")
end

That is as far as I got but I’m sure you can apply similar things to the rest of your rules. Also, look at the Design Patterns thread for additional Ideas.

2 Likes