Skip to content

Instantly share code, notes, and snippets.

@rappleg
Created December 18, 2013 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rappleg/8025759 to your computer and use it in GitHub Desktop.
Save rappleg/8025759 to your computer and use it in GitHub Desktop.
Plant Link
/**
* PlantLink
*
* Author: SmartThings
* Date: 2013-12-17
*/
tiles {
valueTile("humidity", "device.humidity", width: 2, height: 2) {
state("humidity", label:'${currentValue}%', unit:"",
backgroundColors:[
[value: 31, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
)
}
valueTile("battery", "device.battery") {
state "battery", label:'${currentValue}%', unit:""
}
main(["humidity", "battery"])
details(["humidity", "battery"])
}
// Parse incoming device messages to generate events
def parse(String description) {
log.debug "Parse description $description"
def map = [:]
if (description?.startsWith("read attr -")) {
def descMap = parseDescriptionAsMap(description)
log.debug "Desc Map: $descMap"
if (descMap.cluster == "0405" && descMap.attrId == "0000") {
log.debug "Humidity"
map.name = "humidity"
map.value = calculateHumidity(descMap.value)
} else if (descMap.cluster == "0001" && descMap.attrId == "0000") {
log.debug "Battery"
map.name = "battery"
map.value = calculateBattery(descMap.value)
}
}
def result = null
if (map) {
result = createEvent(map)
}
log.debug "Parse returned $map"
return result
}
def parseDescriptionAsMap(description) {
(description - "read attr - ").split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
private calculateHumidity(value) {
def percent = Integer.parseInt(value, 16) / 100
// Make sure our percentage is between 0 - 100
percent = Math.max(0.0, Math.min(percent, 100.0))
percent
}
private calculateBattery(value) {
def min = 2300
def percent = (Integer.parseInt(value, 16) - min) / 10
// Make sure our percentage is between 0 - 100
percent = Math.max(0.0, Math.min(percent, 100.0))
percent
}
private hex(value) {
new BigInteger(Math.round(value).toString()).toString(16)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment