Use a Motion Sensor or Virtual Switch to trigger an Alexa Routine

Hi @markj - welcome to Hubitat. I probably wasn't clear in what I wrote. You don't control one with the other.

It's an alternative for the use case described by @Tim-in-Ca, who triggered an Alexa routine by using a virtual switch that changed the status of a virtual contact sensor (which triggered the routine). This single virtual device (virtual contact sensor with switch) can do both - meaning one would have to create a single virtual device on HE instead of two.

Got it, Thanks.
That worked perfect.

1 Like

What’s cool about this is a switch and sensor both show up in Alexa. I have Simple Lighting rules which change the switch and an Alexa routine triggers on the sensor to control my WiFi outdoor lights. A switch also shows up under devices in Alexa if you want to use that.

1 Like


So when I toggle the switch in Alexa, it must cause the sensor to toggle in HE, when then triggers my routine in Alexa. Don’t know if this would ever be useful but pretty cool!

1 Like

So I realized in this response that a virtual switch has an enable auto off setting. Is there any way to do this with your custom driver?

Sure, it's easy to do that. Here's a modified driver that optionally turns off in 0.5 seconds:

metadata {
	definition (name: "Virtual contact with auto-off Switch", namespace: "cw", author: "cwwilson08") {
		capability "Sensor"
		capability "Contact Sensor"
        capability "Switch"

preferences {
        section {
		    input (
                name: "AutoOff",
                type: "bool",
                title: "Enable auto off", 
                required: false, 
                displayDuringSetup: false, 
                defaultValue: false

def on() {
    sendEvent(name: "contact", value: "closed")
    sendEvent(name: "switch", value: "on")
    if (AutoOff) {
        runInMillis(500, off)

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

def installed() {

After installing this, just turn on the option "Enable auto off" and save preferences.

Just so there's no misunderstanding or incorrect attribution. This driver isn't mine. It is modified from a Virtual Motion with Switch driver posted originally by @cwwilson08.


Thanks @aaiyar! I'm going to try this when I get home. My current driver is actually a modification of your modification :joy:

I'm a software architect and wondering if there is any good documentation on writing your own HE drivers?

I'm not a programmer at all, but I use the SmartThings Classic Developer Documentation.

Hubitat-specific developer documentation is available here:

Perfect! Thank you!

Thank you @aaiyar.
Simple, elegant and therefore brilliant!

Helped to solve my problem of handling more complicated conditional logic in Hubitat that Alexa can’t manage, to trigger Alexa routines controlling non-Hubitat compatible (but Alexa compatible) lighting controllers (with the added benefit of annunciation via Alexa for security system announcements.

Thanks again for sharing!


I have tried using a virtual switch on the hubitat which closes when a plug comes on.... this works at the hubitat level ...the routine in alexa sees the virtual switch on the hubitat close and should launch music as per the routine i have made -
it doesnt what could be wrong?
Checked independently i can see the virtual switch close

Hello - welcome to Hubitat! What you're trying isn't entirely clear to me, because the driver for a physical device (like a z-wave contact sensor) cannot be assigned to a virtual device.

So perhaps I haven't understood you correctly. Could you describe exactly what you've done at the Hubitat end? And separately describe your Alexa Routine?

What this all started with is I wanted Alexa to play music when a plug was switched on

Alexa doesn’t seem to support sensing whether the plug is on or off as a “ when “ event.

So I thought hubitat does it I could have the "plug on" activate a virtual switch that would be sensed by Alexa.

Actually there are two things I tried

  1. I have a virtual switch ( vbuttonsensor) being turned on when a plug ( bathroom light plug) switches on

This virtual switch "open close" is sensed by the Alexa if I check in devices .

Within the routine when I want this "open or close" to play music it doesn’t do anything.

  1. I have a go control door sensor which I wired with an external press button switch having set up a virtual switch which I can see operates when I press the go control to close position.Even in Alexa I can see the virtual button as being put on and off. But when included in an Alexa routine this particular virtual button ( operated by the go control sensor) doesn’t run the routine …. Trying to put on a smart plug.

A similar scenario with a Samsung button - to virtual button - to Alexa runs a routine .

Don’t know whether I clarified what I was doing or have confused it even more :slight_smile:

I think this is the issue. Alexa does not permit the use of a Hubitat virtual switch to trigger a routine.

So instead, create a Hubitat device with the "Virtual contact with Switch" driver that is posted here:

This virtual device acts like a switch for Hubitat and a contact sensor for Alexa. Now use the state of this virtual contact sensor in Alexa to trigger your routine.


Ok using the "Virtual contact with switch driver" I managed to get a virtual switch on habitat activated by an actual plug coming on. the virtual switch detected is then in an Alexa routine to play music.
It now plays music when the plug comes on ( this plug is being activated by by motion sensor on the flex) So motion sensor to "plug on" to "virtual switch close" to routine to play music on the flex)
A peculiar thing I noticed ... I have two amazon Accts this works only on one of them. On one account the routine does not execute. Any thoughts on what or why this happens ( am using two separate phones one pixel 4 for the act that works and a pixel 2 for the amazon account that doesn't) I tired this on the same flex device switched accounts and it just worked.

1 Like

Which driver did you get to work? I am not having any luck getting alexa to see the transition.

This is probably the one he is using...


Love this! I had been beating my head against a wall over getting alexa to see my ST motion sensor

1 Like

Got it, works!


Download the Hubitat app