This is a really basic script to send my household consumption and voltage data to pvoutput. It sends data with a 15 minute delay to allow the solar data (in my case from the Enphase auto feed) to get fed first. If you don’t and don’t have a donation / sponsored account things go wrong - it’s documented, but I forget where.
After editing the access keys and feed IDs, run this every 5 minutes out of cron. You will also get the requests written to the log file so it can be replayed if something goes wrong.
Hope this might help someone in the future.
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use JSON::PP;
use POSIX qw(strftime);
# Change this for local requirements
$ENV{TZ} = 'Europe/London';
# Get the "Read Key" from your emonpi / emoncms set-up
my $EMONCMS_APIKEY = 'XXX';
# I only use this against my local emonpi but it should work against any emoncms
my $EMONCMS_BASE = 'http://localhost/emoncms';
# Edit these to suit
my $PVOUTPUT_APIKEY = 'XXX';
my $PVOUTPUT_SYSID = 'YYY';
my $PVOUTPUT_BASE = 'http://pvoutput.org/service/r2/addstatus.jsp';
# log each raw request here
open(LOG, ">>/tmp/pvoutput-pl.log") || die;
my $ua = LWP::UserAgent->new;
my $json = JSON::PP->new;
my $delay = 900;
my $now = $ARGV[0] || time;
my $then = $now - $delay;
# Grab the feed IDs (the numbers) from your Feeds page
my %EMONCMS_FEEDS = (
voltage => XX, # measures voltage Vrms
home => YY, # accumulated home consumption in kWh
);
my %EMONCMS_VALUES;
foreach (keys %EMONCMS_FEEDS) {
#my $f = "${EMONCMS_BASE}/feed/timevalue.json?apikey=${EMONCMS_APIKEY}&id=$EMONCMS_FEEDS{$_}";
my $f = "${EMONCMS_BASE}/feed/data.json?apikey=${EMONCMS_APIKEY}&id=$EMONCMS_FEEDS{$_}&start=${then}000&end=${now}000&interval=$delay";
my $r = $ua->get($f);
die $r->status_line unless $r->is_success;
my $j = $json->decode($r->decoded_content);
$EMONCMS_VALUES{$_} = $j;
}
my $timestamp = $then;
my $date = strftime("%Y%m%d", localtime($timestamp));
my $time = strftime("%H:%M", localtime($timestamp));
my $voltage = $EMONCMS_VALUES{voltage}->[0][1];
my $home_wh = $EMONCMS_VALUES{home}->[0][1] * 1000;
my $pvoutput_request = "${PVOUTPUT_BASE}?key=${PVOUTPUT_APIKEY}&sid=${PVOUTPUT_SYSID}&d=$date&t=$time&c1=1&v3=${home_wh}&v6=$voltage";
print LOG "${pvoutput_request}\n";
my $r = $ua->get(${pvoutput_request});
print LOG $r->status_line unless $r->is_success;