-
Notifications
You must be signed in to change notification settings - Fork 2
/
zerofeed1.sh
executable file
·248 lines (194 loc) · 6.77 KB
/
zerofeed1.sh
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/sh
# Zero feed script to make sure the solar modules are reducing their output
# to (ideally) not push any energy into the public network.
# Inspired by https://github.com/tbnobody/OpenDTU/blob/master/docs/Web-API.md
# Needs OpenDTU and the Tasmota smart meter IoT devices in your WLAN and is
# intended to be executed on an OpenWrt router (install curl & jq packages).
# USE AT YOUR OWN RISK! Especially I don't know if the inverter is fit for
# this purpose as all this functionality was reverse engineered by the
# fabulous OpenDTU developers. I simply rely on their amazing work here.
# Author: Oliver Hartkopp
# License: MIT
# Exit if 'curl' is not installed
test -x /usr/bin/curl || exit 0
# Exit if 'jq' is not installed
test -x /usr/bin/jq || exit 0
# Current Smart Meter Power (signed value)
# -> should become a small positive value ;-)
SMPWR=0
# Current Solar Power (unsigned value)
SOLPWR=0
# Absolute Solar Limit (unsigned value)
# -> SMPWR + SOLPWR + ABSLIMITOFFSET
SOLABSLIMIT=0
# reduce safety margin from inverter by increasing this value
ABSLIMITOFFSET=0
# SMPWR threshold to trigger the SOLLASTLIMIT increase
SMPWRTHRESMAX=50
SMPWRTHRESMIN=10
# SmartMeter IP (Tasmota) (update for your local network setup)
SMIP=192.168.60.7
# DTU IP (OpenDTU) (update for your local network setup)
DTUIP=192.168.60.5
# DTU default admin user access (from OpenDTU installation)
DTUUSER="admin:openDTU42"
# DTU serial number (insert your inverter SN here)
DTUSN=116180400144
# DTU limiter (should be this at 100% after > 0W start)
DTUNOLIMRELVAL=100
DTULIMREL=0
# limit type absolute (non persistent)
LTABSNP=0
# limit type relative (non persistent)
LTRELNP=1
# poll interval
POLLNORMAL=5
POLLFAST=5
getSOLPWR()
{
# get power from the single selected inverter
SOLPWR=`curl -s http://$DTUIP/api/livedata/status | jq '.inverters[] | select(.serial == "'$DTUSN'").AC."0".Power.v'`
if [ -n "$SOLPWR" ]; then
# remove fraction to make it an integer
SOLPWR=${SOLPWR%.*}
fi
}
getDTUMAXPWR()
{
DTUMAXPWR=`curl -s http://$DTUIP/api/limit/status | jq '."'$DTUSN'".max_power'`
if [ -n "$DTUMAXPWR" ]; then
# remove fraction to make it an integer
DTUMAXPWR=${DTUMAXPWR%.*}
# 2% is the minimum control boundary - so take 3% to be sure
DTUMINPWR=$(($DTUMAXPWR / 33))
# start control process when having 10W more than the minimum control boundary
SOLMINPWR=$(($DTUMINPWR + 10))
fi
}
getDTULIMREL()
{
DTULIMREL=`curl -s http://$DTUIP/api/limit/status | jq '."'$DTUSN'".limit_relative'`
if [ -n "$DTULIMREL" ]; then
# remove fraction to make it an integer
DTULIMREL=${DTULIMREL%.*}
fi
}
# get current power via 'status 8' from Tasmota (for LK13BE smart meter)
getSMPWR()
{
SMPWR=`curl -s http://$SMIP/cm?cmnd=status%208 | jq '.StatusSNS.LK13BE.Power_curr'`
if [ -n "$SMPWR" ]; then
# remove fraction to make it an integer
SMPWR=${SMPWR%.*}
fi
}
getLimitSetStatus()
{
SETSTATUS="\"Pending\""
while [ "$SETSTATUS" = "\"Pending\"" ]; do
sleep 1
SETSTATUS=`curl -s http://$DTUIP/api/limit/status | jq '."'$DTUSN'".limit_set_status'`
# SETSTATUS can be "Ok" or "Pending" or "Failure"
done
}
# run initialization and solar power control forever
while [ true ]; do
getSOLPWR
getDTUMAXPWR
getSMPWR
getDTULIMREL
# wait until curl succeeds
while [ -z "$SOLPWR" ] || [ -z "$SMPWR" ]; do
sleep 2
getSOLPWR
getSMPWR
done
# get maximum power of inverter and fill DTUMINPWR and SOLMINPWR
getDTUMAXPWR
if [ -z "$DTUMAXPWR" ]; then
# no data -> restart process
continue
fi
# wait for at least some remarkable solar power (SOLMINPWR)
while [ -n "$SMPWR" ] && [ -n "$SOLPWR" ] && [ "$SOLPWR" -lt "$SOLMINPWR" ]; do
sleep 10
getSOLPWR
getSMPWR
#cho `date +%d.%m.%y,%T`","$SOLPWR","$SMPWR","$SOLABSLIMIT","$SOLLASTLIMIT","$ABSLIMITOFFSET","$SMPWRTHRESMIN","$SMPWRTHRESMAX > /var/run/zerofeed.state
done
# at this point the inverter is properly powered up
# check if we need to remove the limiter
getDTULIMREL
if [ -z "$DTULIMREL" ]; then
# no data -> restart process
continue
fi
# set OK value if we do not need to set the relative limit
SETSTATUS="\"Ok\""
if [ "$DTULIMREL" -ne "$DTUNOLIMRELVAL" ]; then
# not 100% ? -> set to 100%
SETLIM=`curl -u "$DTUUSER" http://$DTUIP/api/limit/config -d 'data={"serial":"'$DTUSN'", "limit_type":'$LTRELNP', "limit_value":'$DTUNOLIMRELVAL'}' 2>/dev/null | jq '.type'`
getLimitSetStatus
fi
# SETSTATUS can be "Ok" or "Failure" here
if [ "$SETSTATUS" != "\"Ok\"" ]; then
# setting the limit failed -> restart process (skip main control loop)
SMPWR=""
fi
# start from the top
SOLLASTLIMIT=$DTUMAXPWR
# main control loop
while [ -n "$SMPWR" ] && [ -n "$SOLPWR" ]; do
MAINSLEEP=$POLLNORMAL
if [ "$SMPWR" -lt "$SMPWRTHRESMIN" ]; then
# calculate inverter limit to stop feeding into public network
SOLABSLIMIT=$(($SMPWR + $SOLPWR - $SMPWRTHRESMIN + $ABSLIMITOFFSET))
elif [ "$SMPWR" -gt "$SMPWRTHRESMAX" ]; then
# the system power consumption is higher than our defined threshold
# => we could safely increase the current SOLLASTLIMIT by SMPWR
# until DTUMAXPWR is reached (see following if-statement).
# SOLABSLIMIT=$(($SMPWR + $SOLLASTLIMIT - $SMPWRTHRESMIN))
#
# As there was a weird oscillation observed with real SMPWR values
# we make smaller steps with SMPWRTHRESMAX towards DTUMAXPWR instead.
# When SMPWR is 'really big' we jump half of the SMPWR value.
if [ "$SMPWR" -gt $((2 * $SMPWRTHRESMAX)) ]; then
PWRINCR=$(($SMPWR / 2))
else
PWRINCR=$SMPWRTHRESMAX
fi
SOLABSLIMIT=$(($PWRINCR + $SOLLASTLIMIT - $SMPWRTHRESMIN))
fi
# do not set limits beyond the inverter capabilities
if [ "$SOLABSLIMIT" -gt "$DTUMAXPWR" ]; then
SOLABSLIMIT=$DTUMAXPWR
fi
# do not set limits beyond the inverter capabilities
if [ "$SOLABSLIMIT" -lt "$DTUMINPWR" ]; then
SOLABSLIMIT=$DTUMINPWR
fi
# only set the limit when the value was changed
if [ "$SOLABSLIMIT" -ne "$SOLLASTLIMIT" ]; then
SETLIM=`curl -u "$DTUUSER" http://$DTUIP/api/limit/config -d 'data={"serial":"'$DTUSN'", "limit_type":'$LTABSNP', "limit_value":'$SOLABSLIMIT'}' 2>/dev/null | jq '.type'`
getLimitSetStatus
MAINSLEEP=$POLLFAST
fi
# SETSTATUS can be "Ok" or "Failure" here
if [ "$SETSTATUS" != "\"Ok\"" ]; then
# setting the limit failed -> restart process
break
fi
# generate CSV capable status output
#cho `date +%d.%m.%y,%T`","$SOLPWR","$SMPWR","$SOLABSLIMIT","$SOLLASTLIMIT","$ABSLIMITOFFSET","$SMPWRTHRESMIN","$SMPWRTHRESMAX > /var/run/zerofeed.state
SOLLASTLIMIT=$SOLABSLIMIT
sleep $MAINSLEEP
getSOLPWR
getSMPWR
# restart whole process
if [ "$SOLPWR" -eq 0 ] || [ "$DTUMAXPWR" -eq 0 ]; then
unset SOLPWR
unset DTUMAXPWR
break
fi
done
done