Hardware: Rpi3
OS: HASS.IO
Version: 0.114.0
Objective:
Upload Solar Energy Generation from APSystem API to PVOUTPUT service localy (the same Hassio Rpi)
I use that known Script for that:
#!/usr/bin/python
import requests
import json
from datetime import date
from datetime import datetime
import os.path
#id's and keys
ECU_ID = '##########'
PV_OUTPUT_SYSTEMID = '999999'
PV_OUTPUT_APIKEY = '############'
#enter a path and filename below, a file wil be create to save the last update datetime
LAST_UPDATE_FILE = "/config/python_scripts/lastupdate"" #example "text.txt" or "/home/pi/aps/lastupdate"
#usually all below this point should not be modified
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
Every time I run the script from Developers Tools I get two error messages:
Logger: homeassistant.components.python_script
Source: components/python_script/__init__.py:153
Integration: Python Scripts ([documentation](https://www.home-assistant.io/integrations/python_script), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+python_script%22))
First occurred: 14:37:20 (2 occurrences)
Last logged: 14:47:48
Warning loading script apstopvoutput.py: Line 50: Prints, but never reads 'printed' variable., Line None: Prints, but never reads 'printed' variable.
Logger: homeassistant.components.python_script.apstopvoutput.py
Source: components/python_script/__init__.py:205
Integration: Python Scripts (documentation, issues)
First occurred: 14:37:20 (2 occurrences)
Last logged: 14:47:48
Error executing script: __import__ not found
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
exec(compiled.code, restricted_globals)
File "apstopvoutput.py", line 3, in <module>
ImportError: __import__ not found
Am I doing something impossible on running Python Scripts under HASS.IO?
In time, the script works perfect on my Mac’s terminal and the HASS sensor as well.
I just need to run that script each 10 minutes without needing another machine!