Subscribing to virtual devices

Hey, i’ve modified my post so it is a little simpler…

I have created a virtual switch in my SmartApp, and I now want to subscribe to its ‘on’ event. Could someone help me in how I do this? I used the following code to create my device:

def d = addChildDevice("ibeech", "Virtual Pi Relay", "relay01", theHub.id, [label:gpioName1, name:gpioName1])

And now i want to subscribe to relay01s ‘on’ event in my SmartApp.

Thanks

You would subscribe to it the same way you would with a physical switch.

subscribe (ibeech, “switch.on”, “Handler”)

Does that not work?

Sorry, I’m new to development with SmartThings, I’ve not subscribed to events from other devices, or subscribed to anything before :smile:)

I added the event handler, as you suggested, but when i turn the virtual switch on, no event is received…

The preferences section of my SmartApp does not contain the virtual switch, as it is created by the SmartApp, so my preferences looks like this:

section("Raspberry Pi Setup"){
  input "piIP", "text", "title": "Raspberry Pi IP", multiple: false, required: true
  input "piPort", "text", "title": "Raspberry Pi Port", multiple: false, required: true
  input "theHub", "hub", title: "On which hub?", multiple: false, required: true
}

Here is the code in my SmartApp which creates the virtual switch, and subscribes to its events:

log.debug "Create a Virtual Pi Relay named $gpioName1"
def d = addChildDevice("ibeech", "Virtual Pi Relay", "relay01", theHub.id, [label:gpioName1, name:gpioName1])
subscribe(ibeech, "switch.on", switchOn)
log.debug "Subscribed to gpio1: " + d;

And here is the callback function:

def switchOn(evt){

    log.debug "Switch on event!";
    log.debug evt;
}

And the log output:

09:34:04: debug Subscribed to gpio1: Light
09:34:03: debug create a Virtual Pi Relay named Light

I would really appreciate any help

Are you using the standard ST virtual switch device handler or a custom one you created? Did you verify if you look at the virtual switch’s event log in the IDE that there is an on event actually sent

I’m using a custom device handler (it is called ‘Virtual Pi Relay’, which you can see I use in the addChildDevice method). When i turn the switch on, i get the log line expected… Should i be using the ‘Simulated Button’ device?

metadata {
definition (name: "Virtual Pi Relay", namespace: "ibeech", author: "ibeech") {
	capability "Switch"
}

simulator {
	// TODO: define status and reply messages here
}

tiles {    
    
	 standardTile("switch", "device.switch", width: 1, height: 1, canChangeIcon: true) {
		state "on", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#79b821"
		state "off", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff"
	}

	main "switch"
	details (["switch"])
}
}

// parse events into attributes
def parse(String description) {
log.debug "Virtual switch parsing '${description}'"
}

def on() {
log.debug "Executing 'on'"	     

sendEvent(name: "switch", value: "on");    
}

def off() {
log.debug "Executing 'off'"
    
sendEvent(name: "switch", value: "off");   
}

That looks correct. I’m not very familiar with creating child apps, but figured I’d at least make sure your device was creating the event.

Under location -> installed smart apps, if you click on the smart apps name, you can check to see if it actually did subscribe to the child device’s event

Thanks Kevin,

Curiously, it looks like the event subscription did not get created!

ah ha, I had to change it to this:

    def d = addChildDevice("ibeech", "Virtual Pi Relay", "relay01", theHub.id, [label:gpioName1, name:gpioName1])
    subscribe(d, "switch", switchChange)

My event handler is now present in the event subscriptions list, and flicking the switch does trigger the event in my SmartApp!

Now I just need to figure out how to parse the event data :smile:

Check this doc out Subscribe to All Device Events

subscribe(theSwitch, “switch”, switchHandler)

def switchHandler(evt) {
if (evt.value == “on”) {
log.debug “switch turned on!”
} else if (evt.value == “off”) {
log.debug “switch turned off!”
}
}

Thanks man, I got it!! its working

http://cdn.meme.am/instances/500x/62036636.jpg

I jusut sent the event as ‘17.on’ which I can then parse out when I receive the event, then I send another event which is just ‘on’ so the switch is in the expected state. All looks to be working perfectly :smiley:

Thanks for your assistance all.