Hi,
I wrote this little piece of code to read data from my solor inverter. I
works, but there could be (there probably are) some issues with the code
that would cause weewx crashes if something doesn't run as expected. I'm
not a python programmer, so any suggustions regarding this are appreciated.
The first thing is, I used "radiation" for storing solar power, which is
not correct, I know, but the whole thing with extending the database was a
bit too much for the first try :)
weewx.conf
[Fronius]
api_url=http://host/solar_api/v1/GetArchiveData.cgi?
user/fronius.py
import weewx, json, urllib2, syslog, weeutil.weeutil
from weeutil.weeutil import timestamp_to_string
from weewx.engine import StdService
from datetime import datetime
class AddRadiation(StdService):
def __init__(self, engine, config_dict):
# Initialize my superclass first:
super(AddRadiation, self).__init__(engine, config_dict)
# Bind to any new archive record events:
self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record)
def new_archive_record(self, event):
radiation = self.getRadiation(event)
syslog.syslog(syslog.LOG_INFO, "Radiation: %s at %s" % (radiation, (
weeutil.weeutil.timestamp_to_string(event.record['dateTime']))))
event.record['radiation'] = radiation
def getRadiation(self, event):
endDateTime = int(event.record['dateTime'])
startDateTime = endDateTime - int(self.config_dict['StdArchive'][
'archive_interval'])
endDate = datetime.utcfromtimestamp(endDateTime).isoformat()
startDate = datetime.utcfromtimestamp(startDateTime).isoformat()
url = self.config_dict['Fronius']['api_url'] +
'Scope=System&StartDate=' + startDate + '&EndDate=' + endDate +
'&Channel=EnergyReal_WAC_Sum_Produced&Channel=TimeSpanInSec'
syslog.syslog(syslog.LOG_INFO, url)
try:
response = urllib2.urlopen(url)
data = json.loads(response.read())
work = data["Body"]["Data"]["inverter/1"]["Data"]["
EnergyReal_WAC_Sum_Produced"]["Values"]["0"]
timeSpan = data["Body"]["Data"]["inverter/1"]["Data"]["
TimeSpanInSec"]["Values"]["0"]
averagePower = work / (timeSpan / float(3600))
return round(averagePower)
except urllib2.URLError, e:
syslog.syslog(syslog.LOG_ERR, "Error getting inverter data: %r" % e)
except:
syslog.syslog(syslog.LOG_ERR, "Unexpected error:")
I get JSON data from the device like this:
http://host/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate=2019-03-31T07:45:00&EndDate=2019-03-31T07:50:00&Channel=EnergyReal_WAC_Sum_Produced&Channel=TimeSpanInSec
{
"Body" :
{
"Data" :
{
"inverter/1" :
{
"Data" :
{
"EnergyReal_WAC_Sum_Produced" :
{
"Unit" : "Wh",
"Values" :
{
"0" : 97.071666666666673
},
"_comment" : "channelId=6666666"
},
"TimeSpanInSec" :
{
"Unit" : "sec",
"Values" :
{
"0" : 297
},
"_comment" : "channelId=66666"
}
},
"DeviceType" : 247,
"End" : "2019-03-31T09:49:59+02:00",
"NodeType" : 97,
"Start" : "2019-03-31T09:45:00+02:00"
}
}
},
"Head" :
{
"RequestArguments" :
{
"Channel" :
[
"EnergyReal_WAC_Sum_Produced",
"TimeSpanInSec"
],
"EndDate" : "2019-04-01T09:49:59+02:00",
"HumanReadable" : "True",
"Scope" : "System",
"SeriesType" : "Detail",
"StartDate" : "2019-03-31T09:45:00+02:00"
},
"Status" :
{
"Code" : 0,
"ErrorDetail" :
{
"Nodes" : []
},
"Reason" : "",
"UserMessage" : ""
},
"Timestamp" : "2019-04-18T05:57:49+02:00"
}
}