Rule simplification and code reuse

I’m a bit confused, because there is a simple solution with only one rule which which will “rule them all”

Setup a Group Item:

Group:Switch gLowBat "all low bat items"

and set all LowBat Items to be member of that group.

Now one Rule:

// imports at the begin of file
import java.util.HashMap

var HashMap<String, String> mapText = newHashMap(
    "MiMagnet_Front_BattLow"     ->    "Front door open/closed sensor",
    "MiMotion_UpStairs_BattLow"  ->    "Upstairs motion sensor",
    "MiTemp_Bedroom_BattLow"     ->    "Bedroom temperature sensor",
) as HashMap

rule "Low battery warning"
when
    Member of gLowBat changed to ON
then
    var String Msg = "Low battery warning: " + mapText.get(triggeringItem.name)
    sendNotification("joe@mail.com", Msg, "lowBattery", "Alert") // to myopenhab.org & mobile
    logWarn("lowBattery", Msg)
    say(Msg) // Output to google home speaker
end
3 Likes