Beste mede Tweakers.
Ik zit vreselijk te tobben met een python script wat ik probeer te maken. Waarschijnlijk is het super simpel maar mijn gebrek aan kennis van python nekt me;)
Het script bestaat uit een config-file en het script zelf. De config file is zo opgebouwd:
probleem script
error output
in regel 40 ziet u wat wel werkt
in regel 42 ziet u wat niet werkt
volgnes mij komt het er op neer dat de variable van het 'ipaddress' & 'port' &
'timeout' niet worden door gegeven.
In plaatst daarvan struikelt de client.SunSpecClientDevice er over dat 'ipaddres' een string is (text, namelijk het woord ipadress) en niet een ip addres (10.10.30.31) in dit geval. zeflde geld voor de port en de time out.
Mijn vraag, hoe krijg ik python zover dat hij de variable doorgeeft aan de client.SunSpecClientDevice?
Alvast bedankt voor het mee denken!!!
Ik zit vreselijk te tobben met een python script wat ik probeer te maken. Waarschijnlijk is het super simpel maar mijn gebrek aan kennis van python nekt me;)
Het script bestaat uit een config-file en het script zelf. De config file is zo opgebouwd:
Python:
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
| database1 = { #Enable this database 'enabled': 'true', #location of the database we might add more than one 'server' : 'localhost', #database port 'port' : '8086', #databse username 'username' : 'statistics', #database password 'password' : 'verwijderd', #database name 'dbname' : 'powerstatistics', } #can be used for remote storage database2 = { #Enable this database 'enabled': 'false', #location of the database we migt add more than one 'server' : '10.10.30.3', #database port 'port' : '8086', #databse username 'username' : 'remotepowerstatistics', #database password 'password' : 'verwijderd', #database name 'dbname' : 'powerstatistics', } # I thought about making tis in a array, but my pyhthon sikkls lacked;) inverter = { #IP sould be changed to match your inveter. Make shore it supports sunspec. 'ipaddress': '"10.10.30.31"', #Port should be left defauld. 'port': '502', #Only change if you know what you are doing 'timeout': '2.0', #Logical discription of the solar panels 'logicalname': 'Barn', #Phase output (1 or 3) this will determine the report template 'phase': '1' } |
probleem script
code:
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
| #!/usr/bin/python2.7 # to do: # extra off standard IP toevoegen en naar de PI rapporteren (dubbbel dus) als multi setup # Unfortunatly pysunspec is build with python 2.7 so we are limitied in upgrades # # # #importing modules #Please change this line acording to your config!! import configexample as config #if modules are missing, please install via 'pip install $modulename' from termcolor import colored from influxdb import InfluxDBClient import sunspec.core.client as client # key component is pysunspec. This is an python bases program to communicatei wit the sunspec standard. # more info about the pysunspec program: https://github.com/sunspec/pysunspec # Solar Edge SunSpec: https://www.solaredge.com/sites/default/files/sunspec-implementation-technical-note.pdf #we shoudl fix this #setting up connection to Solar Edge inverter #configtimeout = config.inverter['timeout'] #d = client.SunSpecClientDevice(client.TCP, 1, ipaddr="10.10.30.31", ipport=502, timeout=2.0) d = client.SunSpecClientDevice(client.TCP, 1, ipaddr=config.inverter['ipaddress'], ipport=config.inverter['port'], timeout=config.inverter['timeout']) #reading data from Solar Edge Inverter d.common.read() d.inverter.read() d.close() #date used in JSON report for Grafana date = datetime.datetime.now().isoformat() #Show data sets we retreived print colored ('Common info about the inverter', 'green') print d.common print colored ('Detailed info about inverter','green') print d.inverter #this message is realy imported. #if the wrong schama is selected info will be missed. print colored ('----------------------------------', 'green') print colored ('- Data for Grafana -', 'green') print colored ('- Please verify info above -', 'green') print colored ('- with the selected 1 or 3 -', 'green') print colored ('- -phase inverter schema! -', 'green') print colored ('----------------------------------', 'green') print 'Date (UTC):',date # we should add a for loop to have multiple inverters supported in one site #if we use a 1 phase inverter if config.inverter['phase'] == '1': print colored ('----------------------------------', 'green') print colored ('- using 1 phase inverter schema -', 'green') print colored ('----------------------------------', 'green') json_body = [ { "measurement": "Ampere", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.A, } }, { "measurement": "Ampere phase A", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.AphA, } }, { "measurement": "Voltage phase A", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.PPVphAB, } }, { "measurement": "Watt", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.W, } }, { "measurement": "Hertz", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.Hz, } }, { "measurement": "Volt Ampere", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.VA, } }, { "measurement": "Volt Ampere Reactive", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.VAr } }, { "measurement": "Power Factur", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.PF, } }, { "measurement": "Watt Hour (Total)", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.WH, } }, { "measurement": "DC Ampere", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.DCA, } }, { "measurement": "DC Volt", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.DCV, } }, { "measurement": "DC Watt", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.DCW, } }, { "measurement": "Temperature Headsink", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.TmpSnk, } }, { "measurement": "Status Vandor Code", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.StVnd, } }, ] #if we use a 3 phase inverter -> needs a check, never tested! if config.inverter['phase'] == '3': print colored ('----------------------------------', 'green') print colored ('- using 3 phase inverter schema -', 'green') print colored ('----------------------------------', 'green') json_body = [ { "measurement": "Ampere", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.A, } }, { "measurement": "Ampere phase A", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.AphA, } }, { "measurement": "Ampere phase B", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.AphB, } }, { "measurement": "Ampere phase C", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.AphC, } }, { "measurement": "Volt phase A", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.PhVphA, } }, { "measurement": "Volt phase B", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.PhVphB, } }, { "measurement": "Volt phase C", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.PhVphC, } }, { "measurement": "Watt", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.W, } }, { "measurement": "Hertz", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.Hz, } }, { "measurement": "Volt Ampere", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.VA, } }, { "measurement": "Volt Ampere Reactive", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.VAr } }, { "measurement": "Power Factur", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.PF, } }, { "measurement": "Watt Hour (Total)", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.WH, } }, { "measurement": "DC Ampere", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.DCA, } }, { "measurement": "DC Volt", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.DCV, } }, { "measurement": "DC Watt", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.DCW, } }, { "measurement": "Temperature Headsink", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.TmpSnk, } }, { "measurement": "Status Vandor Code", "tags": { "Host": d.common.SN, "logicalname": config.inverter['logicalname'], }, "time": date, "fields": { "value": d.inverter.StVnd, } }, ] #Write to databse 1 if enabled if config.database1['enabled'] == 'true': print colored ('----------------------------------', 'green') print colored ('- send data to databse 1 -', 'green') print colored ('----------------------------------', 'green') client1 = InfluxDBClient(host=config.database1['server'], port=config.database1['port'], username=config.database1['username'], password=config.database1['password']) client1.switch_database(config.database1['dbname']) client1.write_points(json_body) else: print colored ('----------------------------------', 'green') print colored ('- skipping databse 1 -', 'green') print colored ('----------------------------------', 'green') #Write to databse 2 if enabled if config.database2['enabled'] == 'true': print colored ('----------------------------------', 'green') print colored ('- send data to databse 2 -', 'green') print colored ('----------------------------------', 'green') client2 = InfluxDBClient(host=config.database2['server'], port=config.database2['port'], username=config.database2['username'], password=config.database2['password']) client2.switch_database(config.database2['dbname']) client2.write_points(json_body) else: print colored ('----------------------------------', 'green') print colored ('- skipping databse 2 -', 'green') print colored ('----------------------------------', 'green') print colored ('----------------------------------', 'green') print colored ('- Done -', 'green') print colored ('----------------------------------', 'green') |
error output
code:
1
2
3
4
5
6
7
8
9
10
11
| poweradmin@powerstatistics:~/powerstatistics$ ./sunspecread.py Traceback (most recent call last): File "./sunspecread.py", line 34, in <module> d = client.SunSpecClientDevice(client.TCP, 1, ipaddr=config.inverter['ipaddress'], ipport=config.inverter['port'], timeout=config.inverter['timeout']) File "/usr/local/lib/python2.7/dist-packages/sunspec/core/client.py", line 775, in __init__ self.device.scan(progress=scan_progress, delay=scan_delay) File "/usr/local/lib/python2.7/dist-packages/sunspec/core/client.py", line 218, in scan self.modbus_device.connect() File "/usr/local/lib/python2.7/dist-packages/sunspec/core/modbus/client.py", line 674, in connect raise ModbusClientError('Connection error: %s' % str(e)) sunspec.core.modbus.client.ModbusClientError: Connection error: a float is required |
in regel 40 ziet u wat wel werkt
in regel 42 ziet u wat niet werkt
volgnes mij komt het er op neer dat de variable van het 'ipaddress' & 'port' &
'timeout' niet worden door gegeven.
In plaatst daarvan struikelt de client.SunSpecClientDevice er over dat 'ipaddres' een string is (text, namelijk het woord ipadress) en niet een ip addres (10.10.30.31) in dit geval. zeflde geld voor de port en de time out.
Mijn vraag, hoe krijg ik python zover dat hij de variable doorgeeft aan de client.SunSpecClientDevice?
Alvast bedankt voor het mee denken!!!
[ Voor 98% gewijzigd door jawmoret op 23-07-2019 16:44 ]
Do the math: sin adds to your troubles, subtracts from your peace, multiplies your sorrows and divides you from God.