Advice on a simple App


(Michael and Jessica) #1

Guys, I’m trying to write a simple app that will turn off/ on multiple switches based on sensor output; specifically temp and humidity. I want this process to be automated, of course. However, I want to be sure hat the switches do not tun off unless both conditions are below their respective thresholds. I know nothing about writing code so I am following the, ‘How To’s’ like a paint-by-numbers and comparing my work to the work of others whose apps are similar. I know he syntax is correct because the app will save when I hit the button. I was hoping that someone could look it over to see what I may be missing or what might be useful based on their experience. Thanks!

/**
 *  Keep it Cool
 *
 *  Copyright 2015 Michael Schinke
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Keep It Cool",
    namespace: "Smartthings-Users",
    author: "Michael Schinke",
    description: "Turn devices on or off with input from temperature and humidity sensors",
    category: "Green Living",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png@2x.png")


preferences {
    section("Choose which Temperature Sensor will trigger your switches") {
		input "temperatureSensors", "capability.temperatureMeasurement",
            title: "Temperature"
    }
    section("When the temperature rises above...") {
    	input "temperature", "number", title: "Temperature"
        
    }
    
    section("Choose which Humidity Sensor will trigger your switches") {
    	input "humiditySensors", "capability.relativeHumidityMeasurement",
        	title: "Humidity"
            
    }
    
    section("When the humidity rises above...") {
    	input "humidity", "number", title: "Humidity"
        
	}
    
    section("Choose which switch(es) to turn on") {
    	input "switches", "capability.switch", multiple: true
        	title: "Switches and Outlets"
    }
}

def installed() {
		subscribe (temperatureSensors, "temperature", temperatureHandler)
 
		subscribe (relativeHumiditySensors, "humidity", humidityHandler)
        
        subscribe (switches, "switch.on", switchOnHandler)
        
    }

def updated() {
	unsubscribe()
    	subscribe (temperatureSensors, "temperature", temperatureHandler)
 
		subscribe (relativeHumiditySensors, "humidity", humidityHandler)
        
        subscribe (switches, "switch.on", switchOnHandler)
	}

private evaluate(currentTemperature, desiredTemperature, currentHumidity, desiredHumidity)
{
	def threshold = 1.0
    if (currentTemperature - desiredTemperature  >= threshold) {
    		switches.on()
    }
    if (desiredTemperature - currentTemperature >= threshold){
    		switches.off()
    }
    
    else if (currentHumidity - desiredHumidity >= threshold) {
    		switches.on()
     
    }
    
    if (currentHumidity - desiredHumidity >= threshold) {
    		switches.on()
    }
    
    if (desiredHumidity - currentHumidity >= threshold) {
    		switches.off()
            
    }
    
    else if (currrentTemerpature - desiredTemperature >= threshold) {
    		switches.on()
           
    }
    
}

(Tim Slagle) #2

you’re subscriptions are designated to classes that don’t exist?

humidityHandler, tempHandler, etc aren’t defined methods… The was the subscriptions work are

subscribe X trigger with Y value name and run Z method when that happens.

So you need to better define your methods and point the triggers to them correctly.

Also, your logic doesn’t have any way to get the currentHumidity. This will help you get the latest humidity value from a sensor.

deviceYouSelectedInSettings.lastestValue(“valueForThatDevice”)… in your case it should look like this:

humidity.lastestValue("humidity")

(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #3

Thanks for helping out the novice, Tim! :smiley:

Your terminology is confusing me though.

The third argument for subscribe() is a Method name not a “Class”, … Or am I misunderstanding you?

We can’t create Classes in SmartThings. We only deal with Object Instances (usually Devices), Methods (which may be Commands of a Device based on its Device Type), Attributes, Events, and local and state variables (which may be simple typed variables or arrays, Lists or Maps).


(Michael and Jessica) #4

In the case of the currentHumidity data, would the same also be true of the currentTemperature? In which section would I place these statements?

For the classes; are there specific classes for the particular device I’m using or did I just mis-define the class type in general?


(Tim Slagle) #5

I never went to school to be a dev so i’m still learning the “right” terms… But i get my point across :smile: Completely self taught here… Thanks for the lesson.

Yes. First you need to grab the device in the preferences section. Then you can grab the latest value of that device later on. You can use those phrases anywhere you need to get a current value. :smile:

As for the methods, you just need to define them and then properly use them :). You could do something like

def howToBeADev()

or…

def humidityCheck()

As long as you properly point the subscriptions to them


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #6

My pleasure.

Personally I find consistent and standard terminology is essential to teaching and learning programming, but it is a cumbersome step without good glossaries and the relevant product documentation that must also use the same terminology (ST is part way there).

But, yup, I guess you can get the point across with examples.


(Michael and Jessica) #7

Thanks for your help guys. I’m trying to do comparative research into other, similar apps to see how things have been handled in those. I use the Virtual Climate Control app myself and I know it works as it’s supposed to, so that is my baseline for constructing my app. While I understand the concepts I do have trouble connecting them with the actual application of the coding. I guess it just takes experience. Maybe I should have started out more simple but I’m just not that smart. :wink:


(Tim Slagle) #8

PM me and I can help you out :slight_smile: