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

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?

1 Like

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

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.

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!

@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!

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

}

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)
        }
    }
}

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

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.

@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:

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.

Hey Alan - just wanted to post a “thank you” to you for sharing this smartapp - it was exactly what I was looking for. I modified it ever so slightly so that I could have the InPowerCut state trigger a virtual switch to “on” when there’s power, and “off” when the power goes out. This way I have a quick insight into power outages over the past week (in case I do not receive the push or sms notifications for whatever reason) within the virtual switch’s “Recently” tab in the ST app.

Now I just need to figure out how to have the push/sms notifications state my local time (versus UTC) - but that will be for another day.

Thanks again!!

Here is what I came up with:

There is a commercial Zwave power outage sensor that is noted above in this thread. Based on that, I realized (or someone mentioned) that the Ecolink Door/Window sensor also has the a pair of terminals inside that allows you to put an auxiliary ‘switch’ that changes the open/close state. Essentially you short two contacts for closed or leave them open for open.

To “test” the A/C power in the house, I used a “RIB” Relay In a Box RIBU1C that can take 120v current on one side and has both “normally open” and “normally closed” relays inside. “Normally” means “with power applied”. So I hooked up the “normally closed” side so when power is applied to relay, it keeps the circuit closed. As soon as power is lost, the relay opens the circuit

Next, I set up monitoring for an “open” condition like you would for an open door or window. In my case, I simply have a rule written so when that sensor changes state, I get a text message saying it changed states.

For anyone who is interested, here’s the wiring:

  • White/Black Stripe is 120V Hot
  • White/Blue Stripe is 120V Common
  • Solid Orange and Solid Yellow is the open/close pair that will be normally closed.
  • You can adjust the wires if you need 10-30VAC/DC or you want the sensor to be normally open.
  • You only need 4 wires and the relay comes prewired (thus the name, relay in a box) with 6 wires. Tape the unneeded ones off.

The relay is threaded so I mounted it in a 1/2 inch knockout on a 1-gang galvanized box, and will mount the sensor by drilling a 1/4" hole in that box and mounting on a blank plate. An old recycled lamp cord and a knockout strain relief provides power to the unit.

I’ll try to grab a snapshot and post it some time soon.

Enjoy!

1 Like

Also see:

This:

Then use one of the sensors with dry contacts.

2 Likes

Outlets won’t report a power outage, but only a switch action as you have discovered. An outlet switch has to have power to operate. If the power is off, it can’t communicate with the hub. Instead, you have to use a sensor with dry contacts - like many door/window open-close sensors. The ones that worked for me are the good old-fashioned EcoSmart DWZWAVE2.5-ECO model that’s been around a long time. They have a dry contact inside that works to sense an open/closed circuit. That, with a relay like the one in my post above, or in @rontalley’s post below will do the power on/off sensing. The battery powered Door/Window sensor is what communicates with the ST hub to tell it of a status (ie: power) change.

2 Likes

Just set mine up yesterday, working great :+1:

1 Like

Yup! Same like my wired Door bell