Power monitoring with CurrentCost

CurrentCost monitor

The CurrentCost power monitor has become very popular amongst amateur home automators and those technically-savvy who want to keep an eye on how much electricity they are using (and ultimately how much they are going to have to pay in bills). A couple of months ago I purchased the CurrentCost device and a USB cable to connect it to a computer from eBay. Having just seen their eBay store, it looks like they’ve got a fantastic new model on the way, but this article is about the older version.

By itself the device works very well, providing an at-a-glance view of current power use and a history over several periods of time. There is a simple bar chart showing the relative amounts of power used in the previous day, split into day time, evening and night. However if you want a clearer understanding of how power use changes day-to-day or even over a decade, connecting the device to a computer will provide the ability to store every reading in a database and/or produce live graphs of usage.

Several people have written articles on how to do the logging, each using a slightly different method (and language) to do so. Most of them are for linux though, but any programmer should be able to port the ideas across to a different platform such as Windows or Mac OS. For my implementation, I followed this guide on jibble.org.

This is what my perl script turned out like:

#!/usr/bin/perl -w
# Reads data from a Current Cost device via serial port.

use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );

my $PORT = "/dev/ttyUSB1";
my $ob = Device::SerialPort->new($PORT);

$ob->baudrate(9600);
$ob->write_settings;

open(SERIAL, "+>$PORT");
while (my $line = <SERIAL>) {
    if ($line =~ m!<ch1><watts>0*(\d+)</watts></ch1>.*<tmpr>([\d.]+)</tmpr>!) {
        my $watts = $1;
        my $temp = $2;
        print `/usr/bin/rrdtool update /var/scripts/currentcost/powertemp.rrd N:$watts:$temp`
    }
}

Note that because I’m connecting via a USB port, the serial port name is different. The baud is also 9600 rather than 2800. I have set this script to run at startup (detatched from the console) so that it is always running in the background.

I have also produced another simple script, which is run every minute as a cron job.  The script generates the graphs from the data stored in the RRD file, and uploads them to my web server via SSH.

#!/bin/sh

rrdtool graph /var/scripts/currentcost/power-10min.png --start end-10m --width 323 --end now --slope-mode --no-legend --vertical-label Watts --lower-limit 0 --alt-autoscale-max DEF:Power=/var/scripts/currentcost/powertemp.rrd:Power:AVERAGE LINE1:Power#0000FF:"Average" > /dev/null
rrdtool graph /var/scripts/currentcost/power-60min.png --start end-60m --width 323 --end now --slope-mode --no-legend --vertical-label Watts --lower-limit 0 --alt-autoscale-max DEF:Power=/var/scripts/currentcost/powertemp.rrd:Power:AVERAGE LINE1:Power#0000FF:"Average" > /dev/null
rrdtool graph /var/scripts/currentcost/power-day.png --start end-24h --width 323 --end now --slope-mode --no-legend --vertical-label Watts --lower-limit 0 --alt-autoscale-max DEF:Power=/var/scripts/currentcost/powertemp.rrd:Power:AVERAGE LINE1:Power#0000FF:"Average" > /dev/null
rrdtool graph /var/scripts/currentcost/power-week.png --start end-1weeks --width 323 --end now --slope-mode --no-legend --vertical-label Watts --lower-limit 0 --alt-autoscale-max DEF:Power=/var/scripts/currentcost/powertemp.rrd:Power:AVERAGE LINE1:Power#0000FF:"Average" > /dev/null
rrdtool graph /var/scripts/currentcost/power-month.png --start end-1months --width 323 --end now --slope-mode --no-legend --vertical-label Watts --lower-limit 0 --alt-autoscale-max DEF:Power=/var/scripts/currentcost/powertemp.rrd:Power:AVERAGE LINE1:Power#0000FF:"Average" > /dev/null
rrdtool graph /var/scripts/currentcost/power-year.png --start end-1years --width 323 --end now --slope-mode --no-legend --vertical-label Watts --lower-limit 0 --alt-autoscale-max DEF:Power=/var/scripts/currentcost/powertemp.rrd:Power:AVERAGE LINE1:Power#0000FF:"Average" > /dev/null

#Copy locally
cp /var/scripts/currentcost/*.png /var/www/currentcost/

#Upload to mars
/usr/bin/scp /var/scripts/currentcost/*.png root@192.168.2.3:/var/www/vhosts/currentcost.elemental.killswtch.net

You can see the result on the Power page of this site. The graphs at the moment are fairly basic. The Jibble article includes some extra data in the graphs, which I will probably add at some point. For the moment though it does the job very well.

img_3599

The CC device is located downstairs, in the living room. Thanks to the serial output connector being an RJ45 socket, it was very easy to plug it into the exising CAT5 network (which I’m very glad I did) and run the signal to the cabinet in my room where it is then broken out through the RJ45 patch panel, through the RS232/USB cable and into the fileserver boron, which hosts the scripts.

USB cable

5 thoughts on “Power monitoring with CurrentCost”

  1. Hello,

    I’ve very new to this! CC128 arrived today, and I’m a Perl virgin. Is the script you post above the one you use for USB conncetivity? You mention “Note that because I’m connecting via a USB port, the serial port name is different.” What should the port name be for a USB connection?

    TIA!
    Mark

  2. The code above includes the reference to the USB port. However, depending on what other serial-based devices you have connected to your system, the port reference may change. In my case it’s /dev/ttyUSB1

    If you do `ls /dev/ttyUSB*` you will see what serial USB devices are registered on the system. If you’re lucky you will just have ttyUSB0, in which case subsitute “ttyUSB1” in my code for “ttyUSB0”. If you have others you may have to use trial and error, or something like `lsusb` to figure out which one is the correct one.

  3. I strongly suspect that it only works with 240V/50Hz supplies, since it has to make the assumption that it’s working with that voltage and ferquency in order to guess the power (it only measures current). I’m pretty sure there are similar devices for 110V/60Hz supplies though, and there are more expensive devices which are more accurate which measure current, voltage and frequency.

Leave a Reply

Your email address will not be published. Required fields are marked *