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

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.

3 Likes

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:

https://docs.hubitat.com/index.php?title=Developer_Documentation

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!

2 Likes

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.

3 Likes

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...

3 Likes

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!

2 Likes

HI gikjll

Yes this is the driver I use ( the one mentioned by "aaiyar")

Also did you ensure that your virtual switch is listed in the Alexa echo skill on the hubitat.
without that the Alexa device doesn't see it though it works within the hubitat environment.

I'm not a programmer and I know very little about what I'm doing, but I essentially backwards engineered this. I had used the code listed earlier to create a switch in Hubitat that can be the trigger in an Alexa Routine. I'm also avoiding IFTTT since they went subscription. I'm using the trick of having Alexa recognize motion from my Ring Cameras and setting up a routine to trigger a switch on the HE side. The problem is that the virtual switch on the HE side wont trigger anything in HSM. So I took this code and basically switched the motion for switch language and it works. So the Ring Cameras trigger motion in Alexa and a routine flips a switch in Alexa but it acts as motion on HE's side.

I may be reinventing the wheel and I know it defeats the purpose of avoiding the cloud but I am happy that I can trigger motion in HE from Ring Camera motion and avoid IFTTT and any polling issues.

metadata {

                definition (name: "Virtual Switch with Motion", namespace: "tf", author: "tfaulkner319") {

                                capability "Switch"

                                capability "Motion Sensor"

        capability "Sensor"

                }  

}

 

def on() {

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

    sendEvent(name: "motion", value: "active")

    runIn(12, off)

}

 

def off() {

    sendEvent(name: "switch", value: "off")

    sendEvent(name: "motion", value: "inactive")

}

 

def installed() {

}
2 Likes

So I understand what the virtual motion with switch is supposed to do, but how do I get that virtual switch installed?

I installed the driver noted above. But then when I create a new virtual device, I don't see the option of selecting "virtual motion with switch" as the type? What am I missing?

User drivers are at the very bottom of the list.

3 Likes

Download the Hubitat app