How to get a Dashboard Button to call a function/method in a device?

Hi there,

I've looked around but can't see an obvious answer for how people do this..... I know I can do it with Webcore or virtual buttons, but curious to know if there is a better way.....

On my dashboard I would like a few buttons or switches that call certain functions in a device handler, e.g. I have a thermostat handler and have coded a 'Boost' function, which I would like to trigger from my dashboard. I know you can display custom attributes (as text) in a dashboard, but I cant work out how to call a custom function - without creating a virtual device or webcore piston which just clones the function (this would get messy as Id like a few functions, and with multiple thermostats this becomes bloated very quickly)

Am I missing something? Thanks in advance!!

1 Like

only way i know is like you say or with rule machine on button press call a custom command directly in the device handler.. like i do for the tesla integration with a virtual button.

1 Like

Yes, I guess I could make the device handler have a 'button handler' and then you could push buttons, which in turn run the custom functions of the device handler. Though that also seemed a bit inefficient to me, since then you need to have a lookup for which button does what (i.e. button 1 triggers boost mode for 60 minutes, button 2 triggers boost mode until end of day....etc, while the functions are called 'boost1hour' and 'boostday' which are self explanatory)

You can create a virtual button device that has multiple buttons. I do that and have a couple of virtual button devices that have 25 buttons on each to create remote for a Roku and my home theater. Then you're not cluttering your device list with a bunch of virtual devices, just one.

1 Like

If you can somehow call a different feature of a single virtual button, then surely I can call the different functions of the main device handler?

no you misunderstand you do not modify the device handler.. you create a new virtual button device and put that on your dashboard. . and then in rule machine it can call any function in the other device handler directly.. that is what i am doing in my example above shwoing the rule machine rule if you look closely.

1 Like

You can find or create jpeg images to use as button backgrounds then make the button itself transparent. That's how channel buttons are at top of Roku remote. Most use built in icons instead and are not transparent.

1 Like

I like the idea of transparent buttons, but I still don't want to create 50 RM rules and a pile of virtual buttons to cover a few functions in a device handler as it will make RM harder to debug.

I could make the main device handler (thermostat device) respond to button presses, but this is just one of my devices and it is a pain to do this for all device handlers, and also Im still stuck with having to press 'button1' as opposed to just calling function 'XYZname' which is more descriptive.

Well, you have lost me at using a device handler. I actually use Node Red to do all the logic for this but it would work the exact same way using RM. And there is only 1 virtual button controller created in your Device List but it would have multiple buttons to setup in a single Button Controller Rule. Sorry I can't be more help.

1 Like

Ok I understand now, thanks. So a single virtual button device can have an unlimited number of buttons, and each button can trigger a different function on a different device, as opposed to needing one virtual button device per device.

I was just trying to keep it so that the button presses were logical and had descriptive actions. Hence being able to trigger a function/command on a device directly from the dashboard would be an improvement as you wouldnt need the button lookup table. (I could make the main device (thermostat) respond to button presses, hence for these devices I could skip the virtual button requirement, but I would probably need it for something else)

Presumably something like this must be possible somehow, since you can display attributeXYZ from DeviceABC, surely calling CommandDEF would follow similar rules? If this isnt supported currently, why? And how would I make the request to support it?

(you could even modify the button template so that rather than typing in a button number, you could also type a string, and this would then call the listed function)

Thanks again

@bcopeland Sorry to pick you at random...!

Is there any way to trigger custom functions in a device handler, other than using rule machine and virtual buttons? Just like having custom attributes display by name, we could call custom functions/commands in the device handler by name (the commands visible on the device page I guess is what I mean, and even better if they could be called with their parameters e.g. boost1hour(28))

Thank you in advance!

@bcopeland - Sorry to chase you but Im not sure if you saw this. Keen to get my dashboards set up a bit more and want to do it knowing what direction to go.
Thanks very much

@hubitat - Am I missing anything with this request? Thanks again!

Edit just realised all along it is a bank holiday in the USA, didn't think you might be off. Sorry if so!

Even IF they decide to add this, it would be many weeks to months before implemented. If you are anxious to begin, I would use the advice given above regarding using virtual buttons. It really isn't as complicated as you are making it out to be. It is very simple and quick to do it via virtual devices.

With that being said, I don't think they will do this, but I have no inside knowledge of how the hub works. I don't think this is how the backend of this device works from what I have read from people who are much smarter than I am on this topic. I am not a coder, (not sure if you are?) but this seems to me like something that would take a major rewrite of things to implement.

Would definitely require extending the Dashboard, and might limit the number of devices that you could expose. To make this work every device authorized for a dashboard would need to expose all of it’s functions, and allow for the passing of parameters; it would almost take combining Rule Machine with Dashboard. Could increase the security risk for dashboard if the wrong functions were added to a dashboard control.

1 Like

Thanks for the responses, Im not expecting a change quickly but it does still change how I might built things.

In theory all attributes for a device are already accessible in the dashboard using the 'custom attribute' tile template. Meanwhile each tile template already has access to a range of different functions (e.g. on/off/setLevel/setHeating/....etc based on what the template is used for). Hence I figured calling a custom function is a cross between accessing a custom attribute and the existing function calls that have been set as public (i.e. not private).

I think mainly I am a bit surprised there aren't more people wanting this sort of feature since it allows you to access lots of automations/features in a fraction of a second (and would be easy for most users to set up). At the same time as this, I didn't think it was a super complex thing to develop, hence was a great feature to add for a wide range of users, however perhaps I am wrong?

Doing it with virtual buttons is definitely a workable hack, and could be improved further by adding button events to the device handler directly (then you wouldn't need the virtual button device - and it would help everybody else who uses that device handler). I just thought this feature is something that would be far more useful, intuitive and accessible for lots of users..... And you could even take it to town and build an ST style interface with this feature and custom attribute tiles.

For what its worth, and I think this may help a lot of people. I discovered that the dashboard Button tile can support text in addition to integers, hence you can do exactly what I have described above with the following small modification to your device handler:

You need to add a command at the top of the device handler

command "push"

Then you need to add the following function somewhere in the device handler:

def push(button)
    try {
        log.debug "Running function from button press: ${button}"
    catch (e) {
        log.debug "Failed to run function: ${e}"

Then you can add a tile to your dashboard as below, which will call the function you list as the 'Button Number' (e.g. this button now calls the 'refresh' function):

Winner winner chicken dinner!

1 Like

That's cool that the button "number" can actually be a string. I'm going to have to go back to my virtual buttons and change those numbers to strings, it will make it easier to understand my node red flows and follow the Switch Node within.

I have done the trick with treating button numbers as strings in a few of my drivers. In at least one place, I handle integers and strings input differently in the same push command. Great write up, @cjcharles

One tip: you can indeed use strings with buttons on dashboards, but RM only takes integers as input. So, you may also want to make some magic numbers for buttons for your most common string-style operations.

To clarify, this only works if you add those new lines to a Device Handler correct? I misunderstood and thought I could do it with a native Button device but i get this error in my logs:

Since we don't have access to Button DH to tell it to accept a string, I am SOL right? :grin: