Detect power out / off at Hub Location with Hub and Internet on UPS

poweroutage
power

#1

Ok all, time to throw this out there…

I have seen a number of posts about creating an app that could detect when power is out at home, but many relate to turning LED lights down from 100% when the power comes back on. My issue is simpler - to find out when the power has gone off at the ST hub location in the first place.

The scenario is for a user with a ST hub and internet connectivity on battery backup, so the Hub thinks there is still power (and ST does not notify as offline until the UPS dies). Of course we could tackle this by getting a UPS that can send SMS/text or other messages, but that’s no fun when it could be accomplished with ST :slight_smile:

The challenge is that I’m in IT, but am no programmer so I’m just throwing this out there while I grab snippets of code from here and there and try to assemble something that works. I would love to see someone tackle this who has more coding experience or who is willing to provide some advice or guidance. Maybe I’m biting off more than I can chew, but hey - I’m game to learn, regardless of the outcome.

Here’s what I have so far as potential requirements that ST code could handle, inspired by several existing apps:

  • Monitor an A/C only switch for its “Last Update At” as seen in Device page in the IDE.
  • If A/C power is detected as lost, (by creating a calculation comparing current time to the last update), send a notification or SMS message stating House Power has been lost based on no update from that switch in nn minutes. Note: some latency is not an issue. Even up to 30 minutes would be acceptable in my use case, which is simply to preserve food in multiple freezers and return to hook up a manual backup generator.
  • Using the above, devices with capability.energyMeter are natural options. Perhaps just capability.Switch would work, too - but i assumed energyMeter would be metering A/C electricity plugged in and wouldn’t ever be battery-powered.
  • For example, I could use a Aeotec Smart Switch 6, and using its configuration, set updates to 120 seconds to ensure “Last Update At” sees an update at least every 2 minutes. Then, in this proposed app, send an SMS or ST Notification if there are no updates from the selected switch in a user-selected number of minutes, (like 7-15 minutes - giving some lenience for missed updates but not waiting too long).

The apps that have ‘inspired’ me to think about this are:
@krlaframboise Kevin’s Simple Device Manager reminds me in his app that we can look at the last update time from the device and calculate the difference between now and that last update.
@twack Todd Wackford’s MimoLite Power Is Out uses this same concept to monitor a MimoLite device as a contact sensor and send the notification. I decided to try another capability that might cover a wider range of devices that are more likely to be A/C power (only) than only sensors, which in my case are 100% battery operated.

So now I have a bit of code as examples, have dabbled a bit and realize this is more than I can chew. Any takers, mentors or comments/discussion?


(Andy - United Kingdom) #2

You can do it with a little bit of hardware :slight_smile:


(Alan) #3

I’m no expert and my requirements will probably be different to yours…

I have recently put my, hub and router onto a small (2 hour worth) UPS with the view of saving the freezer contents by calling a neighbour if needed when we are away. A couple of times during electrical storms my RCD has tripped out and our neighbour could easily check this for us and save our freezer and smart setup generally.

So my requirements do not require short latency on the notifications, I just want to know when the power has been out for more than a couple of hours.

To do this I have three Smart Outlets in the house (used for random lamp lighting whilst away - but not relevant to this) and I have a script which polls the outlets offline/online status of them every minute. If all three go offline then I raise an SMS alert and then when at least one has come back I raise another alert to say the power is back. For my hub it can take up to 15 minutes to detect that the sockets have gone offline, the hub detecting them coming back on is much faster.

So for my use case the 15 minutes latency is acceptable, actually it’s desirable for me as I don’t want alerts for short outages.

If of interest let me know and I’ll post my script.
Otherwise good luck.

Regards
Alan.

PS - Perhaps it might help if you provided a little bit more use context and expectations around latency.


#4

Yes, @Alan1961, That is exactly the scenario I’m trying to create. Just like you, a 15 minute lag would actually be good - to avoid false alerts. I don’t need an immediate alert. Interestingly, we’re doing exactly the same thing. With 5 teenagers in the house, we have 3 freezers full of food that is the main driver for knowing when the power is out. A 15 minute latency would be quite acceptable.

Thanks!


#5

@Cobra, Thanks for the tip! I am handy with relays and like the “hack” you’ve proposed of the battery-operated door/window switch. If I can’t figure out how to automate it using some code, I would happily go the hardware route and use the already-available apps to manage the monitoring and alerts. Thanks for sharing!


(Alan) #6

In case of use or basis of your own…
Regards
Alan

definition(
name: “Power Monitor”,
namespace: “AlansSmithsStuff”,
author: “Alan Smith”,
description: “Periodically check my smart things sockets for online status and report power cur if all offline. Hub is on a UPS”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet@2x.png
)

preferences
{
section(“Which Smart Sockets …”)
{
input “switches”, “capability.switch”, multiple: true
}
section( “Notifications” )
{
input(“recipients”, “contact”, title: “Send notifications to”)
{
input “sendPushMessage”, “enum”, title: “Send a push notification?”, options: [“Yes”, “No”], required: false
input “phone1”, “phone”, title: “Send a Text Message?”, required: false
}
}
}

