SmartThings Community

Fire event failed: createEvent() and return in parse() not working, sendEvent() works


#1

Hi, guys,

I am trying to write my own Device Handler, but I couldn’t fire an event with createEvent() in the parse().
Below is more information about the problem I met.

I am writing SmartAPPs and Device Handlers without a HUB.
The SmartAPPs are working, but some functions of Device Handlers are not.

I want to fire events in the Device Handlers.
If I used sendEvent() to do that, everything works fine.
The event is fired successfully (the events can be found in the event list of My Location in the IDE).

However, if I try to use createEvent() to fire the events.
The events were never fired since they didn’t show in the event list, and I also used a SmartApp to subscribe the event, the event handler was not triggered, either.

Could you guys tell me where I did wrong?

Thanks!

The codes are shown below.

/**

  • Z-test-Switch
  • Copyright 2018 B
  • 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.

*/
metadata {
definition (name: “Z-test-Switch”, namespace: “000000”, author: “B”) {
capability “Switch”
}

simulator {
// TODO: define status and reply messages here
}

tiles {
// TODO: define your main and details tiles here
}
}

// parse events into attributes
def parse(String description) {
log.debug “execute Parsing ‘${description}’”

def result = createEvent(name: “switch”, value: description)
result.each{k,v ->
log.debug “$k $v”
}

return result
}

// handle commands
def on() {
log.debug “Executing ‘on’”
parse(“on”)
}

def off() {
log.debug “Executing ‘off’”
parse(“off”)
}


(Tony Fleisher) #3

commands don’t implicitly result in events to update the device state. Typically they will send commands to the physical device and events aren’t created until the response comes back.
you might have a look at the sample code for simulated devices.


#4

I’m guessing you are expecting the events to be fired off automatically because you are returning the result of a createEvent() in the parse() method? However it is you that is calling parse() from your commands so there is nothing special about it and that simply won’t happen.

It is when parse() is called by internals of ‘SmartThings’ because it is passing on a message to your device handler for processing that the magic with the returned value happens. That will typically happen if you send a command to a device and a reply comes back, or the device is sending out status information.

I’ve read quite a few device handlers that simply have an empty parse() method, or a one line one to log any message that comes in (even though they don’t expect it to happen). They typically call sendEvent() in the command methods which is effectively what you are doing. They are typically simulating devices or sometimes making http requests and assuming they’ve been processed.


#5

Hi @TonyFleisher and @orangebucket, thanks for your replies.
That’s exactly what I am expecting, " the events to be fired off automatically because you are returning the result of a createEvent() in the parse() method".
I got the reason why no event was fired now. :+1:

So, come to a new question.
Do you guys happen to know how to make the parse() be called by the internals of SmartThings?

I really want to see the magic of firing events with createEvent() (not sendEvent()).
I figured that I should send commands to the device, which leads the device to create a reply. Then, it’s when the parse() will be called and fire the event.

But, I do NOT have a physical device or HUB at the moment.
so, I have to run the tests with simulator.
I have also read quite a few device handlers. But failed found one that could make the magic happen.

Any suggestions?
Thanks!


(Tony Fleisher) #6

probably the only option here would be to use the simulator.
https://docs.smartthings.com/en/latest/device-type-developers-guide/simulator-metadata.html

For your case i think you can just define status options to send values to the parse method from action buttons in the simulator


#7

Thanks TonyFleisher :+1: the events are successfully fired now.
BTW, events are fired only when choosing a virtual device to test in the simulator.
Events still can’t be fired when using a physical device.