willemstoker / aps-to-pvoutput

Upload data from AP Systems solar inverters (APS) automatically to PV Output
8 stars 4 forks source link

Geen data overdracht naar PVOutput #5

Open smurmug opened 3 years ago

smurmug commented 3 years ago

Goedemiddag, Ik heb alle stappen doorlopen zoals beschreven in de Integrations beschrijving "APSystem Solar Energy Integration" vanaf stap 3, ECU without web interface. Ik rijg echter geen data binnen van APS naar PVOutput.

De volgende gegevens zijn ingevuld in PVOutput API Settings

API Access | Disabled         Enabled          HELP ### Enabled The API must be enabled to successfully process requests. API Key | New Key ### API PVOutput Your API key is used to update your data automatically, always keep your API key secret. Read Only Key |   ### API APS Add your own key with read only access to your data, ideal for 3rd party apps API Referrer |   ### Niet ingevuld The URL of your webpage. Only applicable if you are embedding portlets.

Moet ik de URL ook invullen en zo ja welke?

smurmug commented 3 years ago

Misschien iets meer bruikbare info. Hierbij mijn config,

import hassapi as hass import requests import json from datetime import date from datetime import datetime import os.path

class APStoPVOUTPUT(hass.Hass):

initialize() function which will be called at startup and reload

def initialize(self):

Schedule to run every 10 minutes

self.run_every(self.apstopv, "now", 600)

Our callback function will be called by the scheduler

def apstopv(self, kwargs): ECU_ID = 216000044734 PV_OUTPUT_SYSTEMID = 80106 PV_OUTPUT_APIKEY = 2c263512176164aa437cc17f3c8f87681c0d3a63

LAST_UPDATE_FILE = "/config/appdaemon/apps/lastupdate" # remember to change this folder if you need"

MAX_NUMBER_HISTORY = 20
APSYSTEMS_URL = 'http://api.apsystemsema.com:8073/apsema/v1/ecu/getPowerInfo'
PVOUTPUT_URL = 'http://pvoutput.org/service/r2/addstatus.jsp'

def readLastUpdate():
    f = open(LAST_UPDATE_FILE,"r")
    datestring = f.read()
    f.close()
    return datetime.strptime(datestring, "%Y%m%d %H:%M")

def writeLastUpdate(timestringminutes):
    f = open(LAST_UPDATE_FILE,"w+")
    f.write(getDateStringOfToday()+ ' ' +timestringminutes)
    f.close()

def getDateStringOfToday():
    return date.today().strftime("%Y%m%d");

def getDataFromAPS():
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
    }

    data = {
      'ecuId': ECU_ID,
      'filter': 'power',
      'date': getDateStringOfToday()
    }

    response = requests.post(APSYSTEMS_URL, headers=headers, data=data)
    return response.json();

def sendUpdateToPVOutput(timestringminutes, powerstring):
    pvoutputdata = {
      'd': getDateStringOfToday(),
      't': timestringminutes,
      'v2': powerstring
    }

    headerspv = {
        'X-Pvoutput-SystemId': PV_OUTPUT_SYSTEMID,
        'X-Pvoutput-Apikey': PV_OUTPUT_APIKEY
    }

    responsepv = requests.post(PVOUTPUT_URL, headers=headerspv, data=pvoutputdata)

    print ("Response: " + responsepv.text + " updated: " + timestringminutes + " power: " + powerstring)

if not os.path.isfile(LAST_UPDATE_FILE):
    writeLastUpdate('00:00') #create file for the first time

rootdict = getDataFromAPS()
timesstring = rootdict.get("data").get("time")
powersstring = rootdict.get("data").get("power")

timelist = json.loads(timesstring)
powerlist = json.loads(powersstring)
latestUpdate = readLastUpdate()
print("Found latest update: ")
print(latestUpdate)

i = len(timelist) - 1
count = 0;
while i >= 0 and count < MAX_NUMBER_HISTORY:
    timestringminutes = timelist[i][:-3] #get time and strip the seconds
    powerstring = powerlist[i] #get power

    currentUpdate = datetime.strptime(getDateStringOfToday()+ ' ' +timestringminutes, "%Y%m%d %H:%M")

    if currentUpdate > latestUpdate:
        sendUpdateToPVOutput(timestringminutes, powerstring)
    else:
        print("No update needed for: " + timestringminutes)

    if count == 0:
        writeLastUpdate(timestringminutes)

    i -= 1
    count += 1