-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPVOutput_extra.py
300 lines (259 loc) · 10.9 KB
/
PVOutput_extra.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 25 23:30:15 2023
@author: rhermsen
ToDo:
- Verify if HomeWizard support mDNS (it should), and look to support this.
Done:
2024-06-30:
- Handle AirGradient read errors by returning "0" as temperature.
2024-06-27:
- Updated for breaking change in python-homewizard-energy==3.0.0. Tested with python-homewizard-energy==6.0.0.
sed -i 's/total_power_export_kwh/total_energy_export_kwh/g' PVOutput_extra.py
sed -i 's/total_power_import_kwh/total_energy_import_kwh/g' PVOutput_extra.py
2024-01-09:
- Include livingroom temperature, obtained external (AirGradient) as v9 (extended data).
2024-01-08:
- Allow a flexible use of parameters to be send to PVOutput, omid parameters with a none-type.
2024-01-07:
- Upload delta gas consumption as v8 (extended data).
2024-01-06:
- Upload gas consumtion as v7 (extended data).
2024-01-05:
- Obtain voltage line via os environment variable. If more than one is provided use the highest voltage value.
2024-01-04:
- I.s.o. asyncio.sleep(300) calculate the required delay to execute every 5min at xx min, 30 sec (where xx is 04, 09, 14, 19...)
Description:
Obtain smartmeter (P1) data using HomeWizard, and forward to PVOutput.org.
Data obtained from HomeWizard and forwarded:
- Energy consumption v3, a cumulative value in Wh.
- Power consumption v4, current power consumption in W. A negative value if returning to the grid.
- Voltage v6, voltage in v. Highest voltage if multiple lines are in use.
- Gas consumption v7 (extended data), a cumulative value in m3.
- Gas consumption v8 (extended data), a (calculated) increase (delta) from previous measurement in m3.
Data obtained from AirGradient and forwarded:
- Room temperature v9 (extended data), current room temperature in degree Celcius.
"""
from homewizard_energy import HomeWizardEnergy
from homewizard_energy.models import Device, Data
import asyncio
import aiohttp
import json
from datetime import datetime
import os
PVOUTPUT_URL = 'http://pvoutput.org/service/r2/addstatus.jsp'
HOMEWIZARD_IP = '192.168.188.102'
class PVOutput(object):
"""
Send the following information to PVOutput.org.
v3 = Energy [Wh] Consumption, a cumulative value in Wh
v4 = Power [W] Consumption, current power consumption in W
v6 = Voltage (os.environ['SOLAR_LINE']), highest voltage if multiple lines are in use
v7 = Gas [m3] Consumption, a cumulative value in m3
v8 = Gas [m3] Consumption, increase from previous measurement in m3
Ref:
https://pvoutput.org/help/api_specification.html#add-status-service
c1 = 3 (need to omit this flag becasue n=1.)
n = 1
"""
def __init__(self, timestamp, voltage=None, energy=None, power=None, gas=None, delta_gas=None, temp=None):
"""
The following attributes can be send to PVOutput if they have a value other than None.
"""
self.pvoutput_systemid = os.environ['PVO_SYSTEMID']
self.pvoutput_apikey = os.environ['PVO_APIKEY']
self.timestamp = timestamp
self.voltage = voltage
self.energy = energy
self.power = power
self.gas = gas
self.delta_gas = delta_gas
self.temp = temp
async def uploadData(self):
"""
Upload the measurement paramters.
"""
# pvoutputdata = {
# 'd': self.timestamp.strftime("%Y%m%d"),
# 't': self.timestamp.strftime("%H:%M"),
# 'v3': str(self.energy),
# 'v4': str(self.power),
# 'v6': str(self.voltage),
# 'v7': str(self.gas),
# 'v8': str(self.delta_gas),
# 'n': '1'
# }
pvoutputdata = {
'd': self.timestamp.strftime("%Y%m%d"),
't': self.timestamp.strftime("%H:%M")
}
if self.voltage != None:
pvoutputdata['v6'] = self.voltage
if self.energy != None:
pvoutputdata['v3'] = self.energy
if self.power != None:
pvoutputdata['v4'] = self.power
if self.gas != None:
pvoutputdata['v7'] = self.gas
if self.delta_gas != None:
pvoutputdata['v8'] = self.delta_gas
if self.temp != None:
pvoutputdata['v9'] = self.temp
pvoutputdata['n'] = '1'
headerspv = {
'X-Pvoutput-SystemId': self.pvoutput_systemid,
'X-Pvoutput-Apikey': self.pvoutput_apikey
}
async with aiohttp.ClientSession() as session:
async with session.post(PVOUTPUT_URL, headers=headerspv, data=pvoutputdata) as response:
response_string = await response.text()
if str(response.status) != "200":
print("Response error: ", response_string)
# print("Response status:", response.status, "Response reason:", response.reason)
# print("Response: " + str(response.status) + " updated: " + str(self.timestamp) + " voltage: " + str(self.voltage))
class HomeEnergy(object):
"""
Obtain measurement values from the P1 smartmeter using HomeWizard P1 meter.
All device information and measurement data is requested.
"""
def __init__(self, old_gas):
self.device = None
self.data = None
self.last_data = None
self.old_gas = old_gas
self.line = os.environ['SOLAR_LINE']
#self.line = '123'
# Make contact with a energy device
async def contactHW(self, host):
"""
Obtain all available data from the HomeWizard.
"""
async with HomeWizardEnergy(host) as api:
# Obtain device details and current data
self.device = await api.device()
self.data = await api.data()
self.last_data = datetime.now()
def get_pv_line_voltage(self):
"""
V6, Voltage of the line (phase) with the solar installation.
If multiple lines are configured, the highest voltage is returned.
"""
voltage = []
for l in self.line:
if l == '1':
voltage.append(self.data.active_voltage_l1_v)
elif l == '2':
voltage.append(self.data.active_voltage_l2_v)
elif l == '3':
voltage.append(self.data.active_voltage_l3_v)
#print("Voltage list =", voltage, "max = ", max(voltage))
return max(voltage)
def get_energy_consumption(self):
"""
V3, Total cumulative [Wh] imported energy from the net.
Imported = Consumption - Generation
"""
return int(self.data.total_energy_import_kwh * 1000)
def get_energy_generation(self):
"""
V1, Total cumulative [Wh] exported energy back into the net.
Exported = Generation - Consumption
"""
return int(self.data.total_energy_export_kwh * 1000)
def get_power_consumption(self):
"""
v4, Current [W] consumption.
Positive = consumption
Negative = generation
"""
return self.data.active_power_w
def get_collection_timestamp(self):
return self.last_data
def set_collection_timestamp_none(self):
self.last_data = None
def get_gas_consumption(self):
"""
v7, Total cumulative [m3] imported gas.
Extended data which needs configured via Web-GUI e.g.:
Extended Data > v7:
color : #c9244d, Area
Label: Gas, Unit: m3,
Axis: 0, Summary: Change
Credit/Debit: Debit
+ > Calculate as: Uncumulate
"""
return self.data.total_gas_m3
def get_delta_gas_consumption(self):
"""
v8, Gas usage in the last 5 minutes.
Extended data which needs configured via Web-GUI e.g.:
Extended Data > v8:
color : #fe0071, Line
Label: Gas, Unit: m3,
Axis: 0, Summary: Change
Credit/Debit: Debit
"""
if self.old_gas == None:
return 0
else:
return self.data.total_gas_m3 - self.old_gas
class AirGradient(object):
"""
"""
def __init__(self):
self.url = "http://192.168.188.100:8080/metricsjson"
async def get_temp(self):
async with aiohttp.ClientSession() as session:
try:
async with session.get(self.url) as response:
json_data = await response.text()
data_dict = json.loads(json_data)
return data_dict["temp"]
except:
return "0"
async def main():
print(f'PVOutput.py has started at {datetime.now().strftime("%Y%m%d %H:%M:%S")}, and should run infinity.')
old_gas = None
while True:
now = datetime.now()
delay = (4 - (now.minute % 5)) * 60 + (30 - now.second)
if delay < 0:
delay = 300 + delay
#print("Delay =", delay, "Time now =", now.strftime("%Y%m%d %H:%M:%S"))
await asyncio.sleep(delay)
try:
getData = HomeEnergy(old_gas)
getTemp = AirGradient()
task_1 = asyncio.Task(getData.contactHW(HOMEWIZARD_IP))
task_2 = asyncio.Task(getTemp.get_temp())
# await asyncio.gather(getData.contactHW(HOMEWIZARD_IP))
results = await asyncio.gather(task_1, task_2)
print("Data obtained at:", getData.get_collection_timestamp(),
"V6_Voltage:", getData.get_pv_line_voltage(),
"V1_Energy_gen:", getData.get_energy_generation(),
"V3_Energy:", getData.get_energy_consumption(),
"V4_Power:", getData.get_power_consumption(),
"V7_Gas:", getData.get_gas_consumption(),
"v8_Gas delta", getData.get_delta_gas_consumption(),
"v9_Temp", results[1])
except Exception as e:
exception_time = datetime.now().strftime("%Y%m%d %H:%M:%S")
print(f"Failed reading HomeEnergy at {exception_time} - ErrorType : {type(e).__name__}, Error : {e}")
else:
try:
sendData = PVOutput(getData.get_collection_timestamp(),
getData.get_pv_line_voltage(),
getData.get_energy_consumption(),
getData.get_power_consumption(),
getData.get_gas_consumption(),
getData.get_delta_gas_consumption(),
results[1])
await asyncio.gather(sendData.uploadData())
old_gas = getData.get_gas_consumption()
except Exception as e:
exception_time = datetime.now().strftime("%Y%m%d %H:%M:%S")
print(f"Failed to send data at {exception_time} - ErrorType : {type(e).__name__}, Error : {e}")
await asyncio.sleep(5)
print("Done")
asyncio.run(main())