0

I am developing a graph system for energy consumption. The script is pulling the current Kwh of an energymeter and is then pushed to a rrd database with DS COUNTER. I have tried to use VDEF to count the total of an DEF with average but I cant seem to find a good way to make "steps" with this approach.

My goal is to to be able to fetch data for one week from database and then calculate the total of each day. I can not seem to find the best way to do this. Is RRDtool the propel way to do this? If so, what is the correct approach?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
EveGun
  • 9
  • 2

1 Answers1

1

RRDTool works best when recording a rate. Therefore, by using a COUNTER DS type, you can change your periodic kWh total to a point-in-time W value in the database.

For example, if you are sampling every minute, then you can multiply your kWh by 3600000 (to get Watt-seconds, or Joules) and have RRDTool use a COUNTER type to convert this to a per-second rate, or Watts (it is always better to store the data in SI units for clarity and simplicity).

You can then display or calculate this by multiplying by the step size.

For example, if you wish to see a graph of kWh used per day, you need to have a 1-day granularity RRA defined. Then, you can fetch the data from this (either using rrdtool fetch, or rrdtool graph with a specific data granularity) and have a calculated value obtained by multiplying the average Watts by the Interval, and then dividing by 3600000 to get kWh.

This creates an RRD file with a 1-minute sample rate, to store watts.

rrdtool create power.rrd --step 60 \
  DS:watts:COUNTER:120:0: \
  RRA:AVERAGE:0.5:1:86400 \
  RRA:AVERAGE:0.5:1440:400

This will update the RRD, given your latest sample of kWh

JOULES=`expr $KWH \* 3600000`
rrdtool update power.rrd N:$JOULES

This will graph the daily usage, in kWh, over a month

rrdtool graph graph.png \
  --end now  --start "end-1month"  --step 86400 \
  --title "Power usage per day"  --vertical-label "kWh" \
  --units-exponent 0 \
  DEF:watts=power.rrd:watts:AVERAGE:step=86400 \
  CDEF:kwh=watts,STEPWIDTH,*,3600,/,1000,/ \
  VDEF:totkwh=watts,TOTAL,3600000,/ \
  GRPINT:totkwh:"Total kWh this month: %.0lf kWh"

You can, of course, modify this to your needs. You may want different RRAs, or graphing steps, or summarisation.

Steve Shipway
  • 3,754
  • 3
  • 22
  • 39