Monoprice 6-zone Audio amp items, sitemap & rules

You already have examples code here to consolidate a lot of this code. Note, you can consolidate even more if you were to upgrade to the 2.3 snapshot and use Features for Rules that work with Groups. I’m not recommending that, but be aware there are changes coming in 2.3 that will make rules like this even easier.

rule "Process Update"
when
    Item MonoPrice_Status_G3 received update or 
    Item MonoPrice_Status_G4 received update or 
    Item MonoPrice_Status_G6 received update or
    Item MonoPrice_Status_G7 received update or
    Item MonoPrice_Status_G8 received update or 
    Item MonoPrice_Status_G9 received update  or
    Item MonoPrice_Status_G10 received update
then
    val num = MonoPrice_Status_G1.state as Number

    val type = triggeringItem.name.split("_").get(2)

    switch(type){
        case "G3": postUpdate("MonoPrice_Z"+num+"_Power", if(triggeringItem.state == "01") "ON" else "OFF")
        case "G4": postUpdate("MonoPrice_Z"+num+"_Mute", if(triggeringItem.state == "01") "ON" else "OFF")
        case "G6": {
            val vol = (triggeringItem.state as Number) / 38 * 100
            postUpdate("MonoPrice_Z"+num+"_Volume", vol.toString)
        }
        case "G7": {
            val tre = (triggeringItem.state as Number) / 14 * 100
            postUpdate("MonoPrice_Z"+num+"_Treble", tre.toString)
        }
        case "G8": {
            val bas = (triggeringItem.state as Number) / 14 * 100
            postUpdate("MonoPrice_Z"+num+"_Bass", bas.toString)
        }
        case "G9": {
            val bal = (triggeringItem.state as Numnber) / 20 * 100
            postUpdate("MonoPrice_Z"+num+"_Balance", bal.toString)
        }
        case "G10": {
             postUpdate("MonoPrice_Z"+num+"_Source", triggeringItems.toString)
        }
    }
end

You can collapse your switch statements in your zone rules to

    val znum = triggeringItem.name.split("_").get(1) 
    val zone = znum.substring(string.length() - 1)

For the treble rule you just need to add the line:

val tone_z = zone+"TR"

Even if there were not these String parsing shortcuts, you could use a lambda to consolidate the switch statements

These suggestions should drop your LOC by a third or so. No arrays necessary.

1 Like