HSM-like buttons in app?

What should I put in my app to create buttons like those?

image

To render the button, use this:

input "myButton1", "button", title: "Arm Away"

Then, add a method to you app like this:

def appButtonHandler(btn) {
      switch(btn) {
          case "myButtton1":  doWhatEver1()
               break
          case "myButton2":  doWhatever2()
               break
      }
}

Notice that the method is called with the name of the button from the input statement as the parameter. Once the method runs, a SubmitOnChange is performed on the page.

1 Like

Thanks!

One thing to keep in mind, is if any changed user input in on the page when they click the button, that changed information will be saved to the hub after the appButtonHandler method has been called.

Ex. Asking a user to enter a value in an input
Ask them to click a button

appButtonHandler looks for value of that input which will be null or a previous value

Does something...

Comes back to the UI and does a submitOnChange

User input is now saved to DB, and any page logic can be done on that input.

In a future update, the button input will take an optional submitOnChange:false option to disable the UI input submit after.

1 Like

Along the same lines, what do I need to put in the device code to allow these device-page buttons to be called by real buttons. I want to be able to arm the alarm by pressing a physical button.

Currently, I'm getting an error when pressing the physical button which calls "Alarm Arm Away". Obviously missing something in the Device code.

Error is:
java.lang.IllegalArgumentException: Command 'push' is not supported by device. (buttonHandler)

@mike.maxwell

button input types are not supported in driver code
You add "buttons" to drivers by adding a command to the definition section along with a matching method
command "test"
def test(){}

of one with a numeric parameter,
command "test", ["NUMBER"]
def test(param){}

Do I need to return anything in particular?
Every time I click the button I get

An unexpected error has occurred trying to load the app. Check Logs for more information.

But there is nothing relevant in the logs. My preferences page has a single entry under a dynamicPage

You should probably post the entire app, I'm guessing something is missing on that page...

preferences {
    page(name: 'mainPage')
}

def mainPage() {     
    dynamicPage(name: 'main', uninstall: true, install: true) {
        
        section('Commands') {
        	input "check", "button", title: "Check"
        }
        
		section('Sensors') {
			input 'sensors', 'capability.sensor', title: 'Sensors', multiple: true, required: false
		}
    }
}

def installed() {
	log.debug "Installed with settings: ${settings}"
    initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
    initialize()
}

def initialize() {
}

def appButtonHandler(btn) {
	
    log.debug(btn)

    switch(btn) {
          case 'check':
               break
      }
}

You need to add the method to handle the button presses, like this:

def appButtonHandler(btn) {
   if(btn == "check") {
// do whatever you want to do when the button is pressed
   }
}

In your case with only a single button, you don't really need to check which one is pressed (the if(btn == "check") part above).

When the UI button is pressed, that method is called.

I do have that method, it's in the posted code... ?

You are missing the "definition" section in your app

Something like this:

definition(
    name:"MyApp",
    namespace: "You",
    author: "You",
    description: "Sample.",
    category: "Convenience",
    iconUrl: "",
    iconX2Url: "",
    iconX3Url: "",
    importUrl: ""
)

I do have that, I just omitted pasting it, sorry.

Try this: take one of the reference small apps here:

Then modify it by adding your button to it. and making other changes as needed. Check that it still works at each step.

ok, found the issue, I had:

preferences {
page(name: 'mainPage')
}

def mainPage() {     
    dynamicPage(name: 'main', uninstall: true, install: true) {

in preferences the page was named mainPage while it was name: main in dynamicPage. The UI still worked so I had no clue something was wrong. Once I changed to name: mainPage it started working. Thanks for your help.