HA to trigger Alexa routine

I don’t know if I’m barking up the wrong tree with this but I thought I’d chuck it in anyway. I have a couple of Amazon smart plugs that aren’t compatible with HA.

I’m wondering if there’s a way of triggering an Alexa routine through HA somehow using the Alexa integration.

The 2 plugs control a couple of lamps. All my other lamps are Hive or Tradfri and I’ve got an automation running to turn them all on 30 minutes before sunset. But Alexa doesn’t support sunset/sunrise, only absolute times, which is pretty useless.

I’ve been trying to think of a way round it but I’m lost at the moment. I’ve only just come to HA so I’m not even sure about the questions to ask. If anyone has any thoughts, I’d love to hear them.

So far im not aware of any implementation that allows you to trigger alexa routines or commands directly.
If you have more than 1 echo device and they are within listening range of each other, you could try a workaround by letting one of them play the command (ie. saying: Alexa, turn on the switch) and the 2nd one will capture this command and execute it.
I hope in the near future there will be also direct access to routines and commands via an API.

If the smartplugs are also compatible with Google Assistant you could try the GA Webserver

Or if you are using hassio you can try the Google Assistant Service Addon:

This will allow you to send commands via text that will then be executed. In my experience this works pretty well for lights and switches etc.

At the moment, I think the Amazon plugs will only work with Alexa. There’s no IFTTT or anything, which is a bit annoying.

I like the idea of triggering one device with another. I might give that a try as I’ve just replaced a Dot with a Show and the Dot is now spare. I could set that up next to the Echo in the lounge and see if it works.

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