First Shot at Coding need help

Hello, Looking for a little help with code. I have never written code before and looking to make a smartapp. I have been reading the documentation and thought I would give it a shot. What this app does is pulls the temperature from a sensor and changes the color of a bulb to match the degrees. I think I have a good handle on it but I keep getting errors when I try to save and doesn’t really tell me what is the problem. I’m not completely done with it but I need to get back to work. I’m still researching the color codes for the bulb so I know they are not correct but any help anyone could provide or any advice would be appreciated.

definition(
name: “Temperature Bulb”,
namespace: “tagz379”,
author: “Joe Battaglia”,
description: “Change color of bulb based on temperature”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section (“Sensor”) {
input “sensor1”, “capability.temperaturemeasurement”,
title: “Where?” }

section ("Bulb") {   
    input "bulb", "capability.colorcontorl", 
        title: "Bulb" }

def initialize() {
subscribe (sensor1, “sensor”, sensorHandler)}

def sensorHandler(evt) {
log.debug “$evt.value”

if (evt.value == "10") {
   bulb.on();
   bulb.sethue(80);
   bulb.setsaturation(50);
   bulb.setlevel(80); }
   
  else if (evt.value == "20" {
   bulb.on();
   bulb.sethue(70);
   bulb.setsaturation(40);
   bulb.setlevel(80); }

Looks like your braces and brackets aren’t matching. You’re missing a closing bracket on the line below.

I think you’re also missing a closing brace at the bottom of the file. and I think at the end of the preferences section. Computers are very picky that way :smile:

Then in general, you probably want the temperatures to be in ranges, e.g. if value < 10, not if value == 10, and then go down from highest range to lowest. I would also move the repetitive setting of hue and saturation into a function that can be called.

With that said, the code below will at least save. I won’t deprive you of the experience to test and fix the code.

Good luck! But be careful - coding can easily become addictive :smiley:

definition( name: "Temperature Bulb", namespace: "tagz379", author: "Joe Battaglia", description: "Change color of bulb based on temperature", category: "My Apps", iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png" )

preferences {
section (“Sensor”) {
input “sensor1”, “capability.temperaturemeasurement”,
title: “Where?”
}

   section ("Bulb") { 
	   input "bulb", "capability.colorcontorol", 
	title: "Bulb" 
}

}

def initialize() {
subscribe (sensor1, “sensor”, sensorHandler)
}

def sensorHandler(evt)
{
log.debug “$evt.value”

if (evt.value > “20”) {

   setColor(70,40)
}

else if (evt.value > “10”) {
setColor(80,50)
}
}

def setColor(hue, saturation)
{
bulb.on()
bulb.sethue(hue)
bulb.setsaturation(saturation)
bulb.setlevel(80)

}

1 Like

Also a typo here, my friend!

1 Like

Thanks for your help guys, Going to give this a go and see what happens.

Here is what I have so far, However I’m getting error when trying to run and don’t understand what the errors are telling me. Any help from you guys would be greatly appreciated

definition(
name: “Temperature Bulb”,
namespace: “tagz379”,
author: “Joe Battaglia”,
description: “Changes color of Bulb based on Temperature”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“Devices”) {
input “sensor”, “capability.temperatureMeasurement”, title: "Select Temperature Sensor"
input “bulb”, “capability.colorControl”, title: “Select Bulb”, required:true, multiple:true
}
}

def installed()
{
subscribe(sensor, “temperature”, temperatureHandler)
subcribe(bulb, “color”, colorHandler)
}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
subscribe(sensor, "temperature", temperatureHandler)
subcribe(bulb, "color", colorHandler)
initialize()

}

def initialize() {
subscribe (sensor, temperatureHandler, bulb, colorHandler)
}

def temperatureHandler(evt) {
log.debug “temperatureHandler: $evt”

if (evt.value < "10") {  //White
setColor(bulb)
hueColor = 52
saturation = 19
}
else if (evt.value > "20") { //Daylight
setColor(bulb)
hueColor = 52
saturation = 19
}
else if (evt.value > "30") { //Soft White
setColor(bulb)
hueColor = 53
saturation = 91
}
else if (evt.value > "40") { //Warm White
setColor(bulb)
hueColor = 20
saturation = 80
}
else if (evt.value > "50") { //Blue
setColor(bulb)
hueColor = 70
}
else if (evt.value > "60") { //Green
setColor(bulb) 
hueColor = 39
}
else if (evt.value > "70") { //Yellow
setColor(bulb)
hueColor = 25
}
else if (evt.value > "80") { //Orange
setColor(bulb)
hueColor = 10
}
else if (evt.value > "90") { //Red
setColor(bulb)
hueColor = 100
}

}

I keep getting this error when trying to run it and not sure what it means

grails.validation.ValidationException: Validation Error(s) occurred during save():

  • Field error in object ‘physicalgraph.device.Device’ on field ‘name’: rejected value [null]; codes [physicalgraph.device.Device.name.nullable.error.physicalgraph.device.Device.name,physicalgraph.device.Device.name.nullable.error.name,physicalgraph.device.Device.name.nullable.error.java.lang.String,physicalgraph.device.Device.name.nullable.error,device.name.nullable.error.physicalgraph.device.Device.name,device.name.nullable.error.name,device.name.nullable.error.java.lang.String,device.name.nullable.error,physicalgraph.device.Device.name.nullable.physicalgraph.device.Device.name,physicalgraph.device.Device.name.nullable.name,physicalgraph.device.Device.name.nullable.java.lang.String,physicalgraph.device.Device.name.nullable,device.name.nullable.physicalgraph.device.Device.name,device.name.nullable.name,device.name.nullable.java.lang.String,device.name.nullable,nullable.physicalgraph.device.Device.name,nullable.name,nullable.java.lang.String,nullable]; arguments [name,class physicalgraph.device.Device]; default message [{0} cannot be null]

Are you using virtual devices in the simulator or real devices?

As such in your code

  1. The colorHandler function is not implemented. What’s the purpose any way?
  2. All over the place you are calling setColor(bulb) and initializing the color and saturation later.
  3. You may also want to refactor the installed, updates and initialize function. Create your skeleton code from the IDE by creating a SmartApp. It will auto-generate these. And then plug in your code.

Take a look at “Notify my Hue” template as how to setColor.

No I took all of that out before.

definition(
name: “Temperature Bulb”,
namespace: “tagz379”,
author: “Joe Battaglia”,
description: “Changes color of Bulb based on Temperature”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“Devices”) {
input “sensor”, “capability.temperatureMeasurement”, title: “Select Temperature Sensor”, required:true
input “bulb”, “capability.colorControl”, title: “Select Bulb”, required:true, multiple:true
}
}

def installed() {
log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {
subscribe (tempSensor, “temperature”, temperatureHandler)
}

def temperatureHandler(evt) {
log.debug “temperatureHandler: $evt”

def hueColor = 0
def saturation = 100




if (evt.value < "20") { //Pink
hueColor = 83  
}
else if (evt.value > "30") { //Purple
hueColor = 75
}
else if (evt.value > "50") { //Blue
hueColor = 60//60
}
else if (evt.value > "60") { //Green
hueColor = 39//39
}
else if (evt.value > "70") { //Yellow
hueColor = 25
}
else if (evt.value > "80") { //Orange
hueColor = 10
}
else if (evt.value > "90") { //Red
hueColor = 100
}

def newValue = [hue: hueColor, saturation: saturation, level: lightLevel as Integer ?: 100]
bulb*.setColor(newValue)

}

How did you learn how to program in groovy?

Looking at other’s code and learning from experts in the community who are way way in a far superior league. :wink: I come from a Microsoft background and last couple of years Sencha ExtJs which is a JavaScript framework and .NET/C#.

Having spent several years working on a massive ExtJS app, those two words strike terror in me (not that it’s a bad framework, but when you really push the layout engine and start throwing 10’s of thousands of objects at a table grid, it gets… interesting :slight_smile: )

@Joe_Battaglia we just put out a new tutorial for understanding some of the basics of Groovy. It’s by no means a complete reference or inclusive of everything cool/different about Groovy, but does cover many of the core principles/differences with other languages. It also has some good links to other places where you can learn more.

It has been a nightmare but paging does help a little. 5/6 is a little better. And to think of it we support IE 9, 10 and 11 (just dropped support for IE 8).

Yeah we were stuck on 3, and had to support 100k rows in an un-paged grid, all the way back to IE 8. Let’s just say we had to open up the hood and modify Ext… a lot. :slight_smile: OK, back on topic, sorry for kind of forking the thread :slight_smile: