app.updateSettings and app.removeSetting don't work for me

In my app, I have code that looks like the following. 'iActionId' is a value in my setting;

void appButtonHandler(String button) {
    switch (button) {
      case 'handleAction'

        /* do stuff here */

        app.removeSetting('iActionId')
//      app.updateSetting('iActionId'), [type: "Text", value: ""])
//      app.updateSetting('iActionId', [type: "String", value: null])
//      app.updateSetting('iActionId', [type: "enum", value: null])
//      app.updateSetting('iActionId', [type: "String value: null])
        break;
    }
}    

No matter what I try, I can't clear iActionId (confirmed by looking at the "settings" section on the app status page). The code that sets it looks something like this:

def pageMain(Map params) {
    dynamicPage(name: "pageMain", title: title, install: true, uninstall: true) {
        section() {
          input name: "iActionId", type: "enum", title: "Action", options: actions, submitOnChange: true
          if (iActionId) {
              String summary = state.actions[iActionId]
              input name: "handleAction",
                      type: "button",
                      title: "act: $summary",
                      submitOnChange: true
          }
      }
    }
}

I've read the documentation here:
https://docs.hubitat.com/index.php?title=InstalledApp_Object#updateSetting

And my problem appears related to the issue described here:

But neither helps me figure out what I'm doing wrong.

1 Like

It won't let you 'set' nothing, so trying setting a value...

Have you tried seeing if removing this on the button makes a difference? My guess is that the behavior is undefined when you both submit the page (saving the preference you're trying to remove) and also do a removeSetting() (which should otherwise work) on it as part of clicking the same button, which you are--or at the very least I can't find anything documented about what you should expect if you try this.

If I remember correctly, I ran into something similar before, and one workaround I used was move the button that performs removeSetting() onto another page. That way, you don't have to worry about this at all--and in my case it was beneficial since it gave the user a chance to confirm. I might be mis-remembering the exact problem I was trying to solve, but it do recall it being similar to the above.

Nailed it! This is actually intended to be the "confirm" process; select a thing, then the button that enacts it (along with a cancel button which I removed for simplicity) shows up. I did two things based on your recommendation:

      if (iActionId) {
          String summary = state.actions[iActionId]
          input name: "handleAction",
                  type: "button",
                  title: "confirm: $summary"
      } else {
          input name: "iActionId", type: "enum", title: "Action", options: actions, submitOnChange: true
      }

First, I moved the input into an "else" clause so it wouldn't be fighting the app.removeSetting call, and then I removed the submitOnChange: true from the button. Apparently when you remove a setting it forces a refresh, and everything works although not quite as gracefully as I'd hoped, in that the popup is hidden. :man_shrugging:

Thanks!

Let's say I have a settings line such as :

input "sensor", "capability.temperatureMeasurement", title: "temp sensor", multiple: true

how do I remove and then add again one of the multiple devices in the code ?

app.updateSetting("$OneOfTheSensor", [type:"capability", value:null]) 

Would that work or should I go instead for this:

settings.remove("$deviceEntryInTheListOfSensors") ?

Now, can I add it again providing I recorded the device's exact display name or maybe its id ? How do I call a device within all the existing devices of the same capability on my hub in order to add it into a specific list entry in the settings?

Let me know if you need more info to get what I'm trying to do...

I would toString() the string on remove.

I have found that you cannot check immediately, so I don't use it in constructs like

remove(xxx) ? something : something

In some cases I have had to see the next run of the app / device before it fully takes effect.

Thanks. How do you call/list all the devices of a certain capability associated to the hub?

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