Setting state of another virtual device?

Hi,

I am sorry if this has been discussed before and I am just searchwise challenged but I really haven’t managed to find a solution to my problem. So here it goes, feel free to direct me to “this is discussed thousand times before” thread.

Question is - can I set the state of the device from another device handler or smartapp? Rough description of my goal:

  1. I have a device handler for BroadLink device that is defined as capability.Switch
  2. I have a device capability.contactSensor
  3. I want to have the state of the Switch to change to On when the contactSensor state turns to Open WITHOUT running Switch.on() method, ie just change the state without doing the actual stuff that is done when the switch is turned on (sending the corresponding BL command)

Basically I want to achieve the situation where user has the option to turn OFF the switch, which seems to be in open state because of contactsensor being open.

Writing this makes me feel like a total noob, it should be the most simple thing to do but for some reason I can’t get the pieces together.

I think it partially depends on the Device Handler and how it uses state. Some will automatically send a command when the state is changed, so if you change the state, the command will occur. Others do not do this. For those, a Smart Ap or Service manager can call a child device routine that could set a state. Some code that would do that (used in a Service Manager):

            def child = getChildDevice(YourDeviceDNI)
            child.setRemoteState(YOURSTATE)

This will set the state. You can really do any function within the device handler - set state, turn on or off, do a refresh, any command. You just have to have a routine to call, like:

 def setRemoteState(YOURSTATE) {
        sendEvent(name: "switch", value: status, isStateChange: true)
 }

good luck.