Device Type preferences: input validation


I’m new to SmartThings and one of the first devices I set up was the Everspring Flood Sensor (aka Utilitech Water Leak Sensor). I found that the built-in device type (Everspring Flood Sensor) was not returning battery status reliably, if at all. So, being new to SmartThings, and being new to Groovy programming (though not new to programming in general), I thought why not try (re)writing my own device type.

Thus far, I do now have battery being reported reliably, and I’ve changed the device UI slightly to color code the battery level (something that was commented out in the default). So, I thought, why not try adding in a user configurable wake up interval. And I got that working too. So, I thought, why not try to implement some input validation. The Everspring/Utilitech sensor wake up interval is 60sec - 194 days, and I wanted to gracefully trap, say, an input of 15 sec. Here’s where I’m running into a couple of issues.

My preferences setting:

preferences {
    // manufacturer default wake up is every hour; optionally increase for better battery life
    input "userWakeUpInterval", "number", title: "Wake Up Interval (seconds)", description: "Default 3600 sec (60 sec - 194 days)", defaultValue: '3600', required: false, displayDuringSetup: true

My issues:

  1. it seems the ‘defaultValue’ in input has no effect? If userWakeUpInterval is not set, it appears to be null when referenced in other parts of the code. Once set by the user (either in IDE or in mobile app device preferences), then it references as expected. My expectation is that if not explicitly set, then userWakeUpInterval would then be the ‘defaultValue’. Am I not understanding this setting correctly (a distinct possibility)?

  2. Ideally for input range validation, I’d like to (re)set userWakeUpInterval to the min (or max) if user has input a value outside of the range. For example, min interval for this device is 60 sec. So if user inputs 15, I’d like to set userWakeUpInterval back to 60 to make it a bit more obvious that 15 is invalid input. So far I can reference userWakeUpInterval as a right-hand argument, but if I try to assign to it on the left-hand side, I get errors. For example, I can say something like:

     if (userWakeUpInterval.toInteger() < 60)

and that works as expected. But if I then try something like:

userWakeUpInterval = '60'

That causes some kind of error. (Also see next point.) Is it possible to reassign a user preference from within the code? Or, even better, is there a way within preferences to define some kind of input validation?

  1. To point (2) above, the only way I can see that it errors is when running against a real device – and then I cannot see how it errors, only that it does error by seeing the side effects in the device event list in the IDE. It seems the simulator does not (yet?) handle user preferences for device types??

Thanks for any input (no pun?) or advice you all might have!!

Will you be sharing your device type that includes battery reporting?

Indeed, I do plan on sharing … as soon as I either figure out the input validation, or remove my ‘testing hacks’ to share without input validation.

I also need to figure out best way to share … post code here? or get a github account?

Posting here is fine. I don’t know anything about github.

I’m hoping to be able to add your battery reporting to the utilitech siren.

If you haven’t seen it yet wackware has a good battery test alarm that will just let you know if anything gets low. Very helpful if you have a lot of devices.

These are great adds to this device! Thank you for sharing

Well, since I didn’t get any answers to my device type coding questions, I’ve gone ahead and posted my updates to the Everspring Flood (Utilitech Water Leak) sensor here: