Fronius inverter

From Domoticz
Revision as of 17:26, 10 February 2022 by Creasol (talk | contribs) (→‎Installation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Fronius inverter with http access (LAN interface) can be managed simply by a LUA script that fetch telemetry in json format and update some virtual sensors that must be created manually.

Installation

  1. Go to Setup -> Hardware: if there is no hardware with Create Virtual Sensors button, add a new hardware, type Dummy
  2. Create a virtual sensor for the power/energy: click on the Create Virtual Sensors button, enter name Inverter Power (or something similar), type Electric (Instant+Counter)
  3. Create a virtual sensor for the Voltage AC: click on the Create Virtual Sensors button, enter name Inverter Voltage (or something similar), type Voltage
  4. Create a virtual sensor for the photovoltaic Voltage DC: click on the Create Virtual Sensors button, enter name Inverter PV Voltage (or something similar), type Voltage
  5. Create a virtual sensor for the frequency: click on the Create Virtual Sensors button, enter name Inverter Frequency (or something similar), type Custom sensor, Axis label Hz
  6. Select Utility menu, click on Edit button for the power virtual sensor (created at step 2), keep note of its idx and exit
  7. Copy and paste the following script into the directory domoticz/scripts/lua and name it script_time_fronius.lua
  8. Edit the script and set IPFronius variable to the IP number of the inverter, and PVPowerIDX=idx at point 6. If you have created the 4 virtual sensors (steps 2 to 5) in the right sequence, you do not need to specify the idx for the other 3 virtual sensors.
-- 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=1			-- 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 "/home/pi/domoticz/scripts/lua/JSON.lua")()   -- For Linux

--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 DAY_ENERGY   = froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
		if( 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']
		commandArray[1] = {['UpdateDevice'] = PVPowerIDX .. "|0|" .. Pac .. ";" .. DAY_ENERGY}
		commandArray[2] = {['UpdateDevice'] = PVVacIDX .. "|"..Vac.."|"..Vac}
		commandArray[3] = {['UpdateDevice'] = PVVdcIDX .. "|"..Vdc.."|"..Vdc}
		commandArray[4] = {['UpdateDevice'] = PVFreqIDX .. "|"..Freq.."|"..Freq}
	end
end

::exit::
if (DEBUG>=1) then print("fronius: Power="..Pac..", script took ".. (os.clock()-startTime) .."s") end
return commandArray

The script is called every 1 minute: please check in the domoticz log if everything goes well.

The last updated version is available at https://github.com/CreasolTech/domoticz_lua_scripts where it's possible to submit any issues or contribute to improve the script.