HA to trigger Alexa routine

Which Plugs are you using?

One of these.

Yes, seems to be Alexa only… which makes sense seeing the Amazon Brand on it :wink:
If you have some time you could check if you can find the commands used to turn on & off via Wifi using wireshark.

Yeah, I just struggle to see why a company/platform that relies on loads of manufacturers playing nicely with them can have a product that works exclusively with Alexa.

I’ll have a look at Wireshark though. Thanks!

Ah, just realised. If I could set a routine on my Dot to announce “Alexa. Switch the light on” and I could tell it to do that at sunset then I’d also be able to just set a routine to turn the light on at sunset.

This is similar to “If we had some bacon we could have bacon and eggs if we had some eggs” :slight_smile:

1 Like

I just had an idea… you can create a simulated device (a switch or motion sensor) with HA and give alexa access to it. Then you create a routine, that switches the light on/off based on the status of this dummy device. And then you just switch the dummy device in you HA.

I said motion sensor in the above because for in my Alexa App, Locks & Motion Sensors are the only devices available for a routine trigger.

This sounds interesting.

I just looked at routines in Alexa and when I went into the “If” bit and selected device, it said there were no supported devices. I guess that’s where I would see it?

Not sure about the simulated device in HA. I’ll have to have a look at that.

Basically you would have to find a way to get your HA devices into Alexa. There are several ways possible.
One of them would be the HA Cloud (paid). Then you would just create a device i.e. a lock in your config.
You could use the MQTT Lock for example:

I’m still on the trial for cloud so I just added the skill to Alexa and it picked up 40 ish devices. I’m just following the tutorial for the simulated sensor. I added one in with just the basics but Alexa didn’t find it. I’m adding in the humidity one now so I’ll know if a few minutes.

Then I just need to work out how to make HA trigger the dummy sensor at sunset

Well Alexa picked up all my Hive sensors but not the simulated one.

I think I have the US version of the Amazon Smart Plug. I got creative to get it working with HA. I used the Samsung SmartThings free cloud service even though I do not own a SmartThings hub. I am using Hassio & DuckDNS / LetsEncrypt to expose my HA instance to the Internet.

I set up a virtual device handler that is both a binary switch & a sensor. Some people call this an “Alexa Switch”. When the switch is triggered, it triggers the sensor. an Alexa routine controls the smart plug depending on the state of the sensor. I then created a virtual device based off that handler.

so, HA → SmartThings → Alexa

I modified code I found online for the “Alexa Switch” Here it is.

/**
 *  Copyright 2015 SmartThings
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
metadata {

    definition (name: "Alexa Switch", namespace: "bjpierron", author: "bjpierron", runLocally: false, mnmn: "SmartThings", vid: "generic-switch") {
        capability "Switch"
        capability "Relay Switch"
        capability "Sensor"
        capability "Actuator"
        capability "Health Check"
        
        capability "Contact Sensor"

        command    "markDeviceOnline"
        command    "markDeviceOffline"
    }
    
    simulator {
		status "open": "contact:open"
		status "closed": "contact:closed"
	}

    tiles {
        standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
            state "off", label: '${currentValue}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
            state "on", label: '${currentValue}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#00A0DC"
        }
        standardTile("deviceHealthControl", "device.healthStatus", decoration: "flat", width: 1, height: 1, inactiveLabel: false) {
            state "online",  label: "ONLINE", backgroundColor: "#00A0DC", action: "markDeviceOffline", icon: "st.Health & Wellness.health9", nextState: "goingOffline", defaultState: true
            state "offline", label: "OFFLINE", backgroundColor: "#E86D13", action: "markDeviceOnline", icon: "st.Health & Wellness.health9", nextState: "goingOnline"
            state "goingOnline", label: "Going ONLINE", backgroundColor: "#FFFFFF", icon: "st.Health & Wellness.health9"
            state "goingOffline", label: "Going OFFLINE", backgroundColor: "#FFFFFF", icon: "st.Health & Wellness.health9"
        }
        main "switch"
        details(["switch","on","off","deviceHealthControl"])
    }
}

def installed() {
    log.trace "Executing 'installed'"
    markDeviceOnline()
    off()
    initialize()
}

def updated() {
    log.trace "Executing 'updated'"
    initialize()
}

def markDeviceOnline() {
    setDeviceHealth("online")
}

def markDeviceOffline() {
    setDeviceHealth("offline")
}

private setDeviceHealth(String healthState) {
    log.debug("healthStatus: ${device.currentValue('healthStatus')}; DeviceWatch-DeviceStatus: ${device.currentValue('DeviceWatch-DeviceStatus')}")
    // ensure healthState is valid
    List validHealthStates = ["online", "offline"]
    healthState = validHealthStates.contains(healthState) ? healthState : device.currentValue("healthStatus")
    // set the healthState
    sendEvent(name: "DeviceWatch-DeviceStatus", value: healthState)
    sendEvent(name: "healthStatus", value: healthState)
}

private initialize() {
    log.trace "Executing 'initialize'"
    sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
}

def parse(description) {
}

def on() {
    log.debug "$version on()"
    sendEvent(name: "switch", value: "on")
    sendEvent(name: "contact", value: "closed")
}

def off() {
    log.debug "$version off()"
    sendEvent(name: "switch", value: "off")
    sendEvent(name: "contact", value: "open")
}



private getVersion() {
    "PUBLISHED"
}

I’m trying to control Alexa-only devices from HA (Zigbee switches connected to an Echo Plus) by exposing a virtual switch in HA to trigger an Alexa routine. I’ve created Alexa routines to turn my Zigbee switch on/off and HA scripts to call the alexa player media player service to invoke these routines. But I can’t figure out how to configure HA to treat the scripts as a virtual switch. You mentioned in the post below that you are able to do something similar without having to go through SmartThings. What was your solution?

https://community.home-assistant.io/t/home-depot-hampton-bay-zigbee-fan-controller-in-smartthings/115500/2

There is an Alexa integration for ha , you can start routines with it. Works great! Its called alexa media player or something like that…

I do not have my confi available but, yes, Alexa Media Player can do text-to-speech and trigger routines. I had am Amazon Plug I used to control plant grow lights. Here s roughly what I did

2 Alexa routines (on/off) “Plant Light On”, etc.
alexa media player configured for the routines
2 scripts to trigger the routines
A template switch that called the proper script for on & off.

Alexa Media Player just sends the appropriate text to one of your Alexa devices to trigger a routine.

This solution still works or maybe Alexa media player is better?

can you help me to configure it? I have Alexa media player in hass.io witch works fine reg. to play stop music but have no idea what to do next to trigger routine from HA.

1 Like

Can you please share with a sample config that you use for alexa media player to invoke an alexa routine?..I able to control tts of alexa from HA alexa media player, so I know that link between alexa and HA is working, I can also play with asking alexa to tell a joke stuff like that from HA, but I can’t find out the right config to call an alexa routine…As you I have a couple of pieces of hardware that right now are only compatible with alexa and not HA (some Chinese curtain motors), they work perfectly from alexa app and I have alexa routines to control position, up, down, etc…so I think my only missing piece is to how trigger/call those routines from HA… using media player service seems the more easy way and I would love to know how you did it!..+
I think other approaches involve virtual switches, ssl and lambda functions that seems to complicated for what I need…
Please point me in the right direction!
Thanks in advance…

I ended up getting a Conbee II to integrate my Zigbee devices with HA so I no longer have these configurations. But have you checked this out? (especially the section about triggering a skill).

Thank you!, yes I was reading that one and then I find out this one that actually fix some minor issues…

That’s pretty much all what is need it to trigger a routine from HA.!!