-
Notifications
You must be signed in to change notification settings - Fork 1
/
sma2pvoutput
executable file
·153 lines (129 loc) · 4.62 KB
/
sma2pvoutput
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
#!/usr/bin/env python3
# Copyright (c) 2016 Erik Johansson <erik@ejohansson.se>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
from sma import detect_inverters, UnicastSocket
from pvoutput import PvOutput, DefaultStatus
from datetime import date, datetime, time, timedelta
import argparse
import configparser
import logging
import math
def get_current_temperature(protocol=None, model=None, id=None):
try:
from tellcore import telldus
limit = datetime.now() - timedelta(minutes=30)
for sensor in telldus.TelldusCore().sensors():
if not sensor.has_temperature():
continue
if protocol is not None and sensor.protocol != protocol:
continue
if model is not None and sensor.model != model:
continue
if id is not None and sensor.id != id:
continue
temp = sensor.temperature()
if datetime.fromtimestamp(temp.timestamp) >= limit:
return float(temp.value)
except:
pass
return None
def main():
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s")
parser = argparse.ArgumentParser(description="Connect SMA inverter to PVoutput.org")
parser.add_argument("config", help="Configuration file to use")
parser.add_argument(
"-d", "--debug", help="Enable debug output", action="store_true"
)
parser.add_argument(
"-n", "--dry-run", help="Don't send any data", action="store_true"
)
parser.add_argument(
"-i",
"--inverter",
help="IP-address of inverter if "
"automatic detection fails or can't be used "
"(e.g. if you have multiple inverters or use Windows)",
)
parser.add_argument(
"-t",
"--timeout",
type=int,
default=2,
help="Timeout to use for network operations",
)
args = parser.parse_args()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
config = configparser.ConfigParser()
config["pvoutput"] = {}
config.read(args.config)
modified = False
if not config["pvoutput"].get("apikey"):
config["pvoutput"]["apikey"] = input("PVOutput API key: ")
modified = True
if not config["pvoutput"].get("systemid"):
config["pvoutput"]["systemid"] = input("PVOutput System Id: ")
modified = True
if modified:
with open(args.config, "w") as configfile:
config.write(configfile)
temp = get_current_temperature(model="F824")
if temp is not None:
logging.debug("Temperature %.1f C", temp)
sock = None
if args.inverter:
sock = UnicastSocket(args.inverter, timeout=args.timeout)
inverter = detect_inverters(sock=sock, timeout=args.timeout)
if not inverter:
logging.error("No inverter found")
return
logging.debug("Found %d inverter(s)", len(inverter))
inverter = inverter[0]
inverter.login_user("0000")
power = inverter.get_ac_total_power()
if power is None:
logging.debug("Inverter not active")
return
energy = inverter.get_day_yield()
(dc_power, dc_voltage, dc_current) = inverter.get_dc_data()
(_, ac_voltage, _) = inverter.get_ac_data()
if None not in ac_voltage:
ac_voltage = math.sqrt(3) * sum(ac_voltage) / 3
else:
ac_voltage = None
pvoutput = PvOutput(
config["pvoutput"]["apikey"], config["pvoutput"]["systemid"], args.dry_run
)
status = DefaultStatus._replace(
datetime=datetime.now(),
energy_generation=energy,
power_generation=power,
voltage=ac_voltage,
temperature=temp,
extended_1=dc_current[0],
extended_2=dc_current[1],
extended_3=dc_voltage[0],
extended_4=dc_voltage[1],
extended_5=dc_power[0],
extended_6=dc_power[1],
)
try:
pvoutput.add_status(status)
except:
logging.exception("Could not add status")
if __name__ == "__main__":
main()