Device Handler - reading preferences

I am writing a device handler and storing some data in preferences under metadata. However, when I try to read them I just get null returned. I think it is something very simple but not sure. Here is the code I am using and just testing it in the simulator.

I am writing a device handler and storing some data in preferences under metadata. However, when I try to read them I just get null returned. I think it is something very simple but not sure. Here is the code I am using and just testing it in the simulator.

metadata {
definition (name: "Switch", namespace: "harrydn", author: "Harry Dodd-Noble") {
	capability "Switch"
}


simulator {
	// TODO: define status and reply messages here
}

tiles {
	multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
		tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
			attributeState "on", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#79b821", nextState:"turningOff"
			attributeState "off", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"turningOn"
			attributeState "turningOn", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#79b821", nextState:"turningOff"
			attributeState "turningOff", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"turningOn"
		}
        tileAttribute ("device.level", key: "SLIDER_CONTROL") {
			attributeState "level", action:"switch level.setLevel"
		}
	}
}

preferences {
      input name: "email", type: "email", title: "Email", description: "Enter Email Address", required: true, displayDuringSetup: true
    input name: "text", type: "text", title: "Text", description: "Enter Text", required: true
 }
}

// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
// TODO: handle 'switch' attribute

}

// handle commands
def on() {
log.debug "Executing 'on'"
log.debug "email: $email"
log.debug "text: $text"

// TODO: handle 'on' command
}

def off() {
log.debug "Executing 'off'"
// TODO: handle 'off' command
}

Your code should look like this:

log.debug “email: ${email}”
log.debug “text: ${text}”

I still get “debug text: null”

Also if I try to do email.description I get a NullPointerException

Thanks for looking at this!

1 Like

Try this:

preferences {
	input "email", title: "Email", description: "Enter Email Address", required: true, displayDuringSetup: true
        input "text", title: "Text", description: "Enter Text", required: true, displayDuringSetup: true
}
1 Like

No still not working. If you copy and paste the code does it work for you?

Perhaps there is something I need to set up in my IDE or somewhere else as when I tried to use someone else’s custom code I got the same error…

I use that format for many, many of my own DH’s. If you’re using the simulator, make sure you’re populating the preferences by clicking on the gear in the upper right hand corner:

Also please note: The simulator really stinks (sucks actually…) when trying trying to “simulate” a DH or smartapp.

Here’s an example of some of my code:

    preferences {
        input "kWhCost", "string",
        	title: "Enter your cost per kWh (or just use the default, or use 0 to not calculate):",
        	defaultValue: 0.16,
            required: false,                
        	displayDuringSetup: true
        input "wattsLimit", "number",
        	title: "Sometimes the HEM will send a wildly large watts value.  What limit should be in place so that it's not processed? (in watts)",
        	defaultValue: 20000,
            required: false,                
        	displayDuringSetup: true                
        input "reportType", "number", 
            title: "ReportType: Send watt/kWh data on a time interval (0), or on a change in wattage (1)? Enter a 0 or 1:",  
            defaultValue: 1, 
            required: false, 
            displayDuringSetup: true
        input "wattsChanged", "number", 
            title: "For ReportType = 1, Don't send unless watts have changed by this many watts: (range 0 - 32,000W)",  
            defaultValue: 50, 
            required: false, 
            displayDuringSetup: true
        input "wattsPercent", "number", 
            title: "For ReportType = 1, Don't send unless watts have changed by this percent: (range 0 - 99%)",  
            defaultValue: 10, 
            required: false, 
            displayDuringSetup: true
        input "secondsWatts", "number", 
            title: "For ReportType = 0, Send Watts data every how many seconds? (range 0 - 65,000 seconds)",  
            defaultValue: 15, 
            required: false, 
            displayDuringSetup: true
        input "secondsKwh", "number", 
            title: "For ReportType = 0, Send kWh data every how many seconds? (range 0 - 65,000 seconds)",  
            defaultValue: 60, 
            required: false, 
            displayDuringSetup: true
        input "secondsBattery", "number", 
            title: "Send battery data every how many seconds? (range 0 - 65,000 seconds)",  
            defaultValue: 900, 
            required: false, 
            displayDuringSetup: true 
    }

def updated() {
    log.debug "updated (kWhCost: ${kWhCost}, wattsLimit: ${wattsLimit}, reportType: ${reportType}, wattsChanged: ${wattsChanged}, wattsPercent: ${wattsPercent}, secondsWatts: ${secondsWatts}, secondsKwh: ${secondsKwh}, secondsBattery: ${secondsBattery})"
    response(configure())
}
1 Like

I hardly use the simulator because of what I mentioned above. What I usually do is to create a virtual device in the IDE (via My Devices) that I can control via the phone app using the DH I’m working on, and then I use Live Logging to debug.

Yes I’ve heard that about the simulator but thought would at least be able to try the basic features! I also tried the preferences and update you put but again get: updated (kWhCost: null, wattsLimit: null, reportType: null, wattsChanged: null, wattsPercent: null, secondsWatts: null, secondsKwh: null, secondsBattery: null)

How does the gear work? I’ve never clicked that before but when I do it just comes up saying Virtual but no other information or options.

Also, created a virtual device and the same issues…

Yup, I would have been surprised if you did get anything because you didn’t update Preferences.

I just created a virtual device in the IDE, and using your code with the changes I recommended, everything worked:

Aaah yes that worked - thank you!!!

I didn’t realise they needed to be set in app manually.
So is there no way to load them in the simulator? It seems strange that you can’t set them up for testing?

Anytime!

Yeah, you’d think the simulator would allow you to “simulate”, but nope. It’s literally the bare bone basics, and sometimes that doesn’t even work.

I’ve resorted to doing what I’ve shown above because it represents the real deal. It’s actually easier, especially when the load times for the simulator can take as long as home brewing beer…

1 Like