Monitoring Power Outages and Generator Status

UPS Generator Power Outage

This page contains affiliate links. If you purchase an item using an affiliate link I will receive a small commission at no cost to you. Affiliates do not influence my recommendations. Read my disclosures for more information.

Have you ever wanted to know when the power is out at your home? Or when the generator is running? Or when power is restored? Sure, many utilities provide updates on their web sites. However, if yours is like mine, they don’t provide any type of alerts or notifications. I decided this wasn’t good enough for me. Since my home is smart, why not set up my own notifications?

Background

I liked to be prepared for outages. In my area, I have power outages every year so eventually, I saved up to install a standby generator. It’s been great. One day I came home and it was running, and I thought it was odd that I didn’t know I had a power outage. That’s when I got the idea to monitor for power outages and when my generator is running.

Admittedly, my generator offers a paid service for this. They have an app that will alert me when the generator comes on or goes off, which would mean there’s a power outage. But it’s a recurring fee, and I’m not to keen on those. It also doesn’t offer any integration with 3rd parties, and I like for everything to integrate with Home Assistant.

I have a few things I’d like you to keep in mind as I take you through my journey of monitoring for power outages and generator activity in my smarthome. First, I know there are many ways to do these things, and I’m sure my way isn’t the best way, but I know it works. Second, although I use Home Assistant as the glue, I believe this approach could work with many smarthome hubs.

Monitoring power outages with my UPS

My CyberPower UPS

I have a Cyberpower UPS that provides backup power to the two main servers in my house. It connects to my computer via USB, like most of their other models. I’ve been pretty happy with CyberPower UPSs over the years, especially since they support Linux. CyberPower provides monitoring software that supports Macs, Windows, and Linux that provides detailed UPS status information. In Linux, I could also use Network UPS Tools (NUT). Since CyberPower provides a ready to use .deb file for Ubuntu, I decided to go with that instead.

CyberPower’s PowerPanel UPS monitoring software provides the following features:

  • Email Alerts
  • Auto Shutdown
  • Scheduled Shutdowns
  • Self Testing
  • Battery Status (capacity, runtime, etc.)

CyberPower PowerPanel overview and installation

The installation of the software is pretty straight forward. Simply download the .deb file and use your preferred program to install it. I like command line so I went with:

dpkg -i powerpanel_ver_amd64.deb

Next up, I ran the following command to test that the installation worked:

 sudo pwrstat -status

Which gave me this output:

The UPS information shows as following:

	Properties:
		Model Name................... CP1500AVRLCDa
		Firmware Number.............. CTHGZ2005289
		Rating Voltage............... 120 V
		Rating Power................. 900 Watt(1500 VA)

	Current UPS status:
		State........................ Normal
		Power Supply by.............. Utility Power
		Utility Voltage.............. 118 V
		Output Voltage............... 118 V
		Battery Capacity............. 100 %
		Remaining Runtime............ 21 min.
		Load......................... 279 Watt(31 %)
		Line Interaction............. None
		Test Result.................. Unknown

When the power is out I get a readout similar to this:

The UPS information shows as following:

	Properties:
		Model Name................... CP1500AVRLCDa
		Firmware Number.............. CTHGZ2005289
		Rating Voltage............... 120 V
		Rating Power................. 900 Watt(1500 VA)

	Current UPS status:
		State........................ Power Failure
		Power Supply by.............. Battery Power
		Utility Voltage.............. 0 V
		Output Voltage............... 120 V
		Battery Capacity............. 100 %
		Remaining Runtime............ 21 min.
		Load......................... 234 Watt(31 %)
		Line Interaction............. None
		Test Result.................. Unknown
        Last Power Event............. Blackout at 2020/02/23 07:18:56

The key fields to note are:

  • Power Supply by & State – These fields indicate whether the UPS is running on utility power or battery power.
  • Remaining Runtime – This is how long at the current load the UPS estimates the battery will last.
  • Last Power Event – This field indicates when power was lost.
See also  Backup Your Linux Install With Timeshift

CyberPower PowerPanel monitoring and alert setup

PowerPanel also installs a daemon that can alert you when the power goes out as well as shut down your machine before your battery runs out of power. The configuration file on my system is /etc/pwrstatd.conf. The key options I configured for sending an alert are:

powerfail-active = yes
powerfail-delay = 0
powerfail-cmd-path = /home/hth/bin/crons/poutage.bash

These options, in order, tell PowerPanel to run a shell script when there is a power outage, with no delay after a power outage. The final option gives the location of the script to run on power outage. I decided on a pretty simple script:

pwrstat -status |  mail -r "[email protected]" -s "Power Outage: Office UPS on Battery Power" [email protected]
sleep 60
pwrstat -status |  mail -r "[email protected]" -s "Power Outage Update: UPS Status 1 Minute Later" [email protected]

This is a simple three-line script. The first line sends an email that says the power is out and gives me the status of the UPS. Those are fake email addresses in the example above. I actually have it send an email to the email to text gateway for my mobile network provider so I get a text on my phone when the network is out.

Then, because my generator usually kicks in around 30 seconds, I have it send an additional update a minute later that should show the UPS is back on “Utility Power.” However, it will show as utility power if my generator is on or if power is fully restored, which led me to my next project

Monitoring power outages using my generator

