Best way to store information & settings about devices

Hi all,
I've written a number of simple apps/drivers but I'm embarking on a more complex app & I'm not sure I know the best way to design it.

I'd like to have the user select a set of devices as part of the configuration, and then for each device, specific a set of settings. Over the course of the operation, I'll need to store some state information about each device.

Currently, I'm using a map to store the settings & state of each selected device, but it's rather kludgy. I'm wondering if there's a better way.

For example: to generate the settings for each selected device, I'm doing the following :

    section("Devices"){
        input name: "arrivingSwitch", type: "capability.switch", title: "Select Switch which will indicate imminent arrival", multiple: false, required: true
        input name: "devicesToControl", type: "capability.thermostat", title: "Select HVAC Devices to turn on/off", multiple: true, required: true, submitOnChange: true
        input "notifyDevices", "capability.notification", title: "Devices to notify when Device is turned on", required: false, multiple: true,  submitOnChange: true
    }
    section {
        if (devicesToControl) {
            devicesToControl.each{ h ->
                String deviceName = (h.getLabel() == null) ? h.getName().toString() : h.getLabel().toString()
            
                section(hideable: true, hidden: true,  "Settings for: ${deviceName}") {
                    input name:"dev.${deviceName}_dayTemp", type: "number", title: "Daytime Temp for ${deviceName}:", required: true, defaultValue:68
                    input name:"dev.${deviceName}_nightTemp", type: "number", title: "Nighttime Temp for ${deviceName}:", required: true, defaultValue:60
                    input name:"dev.${deviceName}_adjustWhenHome", type: "bool", title: "Adjust ${deviceName} temp based on Day/Night settings?", defaultValue:true
                    input name:"dev.${deviceName}_turnOnWhenHome", type: "bool", title: "Turn ${deviceName} ON if powered off when Home?", defaultValue:false
                    input name:"dev.${deviceName}_turnOnwhenArriving", type: "bool", title: "Turn ${deviceName} ON When Arriving?", defaultValue:false
                }
            }
        }
    }        

To use those settings, I store them in a Map.


void updateSettings(){
    String deviceName
    String settingName

    devicesToControl.each { s ->     
        deviceName = (s.getLabel() == null) ? s.getName() : s.getLabel()
        nightTemp = "dev_${deviceName}_nightTemp"
        dayTemp = "dev_${deviceName}_dayTemp"
        adjustWhenHome = "dev_${deviceName}_adjustWhenHome"
        turnOnWhenHome = "dev_${deviceName}_turnOnWhenHome"
        turnOnwhenArriving = "dev_${deviceName}_turnOnwhenArriving"
        def thisSettings = [name: deviceName, 
                                    nightTemp: this."$nightTemp", 
                                    dayTemp: this."$dayTemp", 
                                    adjustWhenHome: this."$adjustWhenHome",
                                    turnOnWhenHome: this."$turnOnWhenHome",
                                    turnOnwhenArriving: this."$turnOnwhenArriving"
                            ]
        state.deviceSettings.put(deviceName, thisSettings)
    }   
}

I would recommend using device ID vs name/label because you could rename a device and that would break your app for that device. I would suggest looking at this example app as it also has a selection of devices and then stores settings for each:

1 Like

Thank you! That's exactly what I was looking for!

One more follow-up question: In addition to per-device settings (which you answered) I also need to store some per-device state information. Should I store that in the same hashmap or use some other means?

Either could work. Answer really depends on the logic in your app and how it loops through the state variables or references them.

Hi there,
Another follow-up question. In the example you've shared, it uses buttons to save the device-specific settings to the state hashmap with each click.

I'm using more traditional input forms (numbers, text, etc...) and I can't figure out how to save them directly to a Hashmap stored in settings or written to a state variable.

Instead, I'm saving each of the settings hashmap and then constructing the state.deviceSettings hashmap when updated() is called. There are a number of flaws with this approach so I'm wondering if I'm missing a simpler approach.

Thanks!

This is the only example provided to save data from tables. Now this said using the browser inspect you can dissect what’s it’s calling and possible come up with something different but then code May break in a future release.