Trigger Alexa Routine on Virtual Switch - not working

This could be be me missing something, as I have also found some similiar discussion here

Im trying to have Alexa trigger a routine when a Virtual Switch changes state. It just doesnt work. I need it to be a switch and not button (ie Virtual Alexa Button) as I need to know the state.

The switch is selectable as a WHEN action as well as On/Off state. It just doesnt trigger.

Verified that Alexa Routine doesnt honor the switch change via either Alexa UI or HE.

What Ive tried

  • Both on/off Triggers
  • Routine works manually activating it
  • The switch is controllable via Alexa UI, HE sees it change.
  • Alexa sees the change of state when changed in HE

Ive also added the switch the an Alexa Group (link above), but that doesnt appear as a WHEN action in the Alexa Routine. What I havent done yet is add the switch to a HE group and trigger off that.

Am I missing something?

There was a similar discussion SmartThings

Alexa routines cannot be triggered by a switch. The trick is to use a virtual device that has both contact and switch capabilities. Then you can treat that same device as a contact when you want to (in your Alexa routine), and as a switch when you want to (everywhere else).

Hubitat/Virtual Contact Sensor with Switch.groovy at master ยท stephack/Hubitat ยท GitHub

3 Likes

This also works. It's a virtual contact sensor that acts a switch on hubitat. I use it quite often

/**

  • Virtual Motion Sensor with Switch
  • Copyright 2020 Ernie Miller
  • Permission is hereby granted, free of charge, to any person obtaining a copy
  • of this software and associated documentation files (the "Software"), to deal
  • in the Software without restriction, including without limitation the rights
  • to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  • copies of the Software, and to permit persons to whom the Software is
  • furnished to do so, subject to the following conditions:
  • The above copyright notice and this permission notice shall be included in
  • all copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  • IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  • FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  • AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  • LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  • OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  • SOFTWARE.
    */

metadata {
definition(name: "Virtual Motion Sensor with Switch", namespace: "ernie",
author: "Ernie Miller") {
capability "Actuator"
capability "Motion Sensor"
capability "Sensor"
capability "Switch"
}

preferences {
input name: "inactiveSeconds", type: "number",
title: "Inactive Delay",
description: "Seconds until switch/motion toggles off (1 - 120, 0 = disable)",
range: "0..120", defaultValue: 10, required: true
input name: "logEnable", type: "bool", title: "Enable Logging",
defaultValue: false
}
}

def installed() {
off()
}

def on() {
if (logEnable) { log.info "$device.displayName active" }
sendEvent(name: "motion", value: "active")
sendEvent(name: "switch", value: "on")
if (inactiveSeconds > 0) { runIn(inactiveSeconds, "off") }
}

def off() {
if (logEnable) { log.info "$device.displayName inactive" }
sendEvent(name: "motion", value: "inactive")
sendEvent(name: "switch", value: "off")
}

2 Likes

Thanks this makes sense !

copy and paste it into user driver

@rlithgow1
Thank you. I just found this and it works great

1 Like

Maybe things have changed, but I have found that creating a virtual contact sensor in HE works just fine - Alexa recognizes it and it can be used to trigger Alexa routines. I have been using it to trigger pre-determined phrases thru echo speakers. I have Alexa trigger the routine on open. Then I simply open the virtual contact sensor and then immediately close it (important to close it so it will work next time around) in my rule actions in HE. Would be awesome if Alexa would recognize virtual buttons, but sadly it doesn't, at least today. Enjoy!

Hi, I have a question, I have a driver installed and it pulled devices from smartlife api... it created generic devices (child devices) and I would like to add the contact sensor capability to the generic switch. Actually, I would like to use those switch as alexa trigger so I would like to add the contact sensor to it but it would be only to follow the switch state (switch on = contact open, switch off = contact closed). I tried to add the send event for contact state inside the switch on block but it did not work so I was wondering on how could I do it? Thanks!

I would talk to @kkossev about the smartlife stuff in the Smartlife thread

A suggestion for you- Create a virtual contact/ switch device and use the built-in Mirror app to tie your Smartlife switch and the virtual switch together. It really doesnt add too much latency to the automation and you wont have to remember those code changes you made if there is an update by the developer.

1 Like

Thanks for this code. I struggled to figure out how to trigger Alexa routines and was doing it using IFTTT and Voice Monkey. WAAYY too complex. This works great. I changed the Name to "Alexa Trigger Device" so that I would easily be able to pick it out when I am setting up a virtual device to trigger an Alexa routine.

Has anyone been having issues with Alexa triggering on this? I have one of these contact sensors that Alexa sees, she then turns on a Alexa-Enabled sprinkler timer. The turn on works almost 100% of the time. She seems to miss the turn off every other time.

I dont think I had this issue last season, and this is the only crross-integration I have -- but she def. seems to be struggling this season

Your custom driver code did the trick for me! The HE built in virtual contact sensor will be exposed in Alexa but your custom driver adds the necessary delay to automatically toggle off. Thank You!

Not mine, it was @kkossev

1 Like