Help with device handler - URL/URI


#1

Hello,
I found some code to help started for my raspberry pi garage door opener.
It’s functioning well to open/close door, except I can’t get a status updated on status icon.

Basically button activates a URL call internally to a web server that controls actual switch relay.
I have a separate URL that provides current status of the door (again, raspberry pi has magnetic sensor on it).

It seems that “device.status” tile should update whenever I close or open the door, but it doesn’t.
Any help would be greatly appreciated.

metadata {
definition (name: "Garage Opener Test", namespace: "testspacehere.bla.bla", author: "blamonster") {
    capability "Actuator"
    capability "Switch"
    capability "Refresh"
}

// simulator metadata
simulator {
}

preferences {
        input "internal_ip", "text", title: "Internal IP", required: false
        input "internal_port", "text", title: "Internal Port (if not 80)", required: false
        input "internal_open_path", "text", title: "Internal Open Path", required: false
        input "internal_close_path", "text", title: "Internal Close Path", required: false
        input "internal_status_path", "text", title: "Internal Status Path", required: false
}

// UI tile definitions
tiles {
    standardTile("DoorStatus", "device.status", width: 3, height: 2, canChangeIcon: true) {
        state "open", label: "Open", action: "", icon: "st.doors.garage.garage-open", backgroundColor: "#ffffff", nextState: "closed"
        state "closed", label: "Closed", action: "", icon: "st.doors.garage.garage-closed", backgroundColor: "#79b821", nextState: "open"
    }
    standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: true) {
        state "off", label: 'Open', action: "switch.on", icon: "st.doors.garage.garage-opening", backgroundColor: "#ffffff", nextState: "on"
        state "on", label: 'Close', action: "switch.off", icon: "st.doors.garage.garage-closing", backgroundColor: "#79b821", nextState: "off"
    }
    main "DoorStatus"
    details (["DoorStatus","button"])
}
}

def installed() {
log.debug "installed()"
configure()
}

def initialize() {
	log.trace "initialize function was called"
}

def updated() {
log.debug "updated()"
def hosthex = convertIPtoHex(internal_ip).toUpperCase() 
	def porthex = convertPortToHex("80").toUpperCase()
device.deviceNetworkId = "$hosthex:$porthex" 
configure()
}

def configure(){
log.debug "Refreshing Information"
return postAction(internal_status_path)
}

def refresh() {
log.debug "refresh()"
postAction(internal_status_path)
}

def postAction(uri){
	def port
	if (internal_port) { port = "${internal_port}" } else { port = 80 }
  log.debug "url = ${internal_ip}:${port}${uri}"
  	def headers = [:]
	headers.put("HOST", "$internal_ip:$port")
  def result = new physicalgraph.device.HubAction(
method: "GET",
path: uri,
headers: headers
  )
  sendHubCommand(result)
}

def on() {
if (internal_open_path){
    sendEvent(name: "switch", value: "on")
    log.debug "Executing OPEN"
    postAction(internal_open_path)
}
}

def off() {
if (internal_close_path){
    sendEvent(name: "switch", value: "off")
    log.debug "Executing CLOSE"
    postAction(internal_close_path)
}
}
def parse(description) {
	def msg = parseLanMessage(description)
def body = msg.body
	log.debug "body: ${body}"
def events = []
if (body == "OK") return
if (body == "CLOSED") {
    //events << createEvent(name: "Status", value: "closed", isStateChange: true)
    sendEvent(name: "DoorStatus", value: "closed", isStateChange: true)
    log.debug "door status: closed" }
if (body == "OPEN") {
    //events << createEvent(name: "DoorStatus", value: "open", isStateChange: true)
    sendEvent(name: "DoorStatus", value: "open", isStateChange: true)
    log.debug "door status: open" }
//return events
}
private String convertIPtoHex(ipAddress) { 
String hex = ipAddress.tokenize( '.' ).collect {  String.format( '%02x', it.toInteger() ) }.join()
return hex
}

private String convertPortToHex(port) {
	String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}

(www.rboyapps.com - Make your home your butler!) #2

Define attribute "status", "string" in your DH metadata


#3

Thank you! That did it - I can update status.

Another side question: when I open device handler - my button (switch) has a wrong state. It always starts with open, even if current status is already open and it should be “close”.
How can I set a status on opening of a device handler so icon/button is updated to reflect real state?

Thanks again!!!