Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python2.7
''' This script will download the detailed energy usage from dominion power and insert it into InfluxDB which you can then visualize with Grafana
https://www.dominionenergy.com/company/electric-projects/smart-meters/detailed-energy-usage
'''
import mechanize
import cookielib
import csv
import pytz
from influxdb import InfluxDBClient
from dateutil import parser
def getCSV():
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(True)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# for debug
br.set_debug_http(False)
br.set_debug_redirects(False)
br.set_debug_responses(False)
# User-Agent
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
# Open site
r = br.open('https://mya.dominionenergy.com/Usage/DailyIntervalData')
# Select the first (index zero) form
br.select_form(nr=0)
# User credentials
br.form['USER'] = 'your_username'
br.form['PASSWORD'] = 'your_password'
# Login
br.submit()
# find the latest csv for kilowatts
for l in br.links(url_regex='ViewDailyIntervalData'):
if 'csv(KW)' in l.text:
baseurl = 'https://mya.dominionenergy.com'
br.retrieve(baseurl + l.url, '/tmp/history-kw.csv')[0]
def send_to_influx(ts, value):
# influxdb settings
host = 'influxdb.local'
port = 8086
user = 'root'
password = 'root'
dbname = 'dominion'
dbuser = 'smly'
dbuser_password = 'my_secret_password'
client = InfluxDBClient(host, port, user, password, dbname)
json_body = [
{
"measurement": "powerUsage",
"tags": {
"deviceName": "utility",
},
"time": ts,
"fields": { "value": float(value) }
}
]
client.write_points(json_body)
def doMetrics():
utc = pytz.utc
local_tz = pytz.timezone('America/New_York')
columns = { 3: '00:30',
4: '01:00',
5: '01:30',
6: '02:00',
7: '02:30',
8: '03:00',
9: '03:30',
10: '04:00',
11: '04:30',
12: '05:00',
13: '05:30',
14: '06:00',
15: '06:30',
16: '07:00',
17: '07:30',
18: '08:00',
19: '08:30',
20: '09:00',
21: '09:30',
22: '10:00',
23: '10:30',
24: '11:00',
25: '11:30',
26: '12:00',
27: '12:30',
28: '13:00',
29: '13:30',
30: '14:00',
31: '14:30',
32: '15:00',
33: '15:30',
34: '16:00',
35: '16:30',
36: '17:00',
37: '17:30',
38: '18:00',
39: '18:30',
40: '19:00',
41: '19:30',
42: '20:00',
43: '20:30',
44: '21:00',
45: '21:30',
46: '22:00',
47: '22:30',
48: '23:00',
49: '23:30',
50: '00:00'
}
counter = 3
with open('/tmp/history-kw.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
while counter < 51:
dt = parser.parse(row[2] + ' ' + columns[counter])
warp = local_tz.localize(dt)
time = warp.astimezone(utc).strftime('%Y-%m-%dT%H:%M:%SZ')
send_to_influx(time, row[counter])
counter += 1
counter = 3
### BEGIN MAIN PROGRAM
getCSV()
doMetrics()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment