Reverse Momentary Button Device Type

Hi Folks … I looked without any success for a reverse logic momentary button that is always on until you press it and then it is off briefly and then back on. I needed this because I need to tell Alexa to turn off my TV using a momentary button. Without this I have to tell it something that doesn’t sound like English like… “Alexa, turn on TV off” which is stupid. So I wrote this device type which simply reverses the logic of a virtual momentary button. Then I assign it to a Harmony action that turns all entertainment devices off. Here is the code for the device type. To use this just make a new device and assign this type, and then write your smart app referencing it. Don’t forget to add it to Alexa and do the Discover New Devices thing. Enjoy.

/**

  • 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.
  • Momentary Off Button
  • modified Momentary Button Tile with reverse logic
  • Author: Ken Washington - adapted from SmartThings
  • Date: 2013-05-01
  • Date: 2016-03-31
    */
    metadata {
    definition (name: “Momentary Off Button”, namespace: “kewashi”, author: “Ken Washington”) {
    capability “Actuator”
    capability “Switch”
    capability “Momentary”
    capability “Sensor”
    }

// simulator metadata
simulator {
}

// UI tile definitions
tiles {
standardTile(“switch”, “device.switch”, width: 2, height: 2, canChangeIcon: true) {
state “on”, label: ‘Push’, action: “momentary.push”, backgroundColor: “#53a7c0
state “off”, label: ‘Push’, action: “momentary.push”, backgroundColor: “#ffffff”, nextState: “on”
}
main “switch”
details “switch”
}
}

def parse(String description) {
}

def push() {
sendEvent(name: “switch”, value: “off”, isStateChange: true, display: false)
def i=1000
def y = 0
while(i) {
def x = 1.23 / 5.2
y = y+ x
i–
}
sendEvent(name: “switch”, value: “on”, isStateChange: true, display: false)
sendEvent(name: “momentary”, value: “pushed”, isStateChange: true)
}

def on() {
push()
}

def off() {
push()
}