Skip to content

Instantly share code, notes, and snippets.

@shervinshaikh
Created July 22, 2014 23:04
Show Gist options
  • Save shervinshaikh/a0abd01f37b0918548c1 to your computer and use it in GitHub Desktop.
Save shervinshaikh/a0abd01f37b0918548c1 to your computer and use it in GitHub Desktop.
/**
* test2
*
* Copyright 2014 Shervin Shaikh
*
* 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: "T2",
namespace: "",
author: "Shervin Shaikh",
description: "Only records temperature & motion when it changes.",
category: "",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
)
preferences {
section("Configure") {
input "multisensor", "capability.temperatureMeasurement", title: "Which temp?"
input "motionsensor1", "capability.motionSensor", title: "Which motion?"
input "motionsensor2", "capability.motionSensor", title: "Which motion?"
}
}
def installed() {
log.debug "${multisensor.temperature}"
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "TEMP: ${multisensor.temperature}"
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(multisensor, "temperature", tHandler)
subscribe(motionsensor1, "motion", mHandler1)
subscribe(motionsensor2, "motion", mHandler2)
}
def parseHttpResponse(response) {
log.debug "HTTP Response: ${response} + ${response.status}"
}
def tHandler(evt) {
def temp = evt.value // e.g.: 0,0,-1000
if (temp) {
log.debug temp
writeXivelyData("FEED_ID_GOES_HERE", "temp2", temp)
}
else {
log.warn "COULD NOT FIND LATEST TEMP STATE FOR: ${multisensor}"
}
}
def mHandler1(evt) {
def motion = evt.value
log.debug "Motion1? ${motion}"
if(motion == "active"){
writeXivelyData("FEED_ID_GOES_HERE", "motion", 1)
} else if (motion == "inactive"){
writeXivelyData("FEED_ID_GOES_HERE", "motion", 0)
}
sendMeshblu(1, motion)
saveMeshblu(1, motion, "DEVICE_UUID", "DEVICE_TOKEN")
}
def mHandler2(evt) {
def motion = evt.value
log.debug "Motion2? ${motion}"
if(motion == "active"){
writeXivelyData("FEED_ID_GOES_HERE", "motion2", 1)
} else if (motion == "inactive"){
writeXivelyData("FEED_ID_GOES_HERE", "motion2", 0)
}
sendMeshblu(2, motion)
saveMeshblu(2, motion, "DEVICE_UUID", "DEVICE_TOKEN")
}
def saveMeshblu(num, motion, uuid, token){
def uri = "http://skynet.im/data/${uuid}"
def body = "motion=${motion}"
def headers = [
"skynet_auth_uuid" : uuid,
"skynet_auth_token": token
]
def params = [
uri: uri,
headers: headers,
body: body
]
httpPost(params) {response -> parseHttpResponse(response)}
}
def sendMeshblu(num, motion){
log.debug "\"motion\":\"${motion}\", \"num\":\"${num}\""
def uri = "http://skynet.im/messages"
def json = "{\"devices\": \"DEVICES_UUID_GOES_HERE\", \"payload\": { \"dat\": { \"motion\":\"${motion}\", \"num\":\"${num}\" }, \"typ\": \"room\" } }"
def headers = [
"skynet_auth_uuid" : "AUTH_UUID_GOES_HERE",
"skynet_auth_token": "AUTH_TOKEN_GOES_HERE"
]
def params = [
uri: uri,
headers: headers,
body: json
]
httpPostJson(params) {response -> parseHttpResponse(response)}
}
def writeXivelyData(feed, channel, value) {
def uri = "https://api.xively.com/v2/feeds/${feed}.json"
def json = "{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"${channel}\",\"current_value\":\"${value}\"}]}"
def headers = [
"X-ApiKey" : "API_KEY_GOES_HERE"
]
def params = [
uri: uri,
headers: headers,
body: json
]
httpPutJson(params) {response -> parseHttpResponse(response)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment