Subscribe to routine that doesn't exist

By accident I deleted a routine in a app. But the subscripton was still there. But was puzzled that I didn't get an error in the logs when the app was initialized or when the routine would have been called.

Can you share more specifics about what you did and what happened?

Without knowing more, it sounds like a your app had a method you deleted that was used as a callback, subscription handler, scheduled job, or similar. You won't get an error just for saving the app in such a case, but you would indeed get a runtime MissingMethodException if it was ever actually called. But knowing what you were really doing would give us a better idea of what may have happened in your case.

It was an app that had been working for some time. I added a couple things, but most of the original remained the same.

I had an input for a switch. Then a subscription for that switch. When the switch came on it was to execute a routine that turned some other stuff on. All that had been in place and was not changed.

What I inadvertently did was delete the routine that the subscription called while I was adding the new stuff. I then opened the app to enter the new stuff and hit update then done. Log showed the update as normal.

The first time I triggered the switch the other stuff didn't come on. Could not figure out why and was going to add some debug statements when I saw the routine wasn't there.

So was wondering why when I did an update-done it didn't give me an error saying it couldn't subscribe to a non-existent routine. Or the first time the switch triggered I should have received some kind of error that the subscription routine wasn't there.

It's no biggy, just wondering. Would have saved me some time if I had got the error in the first place.

I threw this app together to test what is happening. Install it, trigger a switch and it works. I then deleted the testHandler routine. Hit done on the app and triggered it again. Didn't run obviously, but no errors in log.

EDIT: Further testing shows if I install an app with a subscription to a non-existing routine it doesn't error at all. So it's not just deleting an existing one. I would think if you try to subscribe to a non existing routine it should throw an error.

/**
 *  App Template
 *
 *  Copyright 2025 Jim White
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Test Routine",
    namespace: "jwwhite001",
    author: "Jim White",
    description: "Test Routine",
    category: "My Apps",
    iconUrl: "",
    iconX2Url: "",
    iconX3Url: "")


preferences {
		section("Inputs") {
            input "testSwitch", "capability.switch", required: true, multiple: true, title: "Test Switch"
        }
}


def installed() {
	log.debug "Installed with settings: ${settings}"
	unsubscribe()
    unschedule
	initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"

	unsubscribe()
    unschedule
	initialize()
}

def initialize() {
    subscribe(testSwitch, "switch", testHandler)
}

def testHandler(evt) {
    log.info "Routine Called"
}

I am not able to reproduce any problems. I get the expected error, which I described above, when the handler is set but the method (which I assume you mean with "routine") does not exist:

You will not get an error just for specifying a method name that does not appear to exist at save/compilation time, so that much is normal.

I don’t get that error when I trigger the switch and the handler doesn’t exist. I get nothing.

Do you still have the subscription on the App Status page? Can you reproduce after a database rebuild (or, just for testing, plain soft reset)?

Here is what I think im seeing. If the handler doesn’t exist at install or update the subscription is never created. Then if the switch is triggered it just doesn’t do anything or throw an error.

If the handler exists at install then the subscription is created. Then if you delete the handler but don’t update the app you get an error.

I would think the error should be thrown in either case.

Pardon if my nomenclature is incorrect. Don’t always know the proper terminology.

OK, I am able to reproduce that with your code, the underlying issue being that the event subscription does not get created.

You will get the error you want, or at least the one above, if you use the documented method for creating subscriptions, specifically by changing your call to this:

subscribe(testSwitch, "switch", "testHandler")

Note that the signature specifies the handler name by string value.

1 Like

Well that sorta solves that. Seems strange that not putting it in quotes will still do a subscribe. But I guess some things are the way they are for a reason.