def installed()
{
def twhen = new Date()
log.debug "Power Monitor installed $twhen"
state.InPowerCut = false
unschedule()
runEvery1Minute(DoTest)
}

def updated()
{
def twhen = new Date()
log.debug "Power Monitor updated $twhen"
state.InPowerCut = false
unschedule()
runEvery1Minute(DoTest)
}

def DoTest()
{
log.debug "Testing Mains"
def AllOffline = true
switches.each
{
log.debug "testing $it.displayName --> $it.status"
if (it.status.toUpperCase() in [“ONLINE”]) AllOffline = false
}
def twhen = new Date()
if(AllOffline == true && state.InPowerCut == false)
{
state.InPowerCut = true
state.PowerCutStarted = now()
sendAlarm(“Power Cut Started $twhen”)
}
if(AllOffline == false && state.InPowerCut == true)
{
state.InPowerCut = false
state.PowerCutStarted = now()
sendAlarm(“Power Cut Ended $twhen”)
}

}

private sendAlarm(msg)
{
log.debug(msg)

if (location.contactBookEnabled) 
{
    log.debug("sending notifications to: ${recipients?.size()}")
    sendNotificationToContacts(msg, recipients)
}
else 
{
    if (sendPushMessage != "No") 
    {
        log.debug("sending push message")
        sendPush(msg)
    }

    if (phone1) 
    {
        log.debug("sending text message to ${phone1}")
        sendSms(phone1, msg+" sent to ${phone1} ")
    }
}

log.debug msg

}


(Andy - United Kingdom) #7

This is the app I put together to use with the hardware:
(can text or send message to contact book entries)
I use this for when my RCD trips and a second one for when the full power goes off.

/**
 *  Copyright 2017 SecurEndpoint
 *
 *  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.
 *
 *  Contact Alert
 *
 *  Author: Cobra
 *
 *  Version 1.1
 *
 *  Last Updated: 01/05/2017
 *
 *  Changes: 
 *  
 *  
 *  V1.1 Modified to use contact book
 *  V1.0 - 1st Draft
 *
 */



definition(
    name: "Alerts - Contact Sensor",
    namespace: "Cobra",
    author: "SecurEndpoint",
    description: "Get a message sent when contact(s) status changes ",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
	section("Select Contact(s)"){
		input "contact1", "capability.contactSensor", title: "Door/Windows Contact", required: true, multiple: true
		
		}
    section(""){
        input("recipients", "contact", title: "Send notifications to") {
            input(name: "sms", type: "phone", title: "Send A Text To", description: null, required: false)
            input(name: "pushNotification", type: "bool", title: "Send a push notification", description: null, defaultValue: true)
      }
        }
        
	section("Send this message on Contact Open"){
		input "openText", "text", title: "Message Text", required: false
	}
	section("Send this message on Contact Closed"){
		input "closedText", "text", title: "Message Text", required: false
	}


}

def installed(){
	initialize()
}
	

def updated(){
	unsubscribe()
	initialize()

}

def initialize() {
	subscribe(contact1, "contact.open", contactOpenHandler)
	subscribe(contact1, "contact.closed", contactClosedHandler)
}



def contactOpenHandler(evt) {

 def msg = "$openText"
    	    sendMessage(msg)

}

def contactClosedHandler(evt) {

	def msg = "$closedText"
    	    sendMessage(msg)

}



def sendMessage(msg) {
    if (location.contactBookEnabled) {
        sendNotificationToContacts(msg, recipients)
    }
    else {
        if (sms) {
            sendSms(sms, msg)
        }
        if (pushNotification) {
            sendPush(msg)
        }
    }
}

#9

Thanks @Cobra and @Alan1961! Looks like I’m going to have some fun this weekend :slight_smile: Maybe I’ll work on both!


(Ray) #10

I have quite a few ways to detect power outage but the easiest one is probably a smart outlet.
Have the UPS plug into the outlet and use CoRE/webCoRE to monitor the power usage. Notification when power drop below a certain level for x amount of time.
I also mod a contact sensor for detecting lost of power with a relay like this one.


#11

@Navat604, NICE! Thanks for sharing! I might have 3 ways to detect power out before this is all over - one easy, one coding, and one door sensor hack. This is going to be a fun month :smile:


(Harley) #12

If you already have a UPS which has a USB port and is supported by nut, you can monitor the UPS via USB/nut from a connected Raspberry Pi, and then send a Webhooks call via IFTTT which can, in turn, trigger any number of things in the Smartthings environment. Complete instructions here http://handyharley.blogspot.com/
Note that Raspberry Pi 3’s are Wi-Fi enabled, and are dirt cheap… likely the Raspberry Pi 3 kit and USB cable together would be cheaper than refilling the freezer with new food, even once, and you get a powerful Linux computer to boot!
I get a UPS-triggered virtual switch off flipped within my Smartthings hub within 1-2 seconds of my power going out, and flipped on when the power resumes. The virtual switch flip event itself can serve as a trigger to other Smartthings-based logic which can then reset the pre-power-outage state of devices (or not, based on a number of factors such as time of day/Mode/etc), turn the HVAC to Auto, whatever.