My UPS can reliably tell me when the utility power goes out, but it can’t tell me when it’s back because it can’t distinguish between utility power and generator power. So I thought through a few different options for monitoring my utility power:

  • Add some type of smart relay to the automatic transfer switch connected to my generator. Ultimately decided against this because I didn’t want to mess with it.
  • Add some type of smart clamp to read power coming from the utility or generator. Problem is that smart versions of these clamps aren’t cheap, and I would still need a way to power them.
  • Pay for the monitoring service from Generac. No integration with the rest of my smarthome, and a recurring fee. No thank you.
  • Use the genmon project. This looks like a sweet project for Generac generators, but I worry future firmware upgrades might affect it. I also would have to take apart my generator a bit to install it. It has everything I need and more. However, all I really want is to know when my generator is running and when it isn’t.
See also  The Best Security Keys For Two-Factor Authentication

After some consideration, I passed on all these options and decided to keep it simple. When my generator is on, it makes a decent amount of noise and produces vibration. Why not just attach a vibration sensor to it to tell me when it is on and off?

Choosing a vibration sensor for my generator

Xiaomi Mijia Aqara Vibration Sensor for generator detection
Xiaomi Mijia Aqara Vibration Sensor

It turns out there aren’t a ton of vibration sensors on the market. I have both a Zigbee and a Z-wave network running with Home Assistant and that lead me to these 3 sensors:

Ultimately, I settled on the Aqara vibration sensor because it is only a vibration sensor and it is the least expensive.

Setting up the Aqara vibration sensor with Home Assistant

Set up was mostly straight forward. For reference, I use the Nortek (GoControl) HUSBZB-1 USB stick to power my Zigbee and Z-Wave networks. I simply go to the Home Assistant Configuration page and select ZHA integration towards the bottom of the page. Then I click on Add Devices. Here’s where it can get a bit tricky. Normally to pair Zigbee devices I just have to put them in pairing mode once. With this device, I had to keep pressing the pairing button each second (about 5-10 seconds in total) until it paired.

After pairing, Home Assistant created a binary sensor to indicate motion (vibration) and a battery level sensor. After testing and installing the Aqara sensor on the generator I added an additional MQTT sensor and some automations to determine if the generator is running.

Vibration sensor installed inside the generator housing
Vibration sensor installed inside the generator housing

Setting up Home Assistant generator monitoring sensors and automations

I set up an MQTT sensor for tracking when the generator is on or off to avoid false positives. A ball hitting the generator (I have kids) or a rock or a loud sound would set off the vibration sensor of the Aqara. So I wanted a sensor that would only indicate the generator was on after the sensor had detected vibration for a couple of minutes straight. Let’s start with the MQTT sensor (if you haven’t set up MQTT with Home Assistant, check out my how-to with Docker):

- platform: mqtt
    name: "Generator Status"
    state_topic: 'home/GeneratorStatus'
    qos: 2
    device_class: power
MQTT Generator Sensor In Home Assistant
MQTT Generator Sensor in Home Assistant

This code is placed in the binary_sensor: section of my configuration.yaml file. It’s a basic binary sensor. The power device_class gives it a nice icon in the Home Assistant frontend. Then I wrote two automations to set the generator sensor as on or off depending on the status of the vibration sensor:

- id: generator_is_on
  alias: Generator is on
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.generator_motion
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 1
      seconds: 30
  action:
  - service: mqtt.publish
    data:
      topic: home/GeneratorStatus
      payload: 'ON'
      retain: true
      qos: 2


- id: generator_is_off
  alias: Generator is off
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.generator_motion
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 30
  action:
  - service: mqtt.publish
    data:
      topic: home/GeneratorStatus
      payload: 'OFF'
      retain: true
      qos: 2

Basically, when the vibration sensor (binary_sensor.generator_motion) is on for 90 seconds the generator status is set to on. When the motion stops for 30 seconds the generator status is set to off.

See also  Shelly Plus 1 Review: Better Than the Original?

Next, I just need a couple of automations to send an alert (I use Hangouts for my alerts) when the generator is on or off:

- id: generator_started_running
  alias: Generator Started Running
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.generator_status
    from: 'off'
    to: 'on'
  action:
  - service: hangouts.send_message
    data:
      target:
      - name: Home Assistant
      message:
      - text: 'ALERT: The generator is running'


- id: generator_stopped_running
  alias: Generator Stopped Running
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.generator_status
    from: 'on'
    to: 'off'
  action:
  - service: hangouts.send_message
    data:
      target:
      - name: Home Assistant
      message:
      - text: 'ALERT: The generator stopped running'

That’s it! Now I know when my generator is running and when it’s not. In some ways, this is better than using a power clamp to detect power coming from the generator because this also tells me when my generator spins up to “exercise” and for how long. It runs once a week for 5-10 minutes.

Final Thoughts

Well, that was a long way to determine when my power is out and when it returns. I can use my UPS to determine when power is cut, and a simple Zigbee vibration sensor to determine when my generator is running (power outage) and when it stops (power returned). There are many other ways to do this, but this works for me!

Are you monitoring for power outages in your smarthome? How are you implementing it? Let me know in the comments or on Twitter!

Interested in supporting HomeTechHacker?

Have you found the content on this site useful? If so, are you interested in supporting me and this site? There’s no obligation of course, but I would really appreciate any support you can give. Below are a few ways you can show support:


Thank you! I really appreciate it!
Share this:

1 thought on “Monitoring Power Outages and Generator Status”

Comments are closed.

Monitoring Power Outages and Generator Status

by HomeTechHacker time to read: 8 min