Switch aggregator

if anyone needs it i lashed up a quick Switch aggregator, basically if one of multiple switches are on it will switch on a virtual switch (if all off virtual switch will be off).
Use case motion and mode app, i have a tv and a projector in a room if either of these are on i didn't want the lighting automations to run, but with the built in app i could only have one or the other.

definition(
    name: "Switch aggregator",
    namespace: "Mark-C-UK",
    author: "Mark-C",
    description: "agrate switches many to 1",
    category: "Convenience",
    iconUrl: " ",
    iconX2Url: "",
    pausable: true
)

preferences {
    section("Set you input and output devices for ${app.label}"){
		input "InDevs", "capability.switch", title: "input devices to average", required: true, multiple: true
		input "OutDev", "capability.switch", title: "output device - create a hubitat vitrual switch 1st", required: true, multiple: false
        input "logEnable", "bool", title: "Enable debug logging", defaultValue: false
    }
}

def installed() {
	installer()
}
def updated() {
    installer()
}
def initialize() { // Runs on startup
    def randomSixty = Math.abs(new Random().nextInt() % 60)
    log.info "Initialize running ${app.label} installer in ${randomSixty} seconds"
    runIn(randomSixty,installer)
}
    
def installer(){
    log.info "${app.label} installer"
    if (InDevs && OutDev) {
        subscribe(InDevs, "switch", change)
        log.info "${app.label} installer subscribing"
        change()
    }
}

def change(evt){
    if (logEnable) log.info "${app.label} change, device '${evt?.device}', event '${evt?.value}'"
    String onoff = "off"
    if (evt?.value == "on"){
        //log.debug "on"
        onoff = "on"
    }
    else for (dev in settings.InDevs) {
        if (logEnable) log.info "${dev} ${dev.currentSwitch}"
        if (dev.currentSwitch == "on"){
            onoff = "on"
        }
    }
    if (logEnable) log.trace "${app.label} sending value '${onoff}' to '${settings.OutDev}'"
    settings.OutDev.(onoff)
}

Is this scripted in Groovy and entered as a User App in "Apps"? I've seen similar code samples floating around the Forum, and think I'm starting to understand this feature more.

Yea

I see. So, would this RM rule accomplish pretty much the same effect, without the need for scripting?

Need a end if on it but yea,
RM is just a lot more system intensive because of how much it can do

2 Likes

You can do also this with the built-in Groups and Scenes app.

Choose the switches under "Select switches for group", then check the box down the page for "Use group device to indicate if any members are on?"

You'll wind up with a Device in whatever name you choose for the group which will be "on" if any of the aggregated switches are on.

1 Like

It should be noted that this actually doesn't run on startup, at least not if you don't make it so yourself somehow. You might be confusing this--and easily so--with the "Initialize" capability in drivers, which will cause this command with the same name to run on startup automatically.

That being said, I don't see a reason do do this on system start, anyway; a reboot will not clear event subscriptions, and that looks like all it's doing (besides checking the current states, but you already would have done so by this point--and given the above, your app wouldn't have missed any events in the meantime unless the platform as a whole also did).

Just a couple comments if you're interested...feel free to ignore if you're not. :grinning:

Good points, I just have them as generic code I use, but yea I understand they aren't really needed in this driver

what about this?