Energy Monitoring and Alerts

by Mike520 in Circuits > Raspberry Pi

1348 Views, 0 Favorites, 0 Comments

Energy Monitoring and Alerts

energyMaster.PNG

The genesis of this project was my concern over high energy bills. I suspected it was my outdoor hot tub spa running during the cold winters. I then found that the same project was very useful to monitor and track energy consumption for my pool pump, irrigation system pumps, furnace, air conditioner, refrigerator, freezer, water heater, and for that matter the whole main house, separate workshop, and drinking water pump house. In the process, I discovered that it was easy to monitor for a system was drawing abnormally high energy, or had failed to operate on its assigned schedule. It was as simple as sending an email alert or cell phone text message from the Raspberry Pi.

The build and install was very simple. This project uses a Raspberry Pi and a low-cost ($20 USD) PZEM module to monitor the energy of a system or device. One PZEM module is required per individual system to be monitored. It is also possible to monitor the whole house or building consumption. The module communicates up to a 1,000 feet (300 meters) to a USB dongle that plugs into the RPi. The module uses a split-core current transformer (CT) to measure the current flowing in the electric wire. Said in another way, the split-core CT clips around the wire without removing any insulation and indirectly senses the current flow.

I found that under normal operation most systems and devices have energy consumption that is fairly constant. Frequency of operation may be fairly constant as well if controlled by a time clock. There are two algorithms to generate alerts. One is statistical based and self learns based on power consumption or run time. The other algorithm option is based on entering the horsepower size of the motor, or wattage for other systems. Alerts can be sent to your email or as an SMS to your phone.

energyMaster™ tracks energy used from the system devices and sends status reports with the following information.

Period stats:

  • Number of cycles and energy consumed
  • Minimum, maximum, and total run times

Per on cycle:

  • Run time and standard deviation
  • Average energy and standard deviation

Data Logging (time series of records) can be to any of the following:

  • Comma Separated Variable (CSV) file
  • MQTT (publish/subscribe message queue)
  • influxDB - SQL (multiple variations to be added)
  • Future options: IFTT, PubNub, Twilio, Cellular, and APRS

Supplies

Checkout the github website for further information on these parts.

https://github.com/BrucesHobbies/energyMaster

PZEM Module (about $20 USD)

  • For household Alternating Current (AC) devices: PZEM-016
  • For battery operated Direct Current (DC) devices: PZEM-017

Raspberry Pi system (if you don’t already own one) Any one of the following:

  • RPI-Zero
  • RPI 3B+
  • RPI 4B

Wiring

PZEM2.PNG
PZEM.PNG

The PZEM-016 includes instructions. Basically the split-core current transformer (CT) is clipped around the neutral or hot wire allowing indirect current sensing. The PZEM module will need to be plugged in to a power source to power the module (80 – 260 VAC range). The PZEM communicates over a single low-voltage twisted pair to a USB-dongle using RS-485 so it can run up to 300 meters or roughly 1,000 feet. The USB dongle is plugged into a USB port on the RPi4 or RPi3, or via a USB cable on the RPI zero. If you are at all unsure, be safe; consult an electrician for local electrical and building codes.

Setup the Raspberry Pi Operating System

There are multiple excellent tutorials on installing the RPi operating system on an SD-card that you may want to consult that go into the specific steps. Here is the link to the Raspberry Pi Operating System image: https://www.raspberrypi.org/software/operating-systems/

Before continuing make sure your operating system has been updated with the latest updates.

sudo apt-get update
sudo apt-get full-upgrade
sudo reboot now

Install the required libraries that energyMaster uses:

sudo pip3 install pymodbus
sudo pip3 install matplotlib

Download and Configure the Source Code

To get a copy of the source files type in the following git command assuming you have already installed git:

git clone  https://github.com/BrucesHobbies/energyMaster

next configure the energyMaster software. To add additional PZEM modules, add a name to the chanNames list in energyMaster.py.

chanNames = ["HotTub"]
# chanNames = ["HotTub", "PoolPump", “Furnace”, “AirCond”] # Example with 4 devices
chanPorts = ["/dev/ttyUSB0", "/dev/ttyUSB1","/dev/ttyUSB2", "/dev/ttyUSB3"]   # One entry per chanName[]
chanAddrs = [0x01, 0x01, 0x01, 0x01]                       # One entry per chanName[]
chanOnThresholds = [5, 5, 5, 5]                     # Watts, with one entry per chanName[]

The source code is 100% Python. For some systems, I discovered they have electronics that were always on so I needed to adjust the chanOnThreshold to a slightly higher value of 10 or 20 watts. Some devices had a power on and a power off sequence. Modifying alg.py allows the power on and off sequences to be excluded from the algorithms. To disable or enable status messages and alerts modify these lines:

statusMsgEnabled = 1                              # non zero enables sending of email / SMS text messages
statusMsgHHMM    = [12, 0]                        # Status message time to send [hh, mm]
alertMsgEnabled  = 0                              # non zero enables sending of email / SMS text messages
runTimeAlert = [30*60] * len(chanNames)           # Run time to trigger email / SMS text - seconds
minIntervalBtwEmails = [2*3600] * len(chanNames)  # Wait this long before sending another email - seconds

Run the Program

To run the program from LXTERM:

python3 energyMaster.py

On first time startup energyMaster will ask if you want to enter an email address or phone number for alerts and status messages. You can skip this step and come back to this step later. The entries are saved in the emailCfg.json file, so you would need to delete this file to get the program to ask you for the information again.

To update the energy monitoring algorithm, see the comments in alg.py.

To plot the logged data use the plotEnergyMaster.py which provides example plots. Customize to suit your interests.

python3 plotEnergyMaster.py

Use the buttons on the bottom of the plot windows to zoom and pan to the specific months, weeks, days, or hours of interest.

Auto Start at Boot Type the following command:

sudo crontab –e

Select the type of editor you are familiar with. I used nano. Add the following line at the end of the file and then press ctrl+O to write the file and ctrl+X to exit the nano editor.

@reboot sleep 60 && cd energyMaster && python3 energyMaster.py

I welcome comments and suggestions for improvements. Enjoy!