Dynamic Pages and href Parameters

I’m trying to write a smart app which allows users to select multiple modes, and for each mode, set additional settings. I want to do this with dynamic pages and it doesn’t appear possible to generate a dynamic page from a method. Rather, I’ve discovered I have to jump to the page with an href and if I want to pass the selected mode as a parameter, I’m finding it get’s lost on submit since href parameters do not persist.

Any suggestions for how to work around this limitation?

Below is the example code I’m testing this with:

preferences {
    page(name: "selectRoutines")
    page(name: "anotherPage")
}

def selectRoutines() {
    dynamicPage(name: "selectRoutines", title: "Routine Selection", uninstall: true) {

        section("Set a routine for each mode selected") {
            def m = location.mode
            def myModes = []
            location.modes.each {myModes << "$it"}
            input "modesX", "enum", multiple: true, title: "Select mode(s)", submitOnChange: true, options: myModes.sort(), defaultValue: [m]
            def sortModes = modesX
            if(!sortModes) someSettings(m)
            else sortModes = sortModes.sort()
                sortModes.each {someSettings(it)}
        }
    }
}
def someSettings(modeX) {
    def hrefParams = [modeX: "$modeX"]
    href(name: "moreSettings", title: "an href appeared", params: hrefParams, required: false, page: "anotherPage")
}

def anotherPage(params) {
    dynamicPage(name: "anotherPage", title: "Routine Automation", uninstall: true) {
        section("Another section") {
            input "aChoice", "enum", title: "Choose for $modeX", submitOnChange: true, options: ["Yes", "No"]
        }
    }
}

store the params in a state variable…

Here is how I do it: Look familiar??

		section("Set to a dimmer level selected for each mode") {
			def m = location.mode
			def myModes = []
			location.modes.each {myModes << "$it"}
			input "modesX", "enum", multiple: true, title: "Select mode(s)", submitOnChange: true, options: myModes.sort(), defaultValue: [m]
			def sortModes = modesX
			if(!sortModes) setModeLevel(m, "level$m")
			else sortModes = sortModes.sort()
			sortModes.each {setModeLevel(it, "level$it")}
		}
	}
}

def setModeLevel(thisMode, modeVar) {
	def result = input modeVar, "number", range: "0..100", title: "Level for $thisMode", required: true
}

Yep - I’ve been using your code for motion lighting as an example to learn off of. The key difference is that you set settings (i.e.: time) only once…and I’m trying to set variables for time for each mode selected.

So something like:
Mode1 starting
Mode1 ending
Mode2 starting
Mode2 ending
ModeN starting
ModeN ending

I’ve found that I could use your bit for selecting of modes and then create a page that creates new variables for each mode:

starting$modeX
ending$modeX

And this has worked successfully for saving variables except the href params weren’t persisting so the display of those variables would change to null after submitting changes. If I leave the page and go back to it - the app remembered what I put in each field though.

Using maxwells suggestion of state I think I can figure out how to get around this.

For the record I’m working on creating an app that will let you set routines to run for each mode which change depending on a user defined scheduled. So that when you leave it sets to the away mode for that time and when you return it changes to the correct home mode. Much like routine director but more customizable since I like to have multiple modes after sunset (i.e.: evening and night)