Creating routine in Alexa app for a virtual switch

Alexa doesn't permit switches to act as triggers for routines. Only sensors (contact, motion, leak) and locks.

So you could make a virtual contact sensor in Hubitat and expose it to Alexa. And use activation of the contact sensor as the trigger for your routine. You could also use a simple virtual contact/switch driver like the one listed below, which can be triggered as a switch in Hubitat and use as a contact sensor to trigger an Alexa routine.

Virtual contact sensor with auto-off switch
metadata {
	definition (name: "Virtual contact with auto-off Switch", namespace: "aa", author: "aaiyar") {
		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() {
}
6 Likes