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
| # encoding: utf-8
'''
pvoutput -- Upload van Domoticz naar pvoutput.org
Instructies:
Status informatie voor pvoutput kan ingesteld worden op 5, 10 of 15 minuten,
roep dit programma aan op dat interval voor een juiste upload.
@author: Nicky Bulthuis
@copyright: 2014 Nicky Bulthuis. All rights reserved.
@license: BSD
@deffield updated: Updated
'''
import sys
import json
import urllib2
import re
from decimal import Decimal
from argparse import ArgumentParser
class Domoticz():
def __init__(self, url):
self.baseurl = url
def __execute__(self, url):
req = urllib2.Request(url)
return urllib2.urlopen(req, timeout=5)
def get_device(self, xid):
"""
Get the device information.
"""
url = "%s/json.htm?type=devices&rid=%s" % (self.baseurl, xid)
data = json.load(self.__execute__(url))
return data
def main(argv=None): # IGNORE:C0111
'''Command line options.'''
parser = ArgumentParser(description='Upload van Domoticz naar pvoutput.org')
parser.add_argument("-a", "--apikey", dest="apikey", help="PVOutput API Key", required=True)
parser.add_argument("-s", "--sid", dest="sid", help="PVOutput System Id", required=True)
parser.add_argument("-u", "--url", dest="url", help="URL naar Domoticz, eg: http://localhost:8080", default='http://localhost:8080')
parser.add_argument("-e", "--e-device-id", dest="e_device_id", help="Domoticz Device Id voor het Elektriciteits verbruik", type=int, required=True)
parser.add_argument("-p", "--pv-device-id", dest="pv_device_id", help="Domoticz Device Id voor het PV", type=int)
# Process arguments
args = parser.parse_args()
e_in, e_uit, e_in_pwr, e_uit_pwr = get_e(args.url, args.e_device_id)
pv_in, pv_pwr = get_pv(args.url, args.pv_device_id)
v1 = pv_in
v2 = pv_pwr
v3 = e_in + pv_in - e_uit
v4 = max(e_in_pwr + pv_pwr - abs(e_uit_pwr), 0)
code = upload_to_pvoutput(args.apikey, args.sid, v1, v2, v3, v4)
if code == 200:
return 0
else:
return code
def floorTime(dt=None, roundTo=60):
"""
Floor a datetime object to any time laps in seconds
dt : datetime.datetime object, default now.
"""
import datetime
if dt is None:
dt = datetime.datetime.now()
dt_min = datetime.datetime(datetime.MINYEAR, 1, 1)
dt_min = dt_min.replace(tzinfo=dt.tzinfo)
seconds = (dt - dt_min).seconds
rounding = seconds // roundTo * roundTo
return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond)
def upload_to_pvoutput(apikey, sid, v1, v2, v3, v4):
now = floorTime(roundTo=60*5)
d = now.strftime('%Y%m%d')
t = now.strftime('%H:%M')
url = 'http://pvoutput.org/service/r2/addstatus.jsp?d=%s&t=%s&v1=%s&v2=%s&v3=%s&v4=%s' % (d, t, v1, v2, v3, v4)
print url
req = urllib2.Request(url)
req.add_header('X-Pvoutput-Apikey', apikey)
req.add_header('X-Pvoutput-SystemId', sid)
return urllib2.urlopen(req).getcode()
def get_e(url, device_id):
"""
Ophalen gegevens voor het elektriciteit.
"""
device_data = Domoticz(url).get_device(device_id)
data = device_data['result'][0]
p = re.compile('^([0-9\.]+) .*$')
e_in = int(Decimal(p.match(data['CounterToday']).group(1)) * 1000)
e_uit = int(Decimal(p.match(data['CounterDelivToday']).group(1)) * 1000)
e_in_pwr = int(p.match(data['Usage']).group(1))
e_uit_pwr = int(p.match(data['UsageDeliv']).group(1))
return e_in, e_uit, e_in_pwr, e_uit_pwr
def get_pv(url, device_id):
"""
Ophalen gegevens voor PV.
"""
device_data = Domoticz(url).get_device(device_id)
data = device_data['result'][0]
p = re.compile('^([0-9\.]+) .*$')
pv = int(Decimal(p.match(data['CounterToday']).group(1)) * 1000)
pv_pwr = int(p.match(data['Usage']).group(1))
return pv, pv_pwr
if __name__ == "__main__":
sys.exit(main()) |