Use a Motion Sensor or Virtual Switch to trigger an Alexa 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.

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

Download the Hubitat app