How to pass parameters into a subscription event function

I’m trying to pass a parameter into a subscription function so it works like this:

def initialize()
{
subscribe(MySwitch1, "switch", SwitchHandler("some param"))
subscribe(MySwitch2, "switch", SwitchHandler("some other param"))
}

def SwitchHandler(evt, my_custom_param)
{
}

Anyone know how to accomplish this? I thought about creating an intermediate (like below) handler but that seems tacky and not very scalable. Also doesn’t work for floating point values… just tiny integers and enums.

def initialize()
{
subscribe(MySwitch1, "switch", SwitchHandlerA)
subscribe(MySwitch2, "switch", SwitchHandlerB)
}

def SwitchHandlerA(evt)
{
SwitchHandler(evt, "some param")
}

def SwitchHandlerB(evt)
{
SwitchHandler(evt, "some other param")
}

def SwitchHandler(evt, my_custom_param)
{
}

You can’t get there from here.

Subscribe just takes device/event/handler method, and handlermethod just gets called with the event.

What were you hoping to pass into the hander? And why wouldn’t your tacky version not work?

1 Like

Thanks John. My tacky fix does work. I’m a software engineer by trade so I’m really just wondering if a more elegant solution exists.

This bothered me too, but I haven’t been able to find a solution. Closures could fix this, but SmartThings seems to block closures for security purposes I think.

1 Like

[quote=“johnathan398, post:3, topic:17597”]
a more elegant solution exists.
[/quote]You could have the fan out fan in calls, and look up the method name of the calling method on the stack, and do different things in the fan-in method based on the caller. But that seems less clear than just doing it the other way.

What’s the purpose of the parameter? You can do a switch statement in the event handler based on the device name/id/type/event… if you just want a single handler

1 Like