createEvent doesn't have any effect

I am working on writing a device handler for a contact sensor.

In my parse method I call createEvent, and return that event from parse. This event doesn’t show up in recent activites in the app or in the live logs, and the icon in the app doesn’t change.

The live log does show my log message with the event:
Parse returned [name:contact, value:closed, isStateChange:true, displayed:true, linkText:Virtual, descriptionText:Virtual contact is closed]

Can anyone see what I’m doing wrong?

metadata {
definition (name: “Test Open/Closed Sensor”, namespace: “jwagoner0”, author: “”) {
capability "Contact Sensor"
capability "Sensor"
capability “Refresh”
}

simulator {
    status "open":   "body:eyJTdGF0ZSI6ICJvcGVuIn0="
    status "closed": "body:eyJTdGF0ZSI6ICJjbG9zZWQifQ=="
}

tiles {
    standardTile("contact", "device.contact", width: 3, height: 2) {
        state "open", label: '${name}', icon: "st.contact.contact.open", backgroundColor: "#e86d13"
        state "closed", label: '${name}', icon: "st.contact.contact.closed", backgroundColor: "#00A0DC"
    }
    standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat") {
        state "Refresh", action: "refresh.refresh", icon: "st.secondary.refresh"
    }
    main "contact"
    details(["contact", "refresh"])
}

}

def parse(String description) {
def evt
def map = [:]
def descMap = parseDescriptionAsMap(description)
def body = new String(descMap[“body”].decodeBase64())
def slurper = new JsonSlurper()
def result = slurper.parseText(body)

if (result.containsKey("State")) {
    def contactState = result.State
    state.contact = result.State
    log.debug("state.contact set to ${contactState}")
    if (result.State == "open") {
        evt = createEvent(name: "contact", value: "open")
    } else {
        evt = createEvent(name: "contact", value: "closed")
    }
}
log.debug "Parse returned $evt"
return evt

}

Try using a “sendEvent()” somewhere … just in case. Yes: Parse does an implied sendEvent() with its return value, but you need to isolate the problem one step at a time.

Hmm… now my parse isn’t getting called.

I can see the logs from calling my refresh method, and I can see the request hitting the web server. I put a log right at the top of parse and that isn’t showing up now. Works in the simulator though.

def refresh() {
def uri = "/sensor/${sensor_id}"
def headers = [:]
headers.put(“HOST”, getHostAddress())
headers.put(“AuthCode”, authcode)

def hubAction = new physicalgraph.device.HubAction(
    method: "GET",
    path: uri,
    headers: headers
)
log.debug("Executing hubAction ${uri} on " + getHostAddress())
sendHubCommand(hubAction)    

}

At some point I got the DNI messed up. Resetting the DNI has everything working now (sendEvent and createEvent both work).

1 Like

What are you using for the device’s DNI? If iphex:porthex you won’t ever see parse() called.

It needs to be the MAC address of the devices interface.

Edit: I see we were responding at the same time. Glad you figured it out!

1 Like

"Never trust the Simulator"™

3 Likes

Steve, actually I have the DNI set to iphex:porthex:sensorid and it is working fine.

I’m not sure why it’s working, as that’s not the correct implementation. Who knows, perhaps something changed on the platform with the last update.