Cannot get Writing Your First SmartApp to Work

Created MotionDetector/Switch SmartApp–essentially copied the thir writeup but just changed the name. When I click on play on the MotionDetector icon, the light does not come on. Here’s the code. Please assist. Thanks.

/**

  • MotionDetector/Switch
  • Copyright 2016 Dan Dunne
  • 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: “MotionDetectorSwitch”,
namespace: “DanMotionDetector”,
author: “Dan Dunne”,
description: “Detect motion and turn on light”,
category: “Convenience”,
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(“Turn on when motion detected:”) {
input “themotion”, “capability.motionSensor”, required: true, title: “where?”
}
section(“Turn on this light”) {
input “theswitch”, “capability.switch”, required: true
}
}

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

initialize()

}

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

unsubscribe()
initialize()

}

def initialize() {
subscribe(themotion, “motion.active”, motionDetectedHandler)

}

def motionDetectedHandler(evt) {
log.debug "motionDetectedHandler called: $evt"
theswitch.on()

}

// TODO: implement event handlers

Yeah, welcome to st development, the docs challenge you to figure out what is wrong with the code.

Sarcasm is a coping method. St doesn’t test the docs against the platform as the platform gets updated.

But this is a good way for you to get used to how broken things are on this platform. You will spend an awful lot of time defensively coding around a multitude of changing priorities when it comes to docs, resources and most of all promises.

Sorry, wish I could help, but this the reality of ST.

1 Like

Hey @drdand100…did you ever figure this out? I had to learn to program the ST environment ‘the hard way’ with little help, so I wanted to ‘pay it forward’ to repay those who DID help me.

I am not sure how you determined that your code doesn’t work; I loaded it up and it worked fine. The only suggestion is the the debug line should have something like this:

log.debug "motionDetectedHandler called: ${evt.value}"

This will allow you to see the actual value of the event instead of just some random information.

The two things that will be your friend in using SmartThings is the Live Logging and debug commands.

Anyway, good luck!

I’m not seeing any issues with the code either, but if you’re using the simulator to test it, that’s most likely your problem.

I stopped using the simulator because I was constantly troubleshooting bugs that didn’t exist. Now I always test by installing the SmartApp and using real or virtual devices.

Live Logging doesn’t always show entries in the correct order and sometimes skips them entirely, but it’s still the only tool that makes developing on the SmartThings platform bearable.

1 Like

Hi Michael,

Thanks for your input.

As per your suggestion, when I modify the login to $(evt.value)” I get this error message:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1474914100218693966152.groovy: 55: illegal string body character after dollar sign;solution: either escape a literal dollar sign “$5” or bracket the value expression “${5}” @ line 55, column 6.
log.debug “motionDetectedHandler called: $(evt.value)”

Any other suggestions? Thanks for your help.
^

Dan

Dan Dunne, Ph.D.

Senior Instructional Designer

Senior Technical Writer

Email: mailto:dandunne@sbcglobal.net dandunne@sbcglobal.net

Mobile: 408 230-9313

1075 Space Park Way #310

Mountain View CA 94043

Hi Patrick,

Thanks for your input and the warning.

Dan

Dan Dunne, Ph.D.

Senior Instructional Designer

Senior Technical Writer

Email: mailto:dandunne@sbcglobal.net dandunne@sbcglobal.net

Mobile: 408 230-9313

1075 Space Park Way #310

Mountain View CA 94043

I would have to see your code to figure out what might cause this. I recommending getting a GitHub account so the formatting is a bit easier to see.

However, your issue is odd…even with the bad formatting in the forum post, I was able to simple copy/paste your code into the IDE. It sounds like you may have missed a critical bracket ( { or } ) in your copy/paste.

should be:

log.debug "motionDetectedHandler called: ${evt.value}"
(string interpolation uses ${} not $() - easy mistake ;))

That said, once that’s fixed it’s possible you were running into an issue with the simulator. I’m going to update the docs to provide a warning at least that unfortunately some might run into an issue with the simulator not working correctly :frowning:

These types of phantom errors are the reason why I stopped using the simulator a long time ago.

Installing the SmartApp and using real or virtual devices may create a couple of extra steps, but I’d much rather do that than waste time troubleshooting errors in my code that don’t exist.

1 Like