-
Notifications
You must be signed in to change notification settings - Fork 2
/
zeversolarget.py
85 lines (77 loc) · 3.43 KB
/
zeversolarget.py
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
# File: zeversolarget.py
# Author: Cheong Koo
#
# Description: Home Assistant AppDaemon application to get solar generated power
# from ZeverSolar inverter and send it to the front end using MQTT.
# Callback function to update reading every minute.
#
# Modify MQTT payload format to match with your MQTT settings in Home Assistant.
#
# Reference: ZeverSolar https://forum.pvoutput.org/t/how-to-read-data-direct-from-zeversolar-inverter/1030/2
import appdaemon.plugins.hass.hassapi as hass
import urllib.request
import datetime
# ---------------------------------------------------------------------
# Global variables
# ---------------------------------------------------------------------
link = "http://192.168.1.XXXX/home.cgi" # Fill in IP address of ZeverSolar Inverter
myDatetime = "dd/mm/yyyy hh:mm"
myGeneration = "0.00"
myDailykWh = "0.00"
from urllib.request import Request, urlopen
from urllib.error import URLError
from datetime import datetime
from datetime import timedelta
class ZeverSolar(hass.Hass):
def initialize(self):
self.log("------------------------------------------------")
self.log("Initiatilize: Get energy from solar system.")
# Register call back function for every 1 minute
# Delay callback by 5 mins from start
startTime = datetime.now() + timedelta(minutes=5)
self.handle = self.run_every(self.doGetGenAndSendMQTT, startTime, 1 * 60)
# Get generation and send out message
def doGetGenAndSendMQTT(self, arg):
global myDatetime
global myGeneration
global myDailykWh
self.log("----- Get Gen Callback -----")
self.requestSolarGeneration(self)
# Send generated power
myTopic = "home/solargen/POWER" # MQTT topic for generated power
myPayload = "{\"power\":" + "\"" + myGeneration + "\"}"
self.log("Payload: " + myPayload) # MQTT payload for generated power
self.call_service("mqtt/publish", topic = myTopic, payload = myPayload)
# Send date and time of reading
myTopic = "home/solargen/DATETIME" # MQTT topic for time of reading
myPayload = "{\"datetime\":" + "\"" + myDatetime + "\"}"
self.log("Payload: " + myPayload) # MQTT payload for time of reading
self.call_service("mqtt/publish", topic = myTopic, payload = myPayload)
# Send total generated kWh
myTopic = "home/solargen/DAILYKWH" # MQTT topic for daily kWh
myPayload = "{\"dailykwh\":" + "\"" + myDailykWh + "\"}"
self.log("Payload: " + myPayload) # MQTT payload for generated power
self.call_service("mqtt/publish", topic = myTopic, payload = myPayload)
# Returns a string containing the generation or 0 if an error
def requestSolarGeneration(self, arg):
global myDatetime
global myGeneration
global myDailykWh
now = datetime.now()
myDatetime = now.strftime("%d/%m/%Y %H:%M") # dd/mm/YY H:M:S
req = Request(link)
try:
response = urlopen(req)
htmlresponse = response.read()
st = htmlresponse.decode()
st = st.split()
gen = st[11]
totgen = st[12]
txt = "{:.2f}"
myGeneration = txt.format(float(gen)/1000)
myDailykWh = txt.format(float(totgen))
return
except:
self.log("Error in connecting to Zever solar server")
myGeneration = "0.00"
return