Virtual Devices - How To?

I feel like I'm not being very efficient in my automation. I feel like a bit of a dolt when it comes to virtual switches/devices, etc., and need to understand this and the use cases around this better. Can anyone point me to beginner docs/video's, etc. to assist in getting up to speed with them? I don't think there are any in the Documentation folder here. I don't really use RM yet, mostly just Simple Automation Rules (need to use Basic Rules more), and user/built-in apps that are out there.

I have some wifi devices connected through Amazon Echo, that I would like connect to various motion sensors, tie to modes, etc. I took to heart the usage of more automation rather than just pushing buttons or telling things to turn on.

No real user docs on this subject that I'm aware of, but a sample use case may help. Let's say you have a wifi plug in Alexa that you want to automate/control with HE. In HE create a virtual contact with switch capabilities (sample code below) and share it using the Alexa Echo Skill app. Go into the Alexa app and create two routines, one that turns on the wifi plug when the virtual switch is turned on in HE, and one that turns off when the virtual switch is turned off. For any automations that you want to control the wifi plug, use the virtual switch as its proxy.

"Virtual Contact w/Switch Driver Code
metadata {
	definition (name: "Virtual contact with Switch", namespace: "cw", author: "cwwilson08") {
		capability "Sensor"
		capability "Contact Sensor"
        capability "Switch"
	}   
}

def on() {
    sendEvent(name: "contact", value: "closed")
    sendEvent(name: "switch", value: "on")
}

def off() {
    sendEvent(name: "contact", value: "open")
    sendEvent(name: "switch", value: "off")
}

def installed() {
}

That helps. I'll take more of a look at that.

The one i was looking at this morning was this use case:

  1. Wifi power strip in Alexa (Alexa Device type - plug)
  2. Hue motion sensor in HE

Motion on - turn on power strip, motion stops for 30 minutes, turn off power strip.

Perfect use case, Have a Simple or Basic Rule that uses motion on as the trigger, have it turn on the virtual switch, wait for 30 minutes and then turn off the virtual switch.

Devices->Add Virtual Device
Type: I only see the virtual contact sensor, not one with switch capabilities.

Where is the code entered at in the device? I know, likely basic question. Thanks for your help on this.

Copy the code from above, then go to the Drivers Code tab, create a new driver, paste in the code and save - will appear at the bottom of your drivers list when adding a new device.

1 Like

Doesn't appear to be triggering. Here's what I have:

I don't see any State variables in your "Virtual contact with Switch". Manually turn it on, and then off. You should see Contact OPEN and then CLOSED. If you don't see that, there's something wrong. Your code wasn't pasted correctly or something was missing when you copied it.

Poor Copy/Paste.

The on command changes the state:on and contact:closed, and they flip to state:off and contact:open pretty quick. I say this as I'm sitting in that room, and the motion sensor likely see's me here.

Cool. So now what you do is in the Alexa app, you create a routine and you tell it when that contact device is closed, Turn ON your plug, and a second Alexa routine for when the contact is open, turn OFF your plug

I think in Alexa you can define the device as a light. Then you can just say "Alexa, turn on plug" (or off) I've done this with a power outlet in the kitchen named "Waffle"