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

I am missing something, I have read multiple threads where individuals are now using their motion sensors or virtual switches status to trigger Alexa to speak a specified message from a routine. I can't for the life of me figure out how to make this happen.

I have updated the skill, I have virtual switches running in routines that turn on/off the switch that triggers rules etc... However, I want to be able to have an Alexa routine speak when motion is detected or certain virtual switch states change within HE.

Any assistance you can provided would be greatly appreciated.

1 Like

It would be setup in the routines in the Alexa app. Motion and contact sensor support was added a little while ago. There have been reported issues with routines in the Alexa app the last few days. I have no idea of the specifics or if they even still exist.

Expose a motion sensor in the Alexa Skill and you should be able to just use it. For a switch to trigger, a regular virtual switch isn't supported by AWS. Only motion sensors and contact sensors are. So you need to use this simple custom code.

Copy this code from Chris Wilson @cwwilson08 and create a new virtual device using this driver. Then expose it to the Alexa Skill. It will mimic a motion sensor output when a switch is digitally turned on within HE

metadata {
	definition (name: "Virtual motion with Switch", namespace: "cw", author: "cwwilson08") {
		capability "Sensor"
		capability "Motion Sensor"
        capability "Switch"
	}   
}

def on() {
    sendEvent(name: "motion", value: "active")
    sendEvent(name: "switch", value: "on")
    runIn(12, off)
}

def off() {
    sendEvent(name: "motion", value: "inactive")
    sendEvent(name: "switch", value: "off")
}

def installed() {
}
3 Likes

That was an app problem. It's fixed with an update on iOS. Not sure if the Android fix has rolled out yet.

Yep I had to update. Wouldn't save the routine until I updated the Alexa app.

All is working now.

I'm using virtual contact sensors for everything.

When I access the Alexa app on my phone, I choose "When this happens" from the routine creation I do not see my motion sensor, I have added it to the skill

Normally it will show up immediately. However you may need to do another device discovery to make them show up. If it's in your list of devices, it's available to the routine.

Ok thank you, I'll try again tonight

1 Like

Works, thank you. I have been a complete fail at getting alexacookie.js to work for me on the pi. Manually renewing the cookie was getting tedious. No longer a need with this.

No doubt about that. But, there is a really simple fix for this issue. Simply restart AlexaCookie.js once a day. It doesn't even have to be reconfigured.

This fix was discovered by @dan.t who reported it here. @gabriele, who wrote AlexaCookie.js, independently discovered the same thing as indicated two messages after @dan.t's in the same thread.

I've been doing this using a nightly cron job for the last month - works flawlessly.

FWIW, I removed the line "runIn(12....)". That makes the Alexa routine easier. The switch in Hubitat starts motion which triggers one Alexa routine. Turning the switch off stops motion which triggers another routine. With that line, motion stops automatically in 12 seconds.

I created a virtual switch that triggers a virtual contact sensor to trigger a routine in Alexa … I don't know why Amazon limits "SmartHome" triggers to motion and contact events.

2 Likes

A very good point. I've left requests with Alexa Support a couple times requesting the capacity to use switches.

A little while ago, I created a "Virtual contact with switch" driver (pasted below) using @cwwilson08's "Virtual motion with switch" pasted above.

This allows a single virtual device to be accessed as a switch at the Hubitat end and a contact sensor at the Alexa end. You might find it useful.

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() {
}
9 Likes

Thanks … I'll have to check it out. Still have about 30 devices to finish migrating (plus a few new ones to add!) Looks like Christmas is Cancelled and I'll be working on Rule Machine over the Holiday break to tweak everything!

1 Like

Good luck!

Thanks aaiyar, I used the @cwwilson08 code for "Virtual motion with switch" to trigger an Alexa routine from my tablet. It worked great.

I am not sure how you control a "virtual contact with switch" with a "virtual motion with switch".

Are you setting up a Rule to do this?

1 Like

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

Yup.