Coding device page controls


Attached is a screen shot of part of the device page of a virtual switch I installed in my Hub.

How do I create in my own device driver code these types of controls.

Thanks much.


I've found chatGPT a very good tutor on how to write drivers and apps.

Part 1 - To get these preferences to appear on screen...

In the metadata section of the driver you can include a Preferences section where you can define different types of settings to record for the device. I've posted a few examples below from my thermostat driver (I removed some of the code to try and keep it brief).

My platform setting could be something like your Enable auto off example, where you may want a list of options to choose from. There is a simpler way to include a list in the definition of the setting, I'll see if I can find one.

For your other examples of toggle switches for settings, my logging preferences, which are of type bool are what you are looking for.

metadata {
  preferences {
        // Platform and authentication Preferences
        def platformSelected = []
            platformSelected << ["MELView"   : "MEL View (Aus/NZ)" ]
            platformSelected << ["MELCloud"  : "MEL Cloud (Europe)"]
            platformSelected << ["KumoCloud" : "Kumo Cloud (US)"   ]
		input name: "Platform", type: "enum",     title: "Platform",        displayDuringSetup: true, required: true, multiple: false, options: platformSelected, defaultValue: "MELView"
        input name: "UserName", type: "string",   title:"Username / Email", displayDuringSetup: true, required: true, multiple: false
		input name: "Password", type: "password", title:"Password",         displayDuringSetup: true, required: true, multiple: false
        // Logging Preferences
        input(name: "DebugLogging", type: "bool", title:"Enable Debug Logging",                   displayDuringSetup: true, defaultValue: false)
        input(name: "WarnLogging",  type: "bool", title:"Enable Warning Logging",                 displayDuringSetup: true, defaultValue: true )
        input(name: "ErrorLogging", type: "bool", title:"Enable Error Logging",                   displayDuringSetup: true, defaultValue: true )
        input(name: "InfoLogging",  type: "bool", title:"Enable Description Text (Info) Logging", displayDuringSetup: true, defaultValue: false)
    } // End of Preferences
} // End of metadata

Part 2 - To make use of the settings...

The code below shows how you can reference the settings, plus the method to include to detect changes. The updated method is called when a user clicks the Save Preferences button on the device edit page. If you include this method you can respond to any changes in settings and anything else you may want to do as a result. The references in the code to UserName, Password and DebugLogging are all references to the Preference settings defined in the meta data.
My use of the runIn method, a built in one with the platform, runs a method in the driver after a set amount of time, where I am using the debugAutoDisableMinutes setting. The reference to "debugOff" is the method in the driver that will be run in X minutes, effectively turning off the debug logging after that period of time.

// updated() - Run when the "Save Preferences" button is pressed on the device edit page
def updated() {
   debugLog("updated: Update process called")
   if (   "${UserName}"     != ""
        && "${Password}"     != ""
        && "${getBaseURL()}" != "")
      { refresh() }
   else { debugLog("updated: Refresh process was not called, check Preferences for UserName, Password, Platform and the Base URL State variable") }

   if (DebugLogging) {
     log.debug "updated: Debug logging will be automatically disabled in ${debugAutoDisableMinutes} minutes"
     runIn(debugAutoDisableMinutes*60, "debugOff")
   else { unschedule("debugOff") }

   debugLog("updated: Update process complete")

Another thing I would suggest, but is not required.... If you are going to add preference settings for turning different types of logging on or off, I like to setup my own method for each type, which does a simple check of the setting and if it's turned on, log the message. That way I don't have if conditions spread throughout my code.

//Logging Utility methods
def debugLog(debugMessage) {
	if (DebugLogging == true) {log.debug(debugMessage)}	

def errorLog(errorMessage) {
    if (ErrorLogging == true) { log.error(errorMessage)}  

def infoLog(infoMessage) {
    if(InfoLogging == true) {}    

def warnLog(warnMessage) {
    if(WarnLogging == true) {log.warn(warnMessage)}    

There some more notes here in the developer documentation on Preference Settings:

1 Like

I would also recommend reading Hubitat's developer documentation, especially the primer on drivers, which covers how to do this (and more):


Thanks very much.

A good example is a great help.


1 Like

I did look at the documentation but I didn't see anything that seemed related.

I will look again.