Last active
December 20, 2015 10:30
-
-
Save cwittyman/6115667 to your computer and use it in GitHub Desktop.
SmartThings Motion Detector that pushes to Pushover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Motion Detect - Pushover | |
* | |
* Author: cwitty | |
*/ | |
// Automatically generated. Make future change here. | |
definition( | |
name: "Motion Detect", | |
namespace: "", | |
author: "[email protected]", | |
description: "Sends a notification for motion ", | |
category: "My Apps", | |
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", | |
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png", | |
oauth: true | |
) | |
preferences { | |
section("When motion is detected by..."){ | |
input "motion", "capability.motionSensor", title: "Motion Here", required: true, multiple: false | |
} | |
section("Send Pushover alert (optional)"){ | |
input "apiKey", "text", title: "Pushover API Key", required: false | |
input "userKey", "text", title: "Pushover User Key", required: false | |
input "deviceName", "text", title: "Pushover Device Name", required: false | |
input "messageText", "text", title: "Message Text", required: true | |
input "pushSettings", "enum", title: "SmartThings Push Notifications", required: false, | |
metadata :[ | |
values: ['Yes', 'No'] | |
] | |
input "priority", "enum", title: "Pushover Priority", required: false, | |
metadata :[ | |
values: ['Off', 'Low', 'Normal', 'High', 'Emergency'] | |
] | |
input "sound", "enum", title: "Pushover Sound", required: false, | |
metadata:[ | |
values:[ 'pushover', 'bike', 'bugle', 'cashregister', 'classical', 'cosmic', 'falling', 'gamelan', 'incoming', 'intermission', 'magic', | |
'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat', 'alien', 'climb', 'persistent', 'echo', 'updown', 'none' ] | |
] | |
} | |
} | |
def installed() { | |
log.debug "Installed with settings: ${settings}" | |
subscribeToEvents() | |
} | |
def updated() { | |
log.debug "Updated with settings: ${settings}" | |
unsubscribe() | |
subscribeToEvents() | |
} | |
def subscribeToEvents() { | |
subscribe(motion, "motion.active", motionDetected) | |
} | |
def motionDetected(evt) { | |
if(pushSettings == "Yes"){ | |
sendPush("Motion detected at ${motion.displayName}"); | |
} | |
if(apiKey && userKey && priority !='Off') | |
{ | |
log.debug "Sending Pushover with API Key [$apiKey] and User Key [$userKey]" | |
def postBody = [] | |
def pushPriority = 0 | |
// Set Priority for Pushover Notification | |
if(priority == "Low") | |
{ | |
pushPriority = -1 | |
} | |
else if(priority == "Normal") | |
{ | |
pushPriority = 0 | |
} | |
else if(priority == "High") | |
{ | |
pushPriority = 1 | |
} | |
else if(priority == "Emergency") | |
{ | |
pushPriority = 2 | |
} | |
if(deviceName) | |
{ | |
log.debug "Sending Pushover to Device: $deviceName" | |
if(pushPriority == 2) | |
{ | |
postBody = [token: "$apiKey", user: "$userKey", device: "$deviceName", message: "$messageText", priority: "$pushPriority", sound: "$sound", retry: "60", expire: "3600"] | |
} | |
else | |
{ | |
postBody = [token: "$apiKey", user: "$userKey", device: "$deviceName", message: "$messageText", priority: "$pushPriority", sound: "$sound"] | |
} | |
log.debug postBody | |
} | |
else | |
{ | |
log.debug "Sending Pushover to All Devices" | |
if(pushPriority == 2) | |
{ | |
postBody = [token: "$apiKey", user: "$userKey", message: "$messageText", priority: "$pushPriority", sound: "$sound", retry: "60", expire: "3600"] | |
} | |
else | |
{ | |
postBody = [token: "$apiKey", user: "$userKey", message: "$messageText", priority: "$pushPriority", sound: "$sound"] | |
} | |
log.debug postBody | |
} | |
def params = [ | |
uri: 'https://api.pushover.net/1/messages.json', | |
body: postBody | |
] | |
httpPost(params){ response -> | |
log.debug "Response Received: Status [$response.status]" | |
if(response.status != 200) | |
{ | |
sendPush("Received HTTP Error Response. Check Install Parameters.") | |
} | |
} | |
}//End if API | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment