2-button virtual switch driver

I'm fairly new to Hubitat and nowhere ready to dive into developing device drivers. I'm looking for an Alexa virtual button driver similar to:

but that includes two buttons instead of one: turning switch ON would set button to 1 and turning it off would set button to 2,
or something equivalent. It would act as a switch and momentary button that Alexa can deal with.

I tried making a modified version of Mikee385's driver and failed miserably - the device seemed to work OK, but RM would choke during rule creation process.

I'm hoping this is a common enough requirement that this type of driver already exists. Does anyone know of one? Any help would be greatly appreciated.

This is the type of RM error I was getting:

> PMerrorgroovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: (java.lang.Integer) values: [5]
> Possible solutions: take(int), wait(), any(), wait(long), wait(long, int), charAt(int) on line 3302 (method doActPage)

Here was my attempt at the modified driver (I changed "number of buttons" to 2 and modified the ON and OFF methods to invoke push(1) and push(2) respectively:

/**

  • Virtual Alexa Button Driver
  • Copyright 2020 Michael Pierce
  • 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.

*/

String getVersionNum() { return "1.2.0" }
String getVersionLabel() { return "Virtual Alexa Button, version ${getVersionNum()} on ${getPlatform()}" }

metadata {
definition (
name: "Virtual Alexa Button",
namespace: "mikee385",
author: "Michael Pierce",
importUrl: "https://raw.githubusercontent.com/mikee385/hubitat-mikee385/master/drivers/virtual-alexa-button.groovy"
) {
capability "Actuator"
capability "Contact Sensor"
capability "Momentary"
capability "PushableButton"
capability "Sensor"
capability "Switch"

    command "push", ["number"]
}

}

def installed() {
initialize()
}

def updated() {
unschedule()
initialize()
}

def initialize() {
sendEvent(name: "numberOfButtons", value: 2)
}

def toggleOff() {
sendEvent(name: "switch", value: "off", isStateChange: true)
sendEvent(name: "contact", value: "closed", isStateChange: true)
}

def push(button) {
sendEvent(name: "switch", value: "on", isStateChange: true)
sendEvent(name: "contact", value: "open", isStateChange: true)

sendEvent(name: "pushed", value: "${button}", isStateChange: true, type: "digital")

runIn(1, toggleOff)

}

def push() {
push(1)
}

def on() {
push(1)
}

def off() {
push(2)
}

What do you want to be able to ask Alexa to do with it?

Like, if you named the device switchboard, you'd want to say Alexa, push button 3 of the switchboard ?

I'm not sure if Alexa even supports that syntax = why I'm asking.

What are you looking for in a single driver that multiple devices using the built-in switch/button virtual drivers wouldn't do ?

1 Like

Continuing the discussion from 2-button virtual switch driver:

Quick disclaimer - I haven't been able to use Room Lights effectively so far (maybe my ignorance)...

Assume I have several sets of devices I want to control together using a virtual device. For example, work lights, morning lights, all lights. Some of the devices would belong to more than one of these sets.

I would like to say "Alexa, work lights ON and "Alexa, all lights OFF". And of course also work with them in RM.

I can do this via virtual switches, but it requires more RM logic, RM rules and virtual switches than with a single custom virtual device for each physical device set.

With a custom driver that sets button=1 when it is set to ON and button=2 when it is set to OFF (excluding the automatic turn off as part of the momentary button functionality). Then RM can get the button value from the custom attributes of the virtual device.

So I'm really trying to use the button property to report whether ON or OFF was initiated, not to simulate multiple button actuations.

Unfortunately, I don't have a clue what caused the error shown in the log entry in my post or what to change to fix it. Although I'm an experienced programmer, I still don't know anything about groovy yet.

Finally, with my driver mod I listed, I was able to create a working virtual device and working RM rule to handle it, but while modifying the rule it would eventually fail during RM editing with the log error I showed. That failure was severe enough that it couldn't be fixed via editing and the only solution was to remove/delete the rule completely and start over creating a new rule. I suspect my successful rule, which was just a trial effort, would probably have failed before long during actual use.

When you create a Hubitat Group or Scene (now call room lighting) there is an activator device created (a virtual switch) which can be used in rules and it can also be exposed for Alexa to turn on or off. Getting the correct name set for Alexa is key because it can be somewhere between too damn picky and too damn helpful. Experimenting will be useful.

If you won't write rules then buttons will have limited functionality. They also have lousy dashboard feedback.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.