Has anyone created a Virtual Button from scratch?

Ideally a Virtual Button device with multiple "physical" buttons (and why not, with battery and presence though I can add that)

AFAIK the virtual button source code isn't available.

Most Hubitat built-in drivers, including this one, are closed source. That being said, virtual devices are generally quite easy to create from scratch. However, the Virtual Button driver can already support an arbitrary number of buttons--that is what the "Number of physical buttons" preference does. (Note that this does not affect anything on the driver page, just like how the number of physical buttons on a real button device also does not affect anything on the driver page. This is just an attribute, numberOfButtons, that apps can read to find ways to make things more user-friendly, for example.)

You could certainly add battery (though virtual devices, by definition, don't have them...) and presence (though at that point it's no longer just a button device)--though, being virtual you'd need custom commands or some way to set these attribute values--but again, with virtual devices, that isn't too hard.

May I ask: is there a certain need you're trying to fill that something else doesn't?

1 Like

Absolutely (and maybe a Virtual device isn't really right way to do this).

I have built a Wifi connected device that can essentially act as a button with 6 or more "physical buttons". Right now this device publishes event to a MQTT server so things like button 6:pressed, voltage: 3.80v, present: true, etc. It might not be the right approach and am flexible as to how I can interface with Hubitat but MQTT seemed like a good way to do it.

I add, probably wrongly, assumed that since it's not a Zigbee or Zwave device it should be a Virtual Device but I guess it just needs to be a device with a custom driver that reacts to the MQTT events by modifying its attributes and calling events?

Either way sounds like it should work! With a "pure" virtual device, you'd probably need some app to tie the real and virtual device together, but if you're using MQTT, perhaps you already have something set up for that. (I used to use MQTT, but I'm not familiar with how most people have this set up on Hubitat anymore. There are a couple community options, at least. If you aren't, maybe one would already do that you're looking for.)

Alternatively, a device-specific driver could probably work too. You'd either need to put the MQTT connection code into that or use it as part of another driver (possibly with a "helper app") that does so for you.

1 Like

Sounds like you need to write a MQTT driver for Hubitat to match the topic and payload on the MQTT broker. It's not all that hard if you know some Java or Groovy and have an example to work from. @kevin has a more generic method for dealing with MQTT devices IF the payload format matches up. Depending on what you want Hubitat to do with the data, a button might be the correct device to emulate or maybe just a sensor that rules and app can read ?

2 Likes