-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript_time_fronius.lua
63 lines (56 loc) · 3.6 KB
/
script_time_fronius.lua
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
-- Get telemetry data from Fronius inverter into Domoticz
-- More information at https://www.domoticz.com/wiki/Fronius_inverter
-- Last update script at https://github.com/CreasolTech/domoticz_lua_scripts
-- Originally written by GeyerA, revised by CreasolTech
--
-- This script fetch data from Fronius inverter by http, and update a virtual sensor in domoticz with the current power/energy
-- Look below the lines local PV* with description of the virtual sensors that must be created manually!
local IPFronius='192.168.1.253' -- IP adress of the Fronius inverter
local PVPowerIDX=660 -- IDX of the inverte virtual sensor, to be created manually, type Electric (Instant+Counter)
local PVVacIDX=PVPowerIDX+1 -- IDX of the AC Voltage virtual sensor, to be created manually, type
local PVVdcIDX=PVVacIDX+1 -- IDX of the AC Voltage virtual sensor, to be created manually, type
local PVFreqIDX=PVVdcIDX+1 -- IDX of the Frequency virtual sensor, to be created manually, type Custom Sensor, with "Hz" as Axis label
local PVDisabledAtNight=1 -- 0 => always get telemetry. 1 => disable fetching telemetry in the night
local DEBUG=2 -- 0 => do NOT print anything to log. 1 => print debugging info. 2 => print more debugging info
commandArray = {}
if (DEBUG>=1) then startTime=os.clock() end
if (PVDisabledAtNight==1) then
-- Inverter is OFF during the night, so it does not answer to the LAN interface. Does not try to contact inverter, saving a lot of time (curl timeout)
timeNow = os.date("*t")
minutesNow = timeNow.min + timeNow.hour * 60 -- number of minutes since midnight
if (minutesNow<timeofday['SunriseInMinutes']-60 or minutesNow>timeofday['SunsetInMinutes']+60) then
if (DEBUG>=2) then print("fronius: night time!") end
if (DEBUG>=2) then print("fronius script took ".. (os.clock()-startTime) .."s") end
return commandArray
end
end
JSON = (loadfile "scripts/lua/JSON.lua")() -- using relative path - Thanks to wolfscave for testing
--Extract data from Fronius converter.
froniusurl = 'curl --connect-timeout 1 "http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CommonInverterData"'
jsondata = assert(io.popen(froniusurl))
froniusdevice = jsondata:read('*all')
local retcode={jsondata:close()}
local Pac=0
if (retcode[3]~=28) then -- curl returns 28 in case of timeout => inverter not operational (during the night?)
-- Inverter connection not in timeout
if (DEBUG>=2) then print("fronius: json data="..froniusdevice) end -- print data from Inverter
froniusdata = JSON:decode(froniusdevice)
if (froniusdata ~= nil) then
-- Inverter returned json data with telemetry
local statusCode = froniusdata['Body']['Data']['DeviceStatus']['StatusCode']
local totalEnergy = froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
if (statusCode~=nil and statusCode==7) then --Fronius converter is Running
Pac = froniusdata['Body']['Data']['PAC']['Value']
end
local Vac=froniusdata['Body']['Data']['UAC']['Value']
local Vdc=froniusdata['Body']['Data']['UDC']['Value']
local Freq=froniusdata['Body']['Data']['FAC']['Value']
if (Pac~=nil and totalEnergy~=nil) then commandArray[1] = {['UpdateDevice'] = PVPowerIDX .. "|0|" .. Pac .. ";" .. totalEnergy} end
if (Vac~=nil) then commandArray[2] = {['UpdateDevice'] = PVVacIDX .. "|"..Vac.."|"..Vac} end
if (Vdc~=nil) then commandArray[3] = {['UpdateDevice'] = PVVdcIDX .. "|"..Vdc.."|"..Vdc} end
if (Freq~=nil) then commandArray[4] = {['UpdateDevice'] = PVFreqIDX .. "|"..Freq.."|"..Freq} end
end
end
::exit::
if (DEBUG>=1) then print("fronius: Power="..Pac..", script took ".. (os.clock()-startTime) .."s") end
return commandArray