What's wrong with my sendEvent?


(Dan) #1

I am trying to build a virtual “push button” switch for Alexa. Why is the code below not sending the button.pushed event when the switch.on() function is called? Is there some relation between what events the system will allow and the UI tile definition that I don’t understand?

I see my log message from the on() function and the device behaves as expected, with the exception of not sending the pushed event.

metadata { definition (name: "Virtual push button for Alexa", namespace: "qqq", author: "qqq") { capability "Actuator" capability "Switch" capability "Button" capability "Sensor" }
// simulator metadata
simulator
{
}

// UI tile definitions
tiles {
	standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
		state "on", label: 'On', action: "switch.on", backgroundColor: "#53a7c0", nextState: "nc"
		state "off", label: 'Off', action: "switch.off", backgroundColor: "#222222", nextState: "nc"
		state "nc", label: 'Push', action: "switch.on", backgroundColor: "#ffffff"
	}
    
	main "switch"
	details "switch"
}

}

def parse(String description)
{
}

def on()
{
log.debug "Pushing $device.displayName"
sendEvent(name: “button”, value: “pushed”, descriptionText: “$device.displayName was pushed”)
sendEvent(name: “switch”, value: “on”, isStateChange: true, display: false)
sendEvent(name: “switch”, value: “nc”, isStateChange: true, display: false)
}

def off()
{
sendEvent(name: “switch”, value: “off”, isStateChange: true, display: false)
sendEvent(name: “switch”, value: “nc”, isStateChange: true, display: false)
}


(Dan) #2

The event sends if I use the following:

sendEvent(name: "button", value: "pushed", isStateChange: true, display: false)

(Dan) #3

The code below works for “Alexa, turn OFF …” but when I say “Alexa, turn ON …” I get the sad tones…

What gives?

def on() { sendEvent(name: "switch", value: "on", isStateChange: true, display: false) sendEvent(name: "switch", value: "nc", isStateChange: true, display: false) sendEvent(name: "button", value: "pushed", isStateChange: true, display: false) }

def off()
{
sendEvent(name: “switch”, value: “off”, isStateChange: true, display: false)
sendEvent(name: “switch”, value: “nc”, isStateChange: true, display: false)
sendEvent(name: “button”, value: “pushed”, isStateChange: true, display: false)
}


(Dan) #4

Dagnabbit! I called the switch “Flasher” which looks like it’s too close to Alexa’s Flash keyword. When I renamed it, everything eventually started working. And it’s funny that off works but on not…

I wish Amazon would fix that. I.e. if you use a HA keyword (on, off, etc) then FIRST look to match the list of devices, THEN match everything else. It will get only worse as Alexa learns more keywords…