If on then turn off? Help needed

Hello!
I am trying to build my first smartapp. But I just can’t get it to work.

What i want to do is when a switch is tiggered a light should turn on if it’s off, and off if it’s on.

I have scaled down my code to figure out where it goes wrong…

My code looks like this

def Single(evt) {
log.debug "Single activated"

def asdf = switches.currentState("switch")

log.debug "asdf = ${asdf.value}"


if (${asdf.value} == "on") {
log.debug "Lights is on"

}

The log says

f69df4c5-e324-400e-aa82-1002dc19f8c0 17:55:03: error groovy.lang.MissingMethodException: No signature of method: script_app_Fibaro_sce_23e7aefb_b5c4_43d5_a7cd_ff9f85e9f105_ver_5_4.$() is applicable for argument types: (script_app_Fibaro_sce_23e7aefb_b5c4_43d5_a7cd_ff9f85e9f105_ver_5_4$_Single_closure2) values: [script_app_Fibaro_sce_23e7aefb_b5c4_43d5_a7cd_ff9f85e9f105_ver_5_4$_Single_closure2@676b3c06]
Possible solutions: is(java.lang.Object), now(), url(), run(), any(), app(java.util.Map)
f69df4c5-e324-400e-aa82-1002dc19f8c0 17:55:03: debug asdf = [on]
f69df4c5-e324-400e-aa82-1002dc19f8c0 17:55:03: debug Single activated

What the heck am I doing wrong?

Rather than reinvent the wheel…you might want to just install and use webCoRE. I thought about building my own SmartApps for a while too…but there’s really nothing I’ve wanted to do that webCoRE couldn’t handle. You should check it out.

I have tried webCoRE, but i want to reinvent the wheel. :slight_smile:

But thanks for the tip.

2 Likes

Many ways to do this. Here’s how I toggle lights in my Advanced Button Controller smartApp. Code was taken from the default Button Controller template. You don’t need the siren portion of the code but you can customize as you see fit. Hope it points you in the right direction.

def toggle(devices) {
	log.debug "Toggling: $devices"
	if (devices*.currentValue('switch').contains('on')) {
		devices.off()
	}
	else if (devices*.currentValue('switch').contains('off')) {
		devices.on()
	}
	else if (devices*.currentValue('alarm').contains('off')) {
        devices.siren()
    }
	else {
		devices.on()
	}
}

I tried to change my code to

def Single(evt) {

log.debug “Single activated”

def switchState = slave.currentState(“switch”)
log.debug "switch $switchState.value"
if (switchState.contains(“on”)) {
log.debug “Switch off”
} else if (switchState.contains(“off”)) {
log.debug “Switch on”
}
}

The log says [on] but the if statement never matches… What am I doing wrong?

I don’t really understand what you are doing with the above code. I assume you are subscribing to the switch events.
Shouldn’t your code be…

Def switchState=evt.value

Then you can compare the value and execute accordingly…
If(switchState==‘on’)

In my example in the previous post, I am passing the switches (devices) and comparing their states. In your case you seem to be passing the on/off event itself. If I’m wrong provide more details.

Please post your entire code, not easy to help otherwise.

The

MissingMethodException: No signature of method

is a generic error saying you are calling a method on an object that doesn’t support it.

So you have to make sure you are referencing the right object before getting its state and make sure this method works on the class of object you are using.

I am not fan of Piston things, but something very true about “avoid reinventing the wheel”. When I have difficulties coding something in ST, I check in the forum if someone coded it previously and I also check the ST public GitHub. There might be a lot of example of getting an object state from a smartapp, I am thinking about NotifyMeWhen as a very basic one.

I threw away my code and started over. I’m using the code Stephan suggested and now it does what it’s supposed to.

But I can’t understand why the If-statement didn’t match. Can it be the [ ] brackets that messed things up?

Anyways, thanks for the help :slight_smile: