-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsonnen.lua
More file actions
194 lines (169 loc) · 6.01 KB
/
Copy pathsonnen.lua
File metadata and controls
194 lines (169 loc) · 6.01 KB
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
-- sonnenBatterie — local JSON API v2 (read-only)
-- Emits: battery
-- Protocol: HTTP (local JSON API v2 on the Sonnen unit). Read API must
-- be enabled in the Sonnen web UI under Software-Integration → JSON API,
-- which also surfaces the Auth-Token to put into config.api_token.
--
-- Config example (config.yaml):
-- drivers:
-- - name: sonnen
-- lua: drivers/sonnen.lua
-- capabilities:
-- http:
-- allowed_hosts:
-- - 192.168.x.y # LAN IP of the Sonnen unit
-- config:
-- host: "192.168.x.y"
-- port: 80
-- api_token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
--
-- Sign convention (site: positive W flows INTO the site):
-- battery.w: positive = charging (sink), negative = discharging (source).
-- Sonnen Pac_total_W is reported positive=charging, so it passes through
-- unchanged. This is from community-contributed sample code; verify
-- on first install by checking that battery.w is positive while the
-- site is exporting and the Sonnen is taking the surplus.
DRIVER = {
host_api_min = 1,
host_api_max = 1,
id = "sonnen",
name = "sonnenBatterie (local API)",
manufacturer = "sonnen",
version = "1.0.0",
protocols = { "http" },
capabilities = { "battery" },
description = "sonnenBatterie local JSON API v2: SoC + charge/discharge power. Read-only.",
homepage = "https://sonnen.de",
authors = { "FTW contributors" },
http_hosts = { },
verification_status = "experimental",
verification_notes = "Community-contributed local-API driver; not yet verified against live hardware on a FTW site.",
connection_defaults = {
host = "",
port = 80,
},
-- Secret config keys the wizard / Settings UI should render password
-- inputs for and stuff into config.<key>. Keeps Auth-Token out of
-- yaml-by-hand and lets the operator paste it from the Sonnen web UI
-- (Software-Integration → JSON API).
config_secrets = { "api_token" },
}
PROTOCOL = "http"
local sonnen_host = ""
local sonnen_port = 80
local api_token = ""
-- Backoff state. Doubles from 2 s to 60 s on consecutive HTTP / decode
-- failures so a powered-down or wrong-token unit doesn't pin the poll
-- loop hammering the LAN.
local last_attempt = 0
local backoff_ms = 0
local BACKOFF_MIN = 2000
local BACKOFF_MAX = 60000
local function base_url()
return "http://" .. sonnen_host .. ":" .. tostring(sonnen_port)
end
local function bump_backoff()
if backoff_ms == 0 then
backoff_ms = BACKOFF_MIN
else
backoff_ms = math.min(backoff_ms * 2, BACKOFF_MAX)
end
last_attempt = host.millis()
end
local function clear_backoff()
backoff_ms = 0
last_attempt = 0
end
local function in_backoff()
if backoff_ms == 0 then return false end
return (host.millis() - last_attempt) < backoff_ms
end
-- clamp drops obviously-broken values (NaN, sentinel huge numbers from a
-- partial JSON parse) so a single bad poll doesn't poison the battery
-- model with a 2 GW reading.
local function clamp(v, max_abs)
local n = tonumber(v)
if n == nil then return nil end
if max_abs and math.abs(n) > max_abs then return nil end
return n
end
----------------------------------------------------------------------------
-- Driver interface
----------------------------------------------------------------------------
function driver_init(config)
host.set_make("sonnen")
if config then
if type(config.host) == "string" and config.host ~= "" then
sonnen_host = config.host
end
if config.port then
sonnen_port = tonumber(config.port) or 80
end
if type(config.api_token) == "string" and config.api_token ~= "" then
api_token = config.api_token
end
end
if sonnen_host == "" then
host.log("error", "sonnen: config.host required (LAN IP of the Sonnen unit)")
return
end
if api_token == "" then
host.log("warn", "sonnen: config.api_token is empty — JSON API v2 requires an Auth-Token; enable it in the Sonnen web UI under Software-Integration")
end
host.log("info", "sonnen: driver initialized (host=" .. sonnen_host
.. ":" .. tostring(sonnen_port) .. ")")
end
function driver_poll()
if sonnen_host == "" then
return 10000
end
if in_backoff() then
return 1000
end
local url = base_url() .. "/api/v2/latestdata"
local headers = { ["Auth-Token"] = api_token }
local body, err = host.http_get(url, headers)
if err then
bump_backoff()
host.log("warn", "sonnen: fetch failed: " .. tostring(err)
.. " (retry in " .. backoff_ms .. "ms)")
return 1000
end
local data = host.json_decode(body)
if not data then
bump_backoff()
host.log("warn", "sonnen: JSON decode failed")
return 1000
end
clear_backoff()
local pac_w = clamp(data.Pac_total_W, 30000) or 0
local rsoc = clamp(data.RSOC, 100)
-- USOC is the user-visible SoC the unit clamps usable energy against;
-- prefer it over RSOC (raw SoC) so the EMS sees the same headroom the
-- Sonnen will actually deliver / accept.
local usoc = clamp(data.USOC, 100)
local prod_w = clamp(data.Production_W, 30000) or 0
local cons_w = clamp(data.Consumption_W, 30000) or 0
local cap_wh = clamp(data.FullChargeCapacity, 1000000)
local soc_pct = usoc or rsoc or 0
local battery = {
w = pac_w,
soc = soc_pct / 100.0,
}
if cap_wh then
battery.capacity_wh = cap_wh
end
host.emit("battery", battery)
host.emit_metric("battery_rsoc", rsoc or 0)
host.emit_metric("battery_usoc", usoc or 0)
host.emit_metric("battery_pac_w", pac_w)
host.emit_metric("battery_production_w", prod_w)
host.emit_metric("battery_consumption_w", cons_w)
if cap_wh then
host.emit_metric("battery_capacity_wh", cap_wh)
end
return 1000
end
function driver_cleanup()
clear_backoff()
end