Code Help - Web Services Tutorial Adaptation - Lights turning on randomly

The Web Services Tutorial assumes that you want to manipulate all of the lights you give it access to simultaneously. I don’t want to create a new SmartApp for each light or switch I have, so I’m working to adapt it to take an additional parameter to set the device.

The following works, but I’m having a strange issue. One of the lights that the SmartApp has permission to act upon will turn on and turn blue seemingly randomly. They are Philips Hue bulbs, if that is relevant to the discussion. Are there any obvious problems with this code or do I need to go up the stack to find the answer? Thank you for your help!

mappings {
  path("/switches") {
    action: [
      GET: "listSwitches"
    ]
  }
  path("/switches/:device/:command") {
    action: [
      PUT: "updateSwitch"
    ]
  }
}

void updateSwitch() {
    // use the built-in request object to get the command and device parameters
    def command = params.command
    def device = params.device
    
    // get the index of the device in switches
    def n = switches.findIndexOf { it ==~ device }
    log.debug(n)

    // execute the command on the given switch
    switch(command) {
        case "on":
            switches[n].on()
            break
        case "off":
            switches[n].off()
            break
        default:
            httpError(400, "$The switch does not exist")
    }
}