Can I choose device in parent app ( input list of devices does not appear)?

Trying to have a "master switch" (sensor for external temperature) so in case of exceeding certain number to auto disable child apps.
Unfortunately, everything else appears but the devices "list".
What am I doing wrong (the " input suppressSensor" below does not visualize sensors list to choose from)?

def suppressSensor = [
name: "suppressSensor",
type: "capability.sensor",
title: "Suppress Sensor",
description: "The suppress sensor, should measure outside temperature.",
multiple: false,
required: true
]

preferences {
page name: "mainPage", title: "", install: true, uninstall: true
}

def mainPage() {
dynamicPage(name: "mainPage") {
installCheck()

	if (state.appInstalled == 'COMPLETE') {
		//section(getFormat("title", "${app.label}")) {
        section(getFormat("title", "Smart Termostat")) {
            paragraph "<i>Follow below step-by-step to correctly setup this app.</i>"   
            paragraph getFormat("tickline")
		}
        section("<h3 style='color:#663399;font-weight: bold'>Manual Master Switch:</h3>") {
            input "masterSwitch", "bool", defaultValue: true, title: "All child rules execution active."   
            paragraph getFormat("tickline")
        }
        section("<h3 style='color:#663399;font-weight: bold'>Auto Suppress Switch:</h3>") {
            input "suppressSwitch", "bool", title: "Suppress switch for outside temperature active.", defaultValue: true 
            input "suppressTemp", "float", title: "Tepmerature to suppress heater when above it:", defaultValue: 19.0f
            input suppressSensor
            paragraph getFormat("tickline")
        }
        section ("<h3 style='color:#663399;font-weight: bold'>Temperatures</h3> Define temperatures upon Away and not-Away confitions.") {
            input "minTempAway", "float", title: "Minimum temperature when Away:", defaultValue: 19.0f
            input "maxTempAway", "float", title: "Maximum temperature when Away:", defaultValue: 21.0f
            input "minTempHere", "float", title: "Minimum temperature when Here:", defaultValue: 24.0f
            input "maxTempHere", "float", title: "Maximum temperature when Here:", defaultValue: 26.0f
            paragraph getFormat("tickline")
        }
		section("<h3 style='color:#663399;font-weight: bold'>Zoneficator</h3> Define connections of type \"sensor(s) <-> heater(s)\".") {
			app(name: "anyOpenApp", appName: "Smart Termostat - Zoneficator", namespace: "teotod", title: "<strong>Add new or edit following connections:</strong>", multiple: true)
			paragraph getFormat("line")
		}
        
        //footer
        section() {
	        paragraph getFormat("tickline")
	        paragraph "<div style='color:#663399;text-align:center'> for more... <br> <a href='https://github.com/TeoTod/Hubitat' target='_blank'> Click here for my GitHub Hubitat space </a> </div>"
        } 
	}
}

}

You're running into a Groovy inner working here: apps and drivers are ultimately Groovy scripts, and "top level" code like your def suppressSensor... get moved inside a method, all behind the scenes, when the script is compiled (I assume for Java compatibility, where everything must be inside a method--and class). Because it is then effectively defined inside a method, it is not available outside that method like when you're trying to use it for your input.

The direct solution here would be to "elevate" this to a script-level field, which you can do by annotating it like:

@groovy.transform.Field
def suppressSensor = ...

However, a related question: why are you trying to define it here and use it later instead? I assume there is some reason, but it's not apparent from this snippet, and if there isn't, another way around this is to simply write out the entire input at the place where you need it.

Also, both the name of your input and the (now field) variable itself are called suppressSensor. Because Hubitat also elevates preferences/settings (the selected device[s] would for sure be accessible as settings["suppressSensor"]) to script-level fields (so supressSensor should do the same), you might run into some oddities here. I'd guess that Groovy/Hubitat would gracefully handle this (and maybe your defined field would win out, so you'd just have to remember to use the settings Map to get the devices), but you might want to make things less confusing by naming either your field or your setting name something else so they don't conflict at all.

Thanks Berta, works exactly as you've advised. Thank you for the naming suggestions too, implemented!

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