This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
currentcost
executable file
·819 lines (687 loc) · 24.2 KB
/
currentcost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#!/usr/bin/perl
# -*- perl -*-
=head1 NAME
=encoding utf8
currentcost - Munin plugin to monitor a CurrentCost energy monitor
=head1 APPLICABLE SYSTEMS
Any system connected to a CurrentCost monitor. These can be purchased from L<http://www.currentcost.com>.
=head1 CONFIGURATION
This plugin requires the following Perl modules. Either fetch them from CPAN or your distribution.
=over
=item *
XML::Simple
=item *
Device::SerialPort
=item *
YAML
=item *
Time::Local
=back
This configuration section shows the defaults of the plugin:
[currentcost]
env.device /dev/ttyUSB0
env.baud 2400
env.tick 6
env.currency £
env.rate1 13.9
env.rate1qty 900
env.rate2 8.2
env.nightrate 0
env.nighthours 23:30-06:30
env.standingcharge 0.0
env.metertype CC128
The configuration can be broken down into the following subsections:
=head2 DEVICE
=over
=item env.device
Specfies the device node where the CurrentCost monitor can be found. You may find it useful to use a udev rule to symlink this somewhere permanent.
=item env.baud
Specifies the baud rate to use. CurrentCost devices may speak at 2400, 9600 or 57600 baud, depending on their age.
=item env.tick
How long, in seconds, to consider data valid for. CurrentCost monitors typically put out data every 6 or 10 seconds. If Munin does a data run less than C<env.tick> seconds after a config run, there's no need to wait for more data.
=back
=head2 COSTS
=over
=item env.currency
The currency symbol to use on the cost graph. CurrentCost typically uses "E<pound>" or "E<euro>", but you may find "$" more to your taste.
=item env.rate1
The primary rate in hundredths of a C<env.currency> per kWh. (i.e. pence/cents per kWh)
=item env.rate1qty
How many kWh per month are charged at C<env.rate1>. Some tariffs charge one rate for the first so many units and then another rate for the remainder. If you are charged a flat rate per unit, set this to 0.
=item env.rate2
The secondary rate in hundredths of a C<env.currency> per kWh. (i.e. pence/cents per kWh)
=item env.nightrate
The night rate in hundredths of a C<env.currency> per kWh. Some tariffs (such as Economy 7) charge differently during the night and typically require a meter capable of reading two rates. If you do not have such a tariff, set this to 0.
=item env.nighthours
The time period for which C<env.nightrate> applies. This should be of the form C<hh:mm-hh:mm> and should span midnight.
=item env.standingcharge
The standing charge in hundreths of a C<env.currency> per month. If you do not have a standing charge, set this to 0.
=item env.metertype
The model of the meter. Currently "CC02" and "CC128" are supported.
=back
=head1 MAGIC MARKERS
#%# family=auto contrib
#%# capabilities=multigraph
=head1 AUTHOR
Paul Saunders L<darac@darac.org.uk>
=cut
use strict;
use warnings;
use utf8;
use Munin::Plugin;
use Data::Dump qw{pp};
need_multigraph();
my $device_node = $ENV{device} || "/dev/ttyUSB0";
my $baud_rate = $ENV{baud} || "2400"; # or 9600 or 57600
my $tick_rate = $ENV{tick} || "6";
# Tick_Rate is how long to consider data valid for (in seconds)
# Costs
my $currency = $ENV{currency} || "£"; # £ or €
my $rate1 = $ENV{rate1} || "13.9"; # in pence/cents
my $rate1qty = $ENV{rate1qty} || "900"; # in kWh, 0 to use fixed rate2
my $rate2 = $ENV{rate2} || "8.2"; # in pence/cents
my $nightrate = $ENV{nightrate} || "0"; # 0 = disabled
my $nighthours = $ENV{nighthours} || "23:30-06:30";
my $standingcharge = $ENV{standingcharge} || "0.0"; # pence/cents per month
my $metertype = $ENV{metertype} || "CC128";
my $MUNIN_DEBUG = $ENV{MUNIN_DEBUG} || 0;
my $ret;
if ( !eval "require XML::Simple;" ) {
$ret .= "Could not load XML::Simple; ";
}
if ( !eval "require Device::SerialPort;" ) {
$ret .= "Could not load Device::SerialPort; ";
}
if ( !eval "require YAML;" ) {
$ret .= "Could not load YAML; ";
}
if ( !eval "require Time::Local;" ) {
$ret .= "Could not load Time::Local; ";
}
if ( defined $ARGV[0] and $ARGV[0] eq 'autoconf' ) {
# Shouldn't autoconfigure as there's no reliable way to detect
# serial devices.
print "no\n";
exit 0;
}
my @lastread;
sub save_data {
my @savedata = @_;
# Do we need to save this data?
if ( !@lastread
or time >= Time::Local::timelocal(@lastread) + ($tick_rate) )
{
print "# Saving Data (Data is "
. ( time - Time::Local::timelocal(@lastread) )
. " seconds old)\n"
if $MUNIN_DEBUG;
@lastread = localtime(time);
my @save_vector;
push @save_vector, YAML::Dump(@lastread);
foreach ( split /\n/, YAML::Dump(@savedata) ) {
push @save_vector, $_;
}
save_state(@save_vector);
}
}
sub load_data {
# Bring the data back in
my @save_vector = restore_state();
# Read the timestamp, Do we need to refresh the data?
my @lastread = YAML::Load( shift @save_vector );
my $yamlstr = '';
foreach (@save_vector) {
$yamlstr .= "$_\n";
}
my @dataarray = YAML::Load($yamlstr);
if ( !@lastread
or time >= ( Time::Local::timelocal(@lastread) + ($tick_rate) ) )
{
# Data is stale
print "# Data is stale ("
. ( time - Time::Local::timelocal(@lastread) )
. " seconds). Re-reading device\n"
if $MUNIN_DEBUG;
eval {
# Fetch the XML
my @temparray;
if ( $metertype eq "CC128" ) {
@temparray = collect_cc128_data( $device_node, $baud_rate );
}
elsif ( $metertype eq "CC02" ) {
@temparray = collect_cc02_data( $device_node, $baud_rate );
}
else {
die "Unknown meter type $metertype";
}
# Read the time so we know whether to reset daily/monthly/yearly counters
my @now = localtime(time);
my %is_new;
$is_new{daily} = ( $now[3] != $lastread[3] ) ? 1 : 0;
$is_new{monthly} = ( $now[4] != $lastread[4] ) ? 1 : 0;
$is_new{yearly} = ( $now[5] != $lastread[5] ) ? 1 : 0;
for ( my $i = 0 ; $i <= $#temparray ; $i++ ) {
my $datum = $temparray[$i];
for ( my $j = 0 ; $j <= $#{ $datum->{data} } ; $j++ ) {
for my $period (qw(daily monthly yearly)) {
$period = "n$period" if is_night_rate();
if ( defined( $dataarray[$i]->{data}[$j]->{$period} ) )
{
# There's old data. Consider incrementing it
if ( $is_new{$period} ) {
# Start of a new period, reset the counter
print
"# Start of a new $period period. Resetting counter\n"
if $MUNIN_DEBUG;
$temparray[$i]->{data}[$j]->{$period} = 0;
}
else {
$temparray[$i]->{data}[$j]->{$period} =
$dataarray[$i]->{data}[$j]->{$period} +
$temparray[$i]->{data}[$j]->{value} / 12;
}
}
else {
# No old data. Set it.
$temparray[$i]->{data}[$j]->{$period} =
$temparray[$i]->{data}[$j]->{value} / 12;
}
}
}
}
# If the above threw an error, we won't overwrite the old data
@dataarray = @temparray;
1;
} or do {
print $@;
}
}
return @dataarray;
}
sub is_night_rate {
# Determine if we're on night rate
return 0 if not $nightrate;
my ( $nightstart, $nightstop ) = split /-/, $nighthours;
my ( $start_h, $start_m ) = split /:/, $nightstart;
my ( $stop_h, $stop_m ) = split /:/, $nightstop;
my $start_time = $start_m + ( $start_h * 60 );
my $stop_time = $stop_m + ( $stop_h * 60 );
my @now = localtime(time);
my $now_time = $now[1] + ( $now[2] * 60 );
if ( $now_time >= $start_time
or $now_time <= $stop_time )
{
print "# Night rate is ACTIVE\n" if $MUNIN_DEBUG;
return 1;
}
else {
print "# Night rate is enabled, but NOT active\n" if $MUNIN_DEBUG;
return 0;
}
}
=head1 EXAMPLE INPUT
The device will periodically output a string of XML. The string will be all on one line, they are expanded here for clarity.
=head2 Classic format
Note: I can't find an official spec for this format so, for now, we're using the definitions supplied by L<http://cumbers.wordpress.com/2008/05/07/breakdown-of-currentcost-xml-output/>:
<msg>
<date>
<dsb>00014</dsb> Days since birth
<hr>14</hr> \
<min>07</min> | The Time
<sec>07</sec> /
</date>
<src>
<name>CC02</name> Name of this Device
<id>03280</id> Communication channel for device
<type>1</type> Harware version
<sver>0.07</sver> Software version
</src>
<ch1>
<watts>00080</watts> Value from the first channel
</ch1>
<ch2>
<watts>00000</watts> Value from the second channel
</ch2>
<ch3>
<watts>00000</watts> Value from the second channel
</ch3>
<tmpr>28.8</tmpr> Current temperature (degrees celsius)
<hist>
<hrs>
<h02>000.0</h02> Total kWh used in 2 hour blocks
<h04>000.1</h04>
<h06>000.1</h06>
<h08>000.0</h08>
<h10>000.0</h10>
<h12>000.0</h12>
<h14>000.0</h14>
<h16>000.1</h16>
<h18>000.1</h18>
<h20>000.1</h20>
<h22>000.1</h22>
<h24>000.0</h24>
<h26>000.0</h26>
</hrs>
<days>
<d01>0000</d01> Total kWh used per day
<d02>0000</d02>
<d03>0000</d03>
<d04>0000</d04>
<d05>0000</d05>
<d06>0000</d06>
<d07>0000</d07>
<d08>0000</d08>
<d09>0000</d09>
<d10>0000</d10>
<d11>0000</d11>
<d12>0000</d12>
<d13>0000</d13>
<d14>0000</d14>
<d15>0000</d15>
<d16>0000</d16>
<d17>0000</d17>
<d18>0000</d18>
<d19>0000</d19>
<d20>0000</d20>
<d21>0000</d21>
<d22>0000</d22>
<d23>0000</d23>
<d24>0000</d24>
<d25>0000</d25>
<d26>0000</d26>
<d27>0000</d27>
<d28>0000</d28>
<d29>0000</d29>
<d30>0000</d30>
<d31>0000</d31>
</days>
<mths>
<m01>0000</m01> Total kWh used per month
<m02>0000</m02>
<m03>0000</m03>
<m04>0000</m04>
<m05>0000</m05>
<m06>0000</m06>
<m07>0000</m07>
<m08>0000</m08>
<m09>0000</m09>
<m10>0000</m10>
<m11>0000</m11>
<m12>0000</m12>
</mths>
<yrs>
<y1>0000000</y1> Total kWh used per year
<y2>0000000</y2>
<y3>0000000</y3>
<y4>0000000</y4>
</yrs>
</hist>
</msg>
=head2 CC128 format
For full definition, see L<http://www.currentcost.com/xx128/xml.html>
<msg> start of message
<src>CC128-v0.11</src> source and software version
<dsb>00089</dsb> days since birth, ie days run
<time>13:02:39</time> 24 hour clock time as displayed
<tmpr>18.7</tmpr> temperature as displayed
<sensor>1</sensor> Appliance Number as displayed
<id>01234</id> radio ID received from the sensor
<type>1</type> sensor Type, "1" = electricity
<ch1> sensor channel
<watts>00345</watts> data and units
</ch1>
<ch2>
<watts>02151</watts>
</ch2>
<ch3>
<watts>00000</watts>
</ch3>
</msg> end of message
=cut
sub collect_cc128_data {
# Read data from the serial port until we see a repeated sensor
my ( $port, $baud ) = @_;
my $tty = Device::SerialPort->new($port) || die "Can't open $port: $!";
$tty->baudrate($baud) || die "Can't set serial baudrate";
$tty->parity("none") || die "Can't set serial parity";
$tty->databits(8) || die "Can't set serial databits";
$tty->handshake("none") || die "Can't set serial handshake";
$tty->write_settings || die "Can't set serial parameters";
open( my $ttydev, "<", $port ) || die "Can't open $port: $?";
my @cc_data_arr;
my %seen_sensors;
while (<$ttydev>) {
print "# Read from device: $_\n" if $MUNIN_DEBUG;
if (m{(<msg>.*</msg>)}) {
my $xmlref = XML::Simple::XMLin( $1, KeepRoot => 1 );
my $sensor = $xmlref->{msg}->{sensor};
print "# Parsing Sensor $sensor\n" if $MUNIN_DEBUG;
next unless defined $sensor;
if ( defined $seen_sensors{$sensor} ) {
# We've seen this sensor before.
# Time to stop reading data
print "# Hello again, Sensor $sensor\n" if $MUNIN_DEBUG;
last;
}
$seen_sensors{$sensor} = 1;
my $temphash;
$temphash->{sensor} = $sensor;
$temphash->{temp} = $xmlref->{msg}->{tmpr};
my @temparr;
foreach my $key ( keys %{ $xmlref->{msg} } ) {
if ( $key =~ /ch(\d+)/ ) {
my $channel = $1;
my $unit = ( keys %{ $xmlref->{msg}->{"ch$channel"} } )[0];
my $val = $xmlref->{msg}->{"ch$channel"}->{$unit};
print "# Channel $channel, Unit $unit, Value $val\n"
if $MUNIN_DEBUG;
push @temparr,
{
"channel" => $channel,
"unit" => $unit,
"value" => $val
};
}
}
$temphash->{data} = \@temparr;
push @cc_data_arr, $temphash;
}
}
close($ttydev);
return @cc_data_arr;
}
sub collect_cc02_data {
# Thanks to David Edmondson <dme@dme.org> for this function
# Read data from the serial port
my ( $port, $baud ) = @_;
my $tty = Device::SerialPort->new($port) || die "Can't open $port: $!";
$tty->baudrate($baud) || die "Can't set serial baudrate";
$tty->parity("none") || die "Can't set serial parity";
$tty->databits(8) || die "Can't set serial databits";
$tty->handshake("none") || die "Can't set serial handshake";
$tty->write_settings || die "Can't set serial parameters";
open( my $ttydev, "<", $port ) || die "Can't open $port: $?";
my @cc_data_arr;
while (<$ttydev>) {
if (m{(<msg>.*</msg>)}) {
my $xmlref = XML::Simple::XMLin( $1, KeepRoot => 1 );
my $temphash;
$temphash->{sensor} = 0; # Only one sensor.
$temphash->{temp} = $xmlref->{msg}->{tmpr};
my @temparr;
foreach my $key ( keys %{ $xmlref->{msg} } ) {
if ( $key =~ /ch(\d+)/ ) {
my $channel = $1;
my $unit = ( keys %{ $xmlref->{msg}->{"ch$channel"} } )[0];
my $val = $xmlref->{msg}->{"ch$channel"}->{$unit};
push @temparr,
{
"channel" => $channel,
"unit" => $unit,
"value" => $val
};
}
}
$temphash->{data} = \@temparr;
push @cc_data_arr, $temphash;
last;
}
}
close($ttydev);
return @cc_data_arr;
}
my @cc_data = load_data();
if ( defined $ARGV[0] and $ARGV[0] eq 'config' ) {
if ($ret) {
print $ret;
exit 1;
}
for my $datum (@cc_data) {
my $unit = '';
foreach my $key ( @{ $datum->{data} } ) {
if ( $unit eq '' ) {
$unit = $key->{unit};
}
elsif ( $unit != $key->{unit} ) {
print STDERR "# Conflicting units ($unit and $key->{unit})"
. " on sensor $datum->{sensor}";
}
}
if ( $datum->{sensor} == 0 ) {
# Output the Root graph (being sensor 0)
print <<EOF;
multigraph currentcost
graph_title CurrentCost Consumption
graph_args --base 1000 -l 0
graph_vlabel $unit
graph_category currentcost
graph_info This graph shows the 'whole house' data.
EOF
if ( scalar( @{ $datum->{data} } ) > 1 ) {
print "graph_total Total\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
print <<EOF;
$fieldname.label Channel $channel->{channel}
$fieldname.type GAUGE
$fieldname.min 0
$fieldname.draw AREA
${fieldname}_t.label Channel $channel->{channel} Trend
${fieldname}_t.type GAUGE
${fieldname}_t.min 0
${fieldname}_t.draw LINE2
EOF
if ($nightrate) {
print <<EOF;
${fieldname}_n.label Channel $channel->{channel} Night
${fieldname}_n.type GAUGE
${fieldname}_n.min 0
${fieldname}_t.cdef ${fieldname}_n,UN,${fieldname},${fieldname},IF,3600,TRENDNAN
EOF
}
else {
print "${fieldname}_t.cdef ${fieldname},3600,TRENDNAN\n";
}
}
# Output the Root cumulative graph
print <<EOF;
multigraph currentcost_cumulative
graph_title CurrentCost Total Usage
graph_args --base 1000 -l 0
graph_vlabel $unit * hours
graph_category currentcost
EOF
print "graph_order ";
my $confstr = '';
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
print "${fieldname}=currentcost.$fieldname ${fieldname}_d ";
$confstr .= <<EOF;
${fieldname}.update no
${fieldname}.label Channel $channel->{channel}
${fieldname}.type GAUGE
${fieldname}.min 0
${fieldname}.cdef PREV,${fieldname},12,/,ADDNAN
${fieldname}_d.label Channel $channel->{channel} Daily
${fieldname}_d.type GAUGE
${fieldname}_d.min 0
EOF
}
print "\n$confstr";
print <<EOF;
fudge.label (fudge)
fudge.type GAUGE
fudge.graph yes
fudge.draw LINE0
EOF
# Output the root cost graph
print <<EOF;
multigraph currentcost_cost
graph_title CurrentCost Estimated Cost
graph_args --base 1000 -l 0
graph_vlabel ${currency}
graph_category currentcost
EOF
if ( scalar( @{ $datum->{data} } ) > 1 ) {
print "graph_total Total\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
print <<EOF;
$fieldname.label Channel $channel->{channel}
$fieldname.type GAUGE
$fieldname.min 0
EOF
}
}
else {
# Output a subordinate graph (being an appliance)
my $sensor = $datum->{sensor};
print <<EOF;
multigraph currentcost.appliance$sensor
graph_title CurrentCost Consumption (Appliance $sensor)
graph_args --base 1000 -l 0
graph_vlabel $unit
graph_category currentcost
graph_info This graph shows the data for Appliance $sensor
EOF
if ( scalar( @{ $datum->{data} } ) > 1 ) {
print "graph_total Total\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
print <<EOF;
$fieldname.label Channel $channel->{channel}
$fieldname.type GAUGE
$fieldname.min 0
EOF
if ($nightrate) {
print <<EOF;
${fieldname}_n.label Channel $channel->{channel} Night
${fieldname}_n.type GAUGE
${fieldname}_n.min 0
EOF
}
}
# Output the subordinate cumulative graph
print <<EOF;
multigraph currentcost_cumulative.appliance$sensor
graph_title CurrentCost Total Usage (Appliance $sensor)
graph_args --base 1000 -l 0
graph_vlabel $unit * hours
graph_category currentcost
EOF
print "graph_order ";
my $confstr = '';
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
print "$fieldname=currentcost.$fieldname ";
$confstr .= <<EOF;
$fieldname.label Channel $channel->{channel}
$fieldname.type GAUGE
$fieldname.min 0
$fieldname.cdef PREV,$fieldname,12,/,ADDNAN
EOF
}
print "\n$confstr";
# Output the subordinate Cost graph
print <<EOF;
multigraph currentcost_cost.appliance$sensor
graph CurrentCost Estimated Cost (Appliance $sensor)
graph_args --base 1000 -l 0
graph_vlabel ${currency}
graph_category currentcost
EOF
if ( scalar( @{ $datum->{data} } ) > 1 ) {
print "graph_total Total\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
print <<EOF;
$fieldname.label Channel $channel->{channel}
$fieldname.type GAUGE
$fieldname.min 0
EOF
}
}
}
save_data(@cc_data);
exit 0;
}
# Output the value data
for my $datum (@cc_data) {
if ( $datum->{sensor} == 0 ) {
# Output the Root graph (being sensor 0)
print "multigraph currentcost\n";
}
else {
my $sensor = $datum->{sensor};
print "multigraph currentcost.appliance$sensor\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
if ( is_night_rate() ) {
print "${fieldname}_n.value " . $channel->{value} . "\n";
print "${fieldname}.value 0\n";
}
else {
print "${fieldname}.value " . $channel->{value} . "\n";
print "${fieldname}_n.value 0\n";
}
}
if ( $datum->{sensor} == 0 ) {
# Output the Root graph (being sensor 0)
print "multigraph currentcost_cumulative\n";
}
else {
my $sensor = $datum->{sensor};
print "multigraph currentcost_cumulative.appliance$sensor\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
my $value = $channel->{daily};
$value += $channel->{ndaily} if defined $channel->{ndaily};
print "${fieldname}_d.value $value\n";
}
my @now = localtime(time);
if ( $now[3] == 1 and $now[2] == 0 and $now[1] <= 5 ) {
# First reading of month, reset.
print "fudge.value 0\n";
}
else {
print "fudge.value 1\n";
}
if ( $datum->{sensor} == 0 ) {
# Output the Root Cost graph (being sensor 0)
print "multigraph currentcost_cost\n";
}
else {
my $sensor = $datum->{sensor};
print "multigraph currentcost_cost.appliance$sensor\n";
}
foreach my $channel ( @{ $datum->{data} } ) {
my $fieldname = "ch" . $channel->{channel};
my $kWh = $channel->{monthly} / 1000;
my $nightkWh = $channel->{nmonthly} / 1000
if defined $channel->{nmonthly};
my $cost = $standingcharge;
if ( $nightrate and defined $nightkWh ) {
$cost = $nightkWh * $nightrate;
}
if ( $kWh <= $rate1qty ) {
$cost += $kWh * $rate1;
}
else {
$cost += ( ( $kWh - $rate1qty ) * $rate2 ) + ( $rate1qty * $rate1 );
}
$cost = $cost / 100; # Convert pence/cents into pounds/euros
print "$fieldname.value $cost\n";
my $extinfo = sprintf( "Usage is %.3f kWh.", $kWh );
$extinfo .= sprintf( " Night usage is %.3f kWh.", $nightkWh )
if defined $nightkWh;
print "$fieldname.extinfo $extinfo\n";
}
}
save_data(@cc_data);
exit 0;