Edit 10 Oct This is now in Usefulscripts follow this post 30 to use Python script to import Octopus Agile consumption data into emoncms - #30 by TrystanLea.
After a long wait, Im now finally on Octopus Agile I know @borpin has a nice tool for importing smart meter consumption data from the Octopus API here Import Octopus consumption data but as I dont use node-red, I thought I’d put together a small python script to do the same.
This script can be ran daily from cron to pull in the latest data automatically. The script requests new data since the last valid reading:
import sys,requests, json
from datetime import datetime
# Emoncms server configuration
emoncms_apikey = "APIKEY"
feedid = FEEDID
server = "http://emonpi.local"
# Agile API configuration
mpan = MPAN
serial_number = ""
agile_apikey = ""
# Agile request parameters
params = {'page':1,'order_by':'period','page_size':25000}
# Step 1: Create feed via API call or use input interface in emoncms to create manually
# http://IP-ADDRESS/feed/create.json?tag=agile&name=consumption&datatype=1&engine=5&options={"interval":"1800"}&unit=kWh
# Step 2: Fetch feed meta data to find last data point time and value
result = requests.get(server+"/feed/getmeta.json",params={'id':feedid,'apikey':emoncms_apikey})
meta = json.loads(result.text)
print("Feed meta data:\t\t"+result.text)
if meta['npoints']>0:
end_time = meta['start_time'] + (meta['interval'] * meta['npoints'])
params['period_from'] = datetime.fromtimestamp(end_time).astimezone().isoformat()
print("Request from:\t\t"+params['period_from'])
# Step 3: Request history from Octopus
url = "https://api.octopus.energy/v1/electricity-meter-points/%s/meters/%s/consumption/" % (mpan,serial_number)
result = requests.get(url,params=params,auth=(agile_apikey,''))
data = json.loads(result.text)
if not data: sys.exit(0)
if not 'results' in data: sys.exit(0)
dp_received = len(data['results'])
print("Number of data points:\t%s" % dp_received);
# Step 4: Process history into data array for emoncms
data_out = []
for dp in data['results']:
time = int(datetime.timestamp(datetime.strptime(dp['interval_start'],"%Y-%m-%dT%H:%M:%S%z")))
value = dp['consumption']
print(str(time)+" "+str(value))
data_out.append([time,value])
# Step 5: Send data to emoncms
if len(data_out):
print("Posting data to emoncms")
result = requests.post(server+"/feed/insert.json",params={'id':feedid,'apikey':emoncms_apikey},data={'data':json.dumps(data_out)})
Example crontab entry to run at 7am every morning:
0 7 * * * /usr/bin/python3 /home/pi/agile.py > /dev/null 2>&1
The script uses a new bulk feed data upload API currently available in the emoncms master branch, see: New Emoncms feed data bulk upload API - #2 by johnbanks.