Email Notifications SmartApp v1.0 (requires this SmartApp and the PHP code below)
Note you need to change the URI field (2 spots) in this smartapp to match the location of the notify.php file below!
/**
* Copyright 2016 Inbound Computer Solutions, LLC
*
* 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.
*
* Version 1.0
*
*/
definition(
name: "Email Notifications",
namespace: "InboundCS",
author: "InboundCS",
description: "Receive email notifications when certain things happen....",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/mail_contact.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/mail_contact@2x.png"
)
preferences {
section("Notify which email address:"){
input "email", "text", title: "E-Mail Address", required: true
}
section("When the following happens:"){
input "lowbatt", "capability.battery", title: "Batteries Get Below 20%", multiple: true, required: false
input "tempSensor", "capability.temperatureMeasurement", title: "Temperature Sensor", required: false, multiple: true
input "temperature", "number", title: "Temp Gets Above", defaultValue: 80, required: false
input "temperaturelow", "number", title: "Temp Gets Below", defaultValue: 50, required: false
input "contact", "capability.contactSensor", title: "Door Opens", required: false, multiple: true
input "contactClosed", "capability.contactSensor", title: "Door Closes", required: false, multiple: true
input "water", "capability.waterSensor", title: "Water Detected", required: false, multiple: true
input "smoke", "capability.smokeDetector", title: "Smoke Detected", required: false, multiple: true
input "button", "capability.button", title: "Button Pushed", required: false, multiple: true
input "motion", "capability.motionSensor", title: "Motion Here", required: false, multiple: true
input "acceleration", "capability.accelerationSensor", title: "Acceleration Detected", required: false, multiple: true
input "mySwitch", "capability.switch", title: "Switch Turned On", required: false, multiple: true
input "mySwitchOff", "capability.switch", title: "Switch Turned Off", required: false, multiple: true
input "arrivalPresence", "capability.presenceSensor", title: "Arrival Of", required: false, multiple: true
input "departurePresence", "capability.presenceSensor", title: "Departure Of", required: false, multiple: true
}
section("Minimum time between messages (optional, defaults to every message)") {
input "frequency", "decimal", title: "Minutes", required: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
subscribeToEvents()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribeToEvents()
}
def subscribeToEvents() {
subscribe(tempSensor, "temperature", tempHandler)
subscribe(contact, "contact.open", eventHandler)
subscribe(contactClosed, "contact.closed", eventHandler)
subscribe(smoke, "smoke.detected", eventHandler)
subscribe(smoke, "smoke.tested", eventHandler)
subscribe(smoke, "carbonMonoxide.detected", eventHandler)
subscribe(water, "water.wet", eventHandler)
subscribe(button, "button.pushed", eventHandler)
subscribe(acceleration, "acceleration.active", eventHandler)
subscribe(motion, "motion.active", eventHandler)
subscribe(mySwitch, "switch.on", eventHandler)
subscribe(mySwitchOff, "switch.off", eventHandler)
subscribe(arrivalPresence, "presence.present", eventHandler)
subscribe(departurePresence, "presence.not present", eventHandler)
if (lowbatt) {
log.debug "Scheduling Battery Check"
schedule("0 0 10am 1,15 * ?", checkBattery)
checkBattery()
}
}
def eventHandler(evt) {
log.debug "Notify got event [Name: ${evt.name}], [Value: ${evt.value}], [Description: ${evt.descriptionText}]"
if (frequency) {
def lastTime = state[evt.deviceId]
if (lastTime == null || now() - lastTime >= frequency * 60000) {
sendMessage(evt)
}
}
else {
sendMessage(evt)
}
}
def tempHandler(evt) {
def tooHot = temperature
def tooCold = temperaturelow
log.debug "Notify got event [Name: ${evt.name}], [Value: ${evt.doubleValue}], [Thresholds: ${tooCold}, ${tooHot}], [Description: ${evt.descriptionText}]"
if (evt.doubleValue >= tooHot || evt.doubleValue <= tooCold) {
if (frequency) {
def lastTime = state[evt.deviceId]
if (lastTime == null || now() - lastTime >= frequency * 60000) {
sendMessage(evt)
}
}
else {
sendMessage(evt)
}
}
}
def checkBattery() {
log.debug "Checking Batteries"
def battery = lowbatt.currentValue("battery")
def whichDevice
def x=0
def stamp = new Date().format('yyyy-MM-dd hh:mm:ss a',location.timeZone)
while (x < 50) {
whichDevice = lowbatt[x]
if (whichDevice != null) {
String descText = "The ${whichDevice} battery is at ${battery[x]}%"
log.debug "${descText}"
if (battery[x] < 20) {
def params = [
uri: "http://CHANGE.ME/notify.php",
body: [
email: "${email}",
desc: "${descText}",
location: "${location.name}",
timestamp: "${stamp}"
]
]
log.debug params
try {
httpPostJson(params) { resp ->
log.debug "Received Response: ${resp.status}"
}
} catch (e) {
log.debug "Something went wrong: $e"
}
if (frequency) {
state[evt.deviceId] = now()
}
}
}
x=x+1
}
}
private sendMessage(evt) {
def params = [
uri: "http://CHANGE.ME/notify.php",
body: [
email: "${email}",
desc: "${evt.descriptionText}",
location: "${location.name}",
timestamp: "${evt.date.format('yyyy-MM-dd hh:mm:ss a',location.timeZone)}"
]
]
log.debug params
try {
httpPostJson(params) { resp ->
log.debug "Received Response: ${resp.status}"
}
} catch (e) {
log.debug "Something went wrong: $e"
}
if (frequency) {
state[evt.deviceId] = now()
}
}
Then you need to find a spot to host this notify.php file (be sure to change the 2 URI locations in the smart app above to match wherever this file is):
Notify.php v1.0 (you may need to change the $from_name and $from_email variables.)
<?php
/**
* Copyright 2016 Inbound Computer Solutions, LLC
*
* 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.
*
* Version 1.0
*
*/
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE );
$eventDescription = $input[desc][values][0];
$location = $input[location][values][0];
$time = $input[timestamp][values][0];
$to_email = $input[email][values][0];
$subject = "[SmartThings] {$location}";
$body = "{$location}: {$eventDescription} at {$time}";
$from_name = "SmartThings Notification";
$from_email = "change@me.com";
$headers = "From: \"".$from_name."\" <".$from_email.">\r\n";
$headers .= "Reply-To: \"".$from_name."\" <".$from_email.">\r\n";
$headers .= "Return-Path: \"".$from_name."\" <".$from_email.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n\n";
$headers .= $body."\n";
mail($to_email, $subject, '', $headers, "-f$from_email");
?>