Solved: Problem with LUA script reading from Fronius inverter

Moderator: leecollings

glsf91
Posts: 58
Joined: Tuesday 14 November 2017 21:56
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by glsf91 »

Meulderg wrote: Thursday 16 February 2017 12:44 The script that was posted here, didn’t give me the result I wanted (it didn’t give me a result at all).
It took me a while to get things working for me, as I’m no expert in scripting.

This script runs between sunrise – 30 min and sunset +30 min
After that it looks at the state of the converter, if it is running than it will retrieve data and update the sensors.
A long time ago, but thanks for your script.
I changed your script to dzVents. I also made the call to Fronius async so the Domoticz will not hang if Fronius Inverter is not reachable.

Code: Select all

-- Script to read solardata from FroniusAPI
-- Original Autor : Meulderg  http://www.domoticz.com/forum/viewtopic.php?t=15037#p119999
-- Changed to dzVents by glsf91
-- Date : 27-Jan-2019
-- Warning: Don't forget to setup the Local Networks otherwize the device counters won't get updated if site security is used.

return {
    active = true,
	logging = {
    	--level = domoticz.LOG_DEBUG, -- Uncomment to override the dzVents global logging setting and use debug
    	marker = 'Fronius'
	},
   on = {
      timer = {
         'every minute'
      },
      httpResponses = { 'triggerFronius' }
   },
   execute = function(domoticz,item)
       
       	-- Variables to customize ------------------------------------------------
       	-- If you change the names of the sensor, also change it below
    	local IPdomiticz = domoticz.variables('UV_DomoticzIP').value    -- IP adress of the domoticz service + port
    	local IPFronius = domoticz.variables('UV_FroniusIP').value		-- IP adress of the Fronius converter
    	local idx_Fronius_Opbrengst = domoticz.devices('Fronius_Opbrengst').idx
    	local idx_UDC = domoticz.devices('Fronius_UDC').idx
    	local idx_UAC = domoticz.devices('Fronius_UAC').idx
    	local idx_IDC = domoticz.devices('Fronius_IDC').idx
    	local idx_IAC = domoticz.devices('Fronius_IAC').idx
    	local idx_DAY_ENERGY = domoticz.devices('Fronius_Day_Energy').idx
    	local idx_YEAR_ENERGY = domoticz.devices('Fronius_Year_Energy').idx
    	local idx_TOTAL_ENERGY = domoticz.devices('Fronius_Total_Energy').idx

    	--Create Virtual Hardware via JSON script (Copy this URL manualy)
    	--Change IP, Port and name accordingly
    	--http://192.168.1.159:8080/json.htm?type=command&param=addhardware&htype=15&port=1&name=Fronius&enabled=true
    	
    	--Create Virtual sensors via JSON script (Copy these URL manual)
    	--Change IP, Port and idx (idx is of the previously created hardware ID) accordingly
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_Opbrengst&sensortype=18
    		--Above sensor needs to be manual adjusted to Type: Return.
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_Day_Energy&sensortype=248     Ander type ??
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_Year_Energy&sensortype=248    Ander type ??
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_Total_Energy&sensortype=248   Ander type ??
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_UAC&sensortype=4
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_UDC&sensortype=4
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_IAC&sensortype=19
    	--http://192.168.1.159:8080/json.htm?type=createvirtualsensor&idx=15&sensorname=Fronius_IDC&sensortype=19
    	
    	-- one-time load of the routines
    	--	JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
		local json = assert(loadfile "/home/john/domoticz/scripts/lua/JSON.lua")()	-- For Linux

        domoticz.log('Fronius script running', domoticz.LOG_DEBUG)

        if (item.isTimer) then
        	-- get current time & calculate sunrise and sunset.
        	time_now = os.date("*t")
        	minutes_now = time_now.min + time_now.hour * 60
        	
        	domoticz.log('Time in minutes: ' ..minutes_now, domoticz.LOG_DEBUG)
        	domoticz.log('Sunrise in minutes: ' ..timeofday['SunriseInMinutes'] - 60, domoticz.LOG_DEBUG)
        	domoticz.log('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60, domoticz.LOG_DEBUG)

        	-- Validation for sunrise and sunset.
        	if (minutes_now > timeofday['SunriseInMinutes'] - 30) and (minutes_now < timeofday['SunsetInMinutes'] + 30) then
                domoticz.log('Call Fronius url with callback', domoticz.LOG_DEBUG)
                -- ASYNC openUrl
                domoticz.openURL({
                    url = 'http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData',
                    method = 'GET',
                    callback = 'triggerFronius'
                })
            else                
                domoticz.log('Fronius inverter Offline because of SunSet', domoticz.LOG_INFO) 
            end
        end

        if (item.isHTTPResponse and item.ok) then
            domoticz.log('Callback from url requested', domoticz.LOG_DEBUG)
            domoticz.log('Data: '..item.data, domoticz.LOG_DEBUG)
            
            -- we know it is json but dzVents cannot detect this if there is no correct Content-Type
            -- convert to Lua
            local jsonfroniusdata = domoticz.utils.fromJSON(item.data)
            -- json is now a Lua table

            if (jsonfroniusdata == nil) then
                domoticz.log('Fronius inverter data is empty', domoticz.LOG_ERROR) 
                return
            end

            -- request successfull ?
            if ( jsonfroniusdata.Head.Status.Code ~= 0 ) then
                domoticz.log('Fronius inverter head status code data is not 0', domoticz.LOG_ERROR) 
                domoticz.log('Status code: ' + jsonfroniusdata.Head.Status.Code + ' Reason: ' + jsonfroniusdata.Head.Status.Reason + ' User message: ' + jsonfroniusdata.Head.Status.UserMessage, domoticz.LOG_ERROR) 
                return
            end


            local StatusCode = jsonfroniusdata.Body.Data.DeviceStatus.StatusCode 
            domoticz.log('json froniusdata Statuscode: '..StatusCode, domoticz.LOG_DEBUG) 
            
            if ( StatusCode == 7) then --Fronius converter is Running 
            		
    		local DAY_ENERGY	= jsonfroniusdata.Body.Data.DAY_ENERGY.Value
    		local YEAR_ENERGY	= jsonfroniusdata.Body.Data.YEAR_ENERGY.Value
    		local TOTAL_ENERGY	= jsonfroniusdata.Body.Data.TOTAL_ENERGY.Value
    		local PAC 			= jsonfroniusdata.Body.Data.PAC.Value
    		local UDC			= jsonfroniusdata.Body.Data.UDC.Value
    		local UAC			= jsonfroniusdata.Body.Data.UAC.Value
    		local IDC			= jsonfroniusdata.Body.Data.IDC.Value
    		local IAC			= jsonfroniusdata.Body.Data.IAC.Value

                domoticz.log('PAC: '..PAC, domoticz.LOG_DEBUG)
                domoticz.log('Day Energy: '..DAY_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Year Energy: '..YEAR_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Total Energy: '..TOTAL_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('UDC: '..UDC, domoticz.LOG_DEBUG)
                domoticz.log('UAC: '..UAC, domoticz.LOG_DEBUG)
                domoticz.log('IDC: '..IDC, domoticz.LOG_DEBUG)
                domoticz.log('IAC: '..IAC, domoticz.LOG_DEBUG)
                
                --domoticz.devices('Fronius_UDC').updateVoltage(UDC)
                --domoticz.devices('Fronius_UAC').updateVoltage(UAC)
                --domoticz.devices('Fronius_IDC').updateCurrent(IDC)
                --domoticz.devices('Fronius_IAC').updateCurrent(IAC)
                --domoticz.devices('Fronius_Day_Energy').updateEnergy(DAY_ENERGY)
                --domoticz.devices('Fronius_Year_Energy').updateEnergy(YEAR_ENERGY)
                --domoticz.devices('Fronius_Total_Energy').updateEnergy(TOTAL_ENERGY)
                --domoticz.devices('Fronius_Opbrengst').updateElectricity(PAC,DAY_ENERGY)

                -- to trigger also influxdb update. replaced with the url
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_DAY_ENERGY..'&nvalue=0&svalue='..DAY_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_YEAR_ENERGY..'&nvalue=0&svalue='..YEAR_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_TOTAL_ENERGY..'&nvalue=0&svalue='..TOTAL_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue='..PAC..';'..DAY_ENERGY)

            else
		domoticz.log('Fronius converter state (Statuscode:'..StatusCode..') other than running', domoticz.LOG_INFO) 
		local UDC = 0	local UAC = 0	local IDC = 0	local IAC = 0

                --domoticz.devices('Fronius_UDC').updateVoltage(0)
                --domoticz.devices('Fronius_UAC').updateVoltage(0)
                --domoticz.devices('Fronius_IDC').updateCurrent(0)
                --domoticz.devices('Fronius_IAC').updateCurrent(0)
  
                -- to trigger also influxdb update. replaced with the url
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC)
                domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC)
            end

        end
    end

}
I also changed another few things but basically it is doing the same.
I'am not completely happy with the devicetype of the 3 energy devices. It is showing Watt instead of Wh as a unit. I think a custom sensor as device type is better.
I also used some user variables for the IP addresses. Debug is done a little bit different.
GeyerA
Posts: 12
Joined: Friday 18 September 2020 11:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by GeyerA »

Hi, I also wanted to share my script which I improved to cover my expectations. It was initially found in the "photovoltaikforum". User Gerhard told me that he got great support here; so credit to Gerhard and thanks to all who helped him.
I have also added the Own consumption measurement. This is supposed to count just what I consume from self-production. Important for that sensor is that the energy is "computed" based on the power. I have added a side note on the sensor creation string.

Code: Select all

   local IPFronius = '192.168.178.34'         -- IP adress of the Fronius converter
   local idx_PV = 68                -- idx of the virtual sensor, you need to change this to your own Device IDx
   local idx_VERBRAUCH = 71               -- idx of PowerMeter (Energy provider supply)
   local idx_EIGENVERBRAUCH = 69          -- idx of Eigenverbrauch (House consumption W and computed KWh)
   local idx_PEIGENVERBRAUCH = 70          -- idx of PEigenverbrauch (House consumption own consumed W and computed KWh)

   --Create Virtual Hardware via JSON script (Copy this URL manualy)
   --Change IP, Port and name accordingly
   --http://192.168.178.47:8080/json.htm?type=command&param=addhardware&htype=15&port=1&name=Fronius&enabled=true
   --Create Virtual sensors via JSON script (Copy these URL manual)
   --Change IP, Port and idx (idx is of the previously created hardware ID) accordingly
   --http://192.168.178.47:8080/json.htm?type=createvirtualsensor&idx=18&sensorname=Solar Power&sensortype=18
   --http://192.168.178.47:8080/json.htm?type=createvirtualsensor&idx=18&sensorname=House consumption&sensortype=18 (enable computed)
   --http://192.168.178.47:8080/json.htm?type=createvirtualsensor&idx=18&sensorname=Own consumption&sensortype=18 (enable computed)
      --Above sensor needs to be manual adjusted to Type: Return.
   --http://192.168.178.47:8080/json.htm?type=createvirtualsensor&idx=18&sensorname=Energy provider supply&sensortype=248

   JSON = (loadfile "/opt/domoticz/scripts/lua/JSON.lua")()   -- For Linux

--   local function tprint (t, indent, done) -- introduced done to prevent infinite loops for tables with self-references
--      local done = done or {} 
--      indent = indent or 0
--      for key, value in pairs (t) do
--        pre = (string.rep (" ", indent)) -- indent it
--        if type (value) == "table" and not done[value] then
--          done [value] = true
--          print (pre .. tostring (key) .. ":");
--          tprint (value, indent + 2, done)
--        elseif type(value) == 'function' and not done[value] then
--          print( pre .. (tostring (key) .. "()"))
--        else
--          pre = pre .. (tostring (key) .. ": ")
--         print (pre .. tostring(value))
--        end
--      end
--    end

   commandArray = {}
      --Extract data from Fronius converter.
      froniusurl   = 'curl "http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData"'
      jsondata    = assert(io.popen(froniusurl))
      froniusdevice = jsondata:read('*all')
      jsondata:close()
      froniusdata = JSON:decode(froniusdevice)

      -- extract data from Fronius Power Meter 63A
      froniusurl2   = 'curl "http://'..IPFronius..'/solar_api/v1/GetMeterRealtimeData.cgi?Scope=System"'
      jsondata2    = assert(io.popen(froniusurl2))
      froniusdevice2 = jsondata2:read('*all')
      jsondata2:close()
      froniusdata2 = JSON:decode(froniusdevice2)

      --Process fronius data
      if (froniusdata ~= nil) then
      local StatusCode       = froniusdata['Body']['Data']['DeviceStatus']['StatusCode']
         if( StatusCode == 7) then --Fronius converter is Running
           local DAY_ENERGY   = froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
           local PAC          = froniusdata['Body']['Data']['PAC']['Value']
           local VERBRAUCH    = froniusdata2['Body']['Data']['0']['PowerReal_P_Sum']            
           local EIGENVERBRAUCH = VERBRAUCH + PAC
           
           if VERBRAUCH < 0 then
           MYEIGENVERBRAUCH = EIGENVERBRAUCH
           elseif VERBRAUCH > 0 then
           MYEIGENVERBRAUCH = PAC
           end
           
           local PEIGENVERBRAUCH = MYEIGENVERBRAUCH
           
           commandArray[1] = {['UpdateDevice'] = idx_PV .. "|0|" .. PAC .. ";" .. DAY_ENERGY}
           commandArray[2] = {['UpdateDevice'] = idx_VERBRAUCH .. "|0|" .. VERBRAUCH }
           commandArray[3] = {['UpdateDevice'] = idx_EIGENVERBRAUCH .. "|0|" .. EIGENVERBRAUCH }
           commandArray[4] = {['UpdateDevice'] = idx_PEIGENVERBRAUCH .. "|0|" .. PEIGENVERBRAUCH }


         else
           local PAC=0
           local PEIGENVERBRAUCH = 0
           local DAY_ENERGY  = froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
           local VERBRAUCH   = froniusdata2['Body']['Data']['0']['PowerReal_P_Sum']            
           commandArray[1] = {['UpdateDevice'] = idx_PV .. "|0|" .. PAC .. ";" .. DAY_ENERGY}
           commandArray[2] = {['UpdateDevice'] = idx_VERBRAUCH .. "|0|" .. VERBRAUCH }
           commandArray[3] = {['UpdateDevice'] = idx_EIGENVERBRAUCH .. "|0|" .. VERBRAUCH }
           commandArray[4] = {['UpdateDevice'] = idx_PEIGENVERBRAUCH .. "|0|" .. PEIGENVERBRAUCH }
         end   
  end
  
--      tprint(commandArray)
      
   return commandArray[attachment=0]Selfconsumed.png[/attachment]
Attachments
Selfconsumed.png
Selfconsumed.png (73.98 KiB) Viewed 2069 times
kniazio
Posts: 198
Joined: Thursday 06 October 2016 8:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7243
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by kniazio »

I have such an error
Any suggestions?

Code: Select all

 2021-03-09 16:47:29.899 Error: EventSystem: in Solar: [string " local IPFronius = '192.168.1.199' ..."]:90: ']' expected near '='
Meulderg
Posts: 6
Joined: Thursday 16 February 2017 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by Meulderg »

as i was rebuilding my Domoticz from scratch, i rewatched my old post.
Thanks glsf91 for editing the script, to be more resource friendly.
On the old script, i never used the values UAC IAC UDC and IDC. so i commented them out of this script
Also added some more help on what sensors to create.
Hope this helps others.

Code: Select all

-- Script to read solardata from FroniusAPI
-- Original Autor : Meulderg  http://www.domoticz.com/forum/viewtopic.php?t=15037#p119999
-- Changed to dzVents by glsf91
-- Date : 14-03-2021
-- This script needs to be placed in the /home/pi/domoticz/scripts/dzVents/scripts/ directory
-- Warning: Don't forget to setup the Local Networks otherwize the device counters won't get updated if site security is used.

return {
    active = true,
	logging = {
    	--level = domoticz.LOG_DEBUG, -- Uncomment to override the dzVents global logging setting and use debug
    	marker = 'Fronius'
	},
   on = {
      timer = {
         'every minute'
      },
      httpResponses = { 'triggerFronius' }
   },
   execute = function(domoticz,item)
       
       	-- Variables to customize ------------------------------------------------
	      --Create Virtual Hardware via JSON script, dubble click the URL after changing the requred settings. The Virtual hardware is then automaticly created.
    	    --Change IP, Port and name accordingly
    	    --http://192.168.1.4:8080/json.htm?type=command&param=addhardware&htype=15&port=1&name=Fronius&enabled=true
    	
    	--Create Virtual sensors via JSON script, dubble click the URL after changing the requred settings. The Virtual hardware is then automaticly created.
    	  --Change IP, Port and idx (idx is of the previously created hardware ID) accordingly
    	  --http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_Opbrengst&sensortype=18
    		--Above sensor needs to be manual adjusted to Type: Return. (Open the device and edit the type).
    	
		--For these sensors below create a virtual sensor manualy. Sensor type = Custom sensor.
        --Fronius_Day_Energy with label 'kW' 
    	--Fronius_Year_Energy with label 'kW'
    	--Fronius_Total_Energy with label 'MW'
		
		--The Virtual sensors below can be used, but if not delete them from the rest of the script also		
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_UAC&sensortype=4
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_UDC&sensortype=4
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_IAC&sensortype=19
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_IDC&sensortype=19       

	   -- If you change the names of the sensor, also change it below
    	local IPdomiticz = domoticz.variables('UV_DomoticzIP').value    -- Set this value in Domoticz as User Variable "String" -> IP adress of the domoticz service + port
    	local IPFronius = domoticz.variables('UV_FroniusIP').value		-- Set this value in Domoticz as User Variable "String" -> IP adress of the Fronius converter
    	local idx_Fronius_Opbrengst = domoticz.devices('Fronius_Opbrengst').idx
    	local idx_DAY_ENERGY = domoticz.devices('Fronius_Day_Energy').idx
    	local idx_YEAR_ENERGY = domoticz.devices('Fronius_Year_Energy').idx
    	local idx_TOTAL_ENERGY = domoticz.devices('Fronius_Total_Energy').idx
   	    --local idx_UDC = domoticz.devices('Fronius_UDC').idx
    	--local idx_UAC = domoticz.devices('Fronius_UAC').idx
    	--local idx_IDC = domoticz.devices('Fronius_IDC').idx
    	--local idx_IAC = domoticz.devices('Fronius_IAC').idx
		
    	-- one-time load of the routines
    	--	JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
		local json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()	-- For Linux

        domoticz.log('Fronius script running', domoticz.LOG_DEBUG)

        if (item.isTimer) then
        	-- get current time & calculate sunrise and sunset.
        	time_now = os.date("*t")
        	minutes_now = time_now.min + time_now.hour * 60
        	
        	domoticz.log('Time in minutes: ' ..minutes_now, domoticz.LOG_DEBUG)
        	domoticz.log('Sunrise in minutes: ' ..timeofday['SunriseInMinutes'] - 60, domoticz.LOG_DEBUG)
        	domoticz.log('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60, domoticz.LOG_DEBUG)

        	-- Validation for sunrise and sunset.
        	if (minutes_now > timeofday['SunriseInMinutes'] - 30) and (minutes_now < timeofday['SunsetInMinutes'] + 30) then
                domoticz.log('Call Fronius url with callback', domoticz.LOG_DEBUG)
                -- ASYNC openUrl
                domoticz.openURL({
                    url = 'http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData',
                    method = 'GET',
                    callback = 'triggerFronius'
                })
            else                
                domoticz.log('Fronius inverter Offline because of SunSet', domoticz.LOG_INFO) 
            end
        end

        if (item.isHTTPResponse and item.ok) then
            domoticz.log('Callback from url requested', domoticz.LOG_DEBUG)
            domoticz.log('Data: '..item.data, domoticz.LOG_DEBUG)
            
            -- we know it is json but dzVents cannot detect this if there is no correct Content-Type
            -- convert to Lua
            local jsonfroniusdata = domoticz.utils.fromJSON(item.data)
            -- json is now a Lua table

            if (jsonfroniusdata == nil) then
                domoticz.log('Fronius inverter data is empty', domoticz.LOG_ERROR) 
                return
            end

            -- request successfull ?
            if ( jsonfroniusdata.Head.Status.Code ~= 0 ) then
                domoticz.log('Fronius inverter head status code data is not 0', domoticz.LOG_ERROR) 
                domoticz.log('Status code: ' + jsonfroniusdata.Head.Status.Code + ' Reason: ' + jsonfroniusdata.Head.Status.Reason + ' User message: ' + jsonfroniusdata.Head.Status.UserMessage, domoticz.LOG_ERROR) 
                return
            end


            local StatusCode = jsonfroniusdata.Body.Data.DeviceStatus.StatusCode 
            domoticz.log('json froniusdata Statuscode: '..StatusCode, domoticz.LOG_DEBUG) 
            
            if ( StatusCode == 7) then --Fronius converter is Running 
            		
    		local DAY_ENERGY	= jsonfroniusdata.Body.Data.DAY_ENERGY.Value/1000
    		local YEAR_ENERGY	= jsonfroniusdata.Body.Data.YEAR_ENERGY.Value/1000
    		local TOTAL_ENERGY	= jsonfroniusdata.Body.Data.TOTAL_ENERGY.Value/1000000
    		local PAC 			= jsonfroniusdata.Body.Data.PAC.Value
    		--local UDC			= jsonfroniusdata.Body.Data.UDC.Value -- Optional sensor, can be commented if not wanted
    		--local UAC			= jsonfroniusdata.Body.Data.UAC.Value -- Optional sensor, can be commented if not wanted 
    		--local IDC			= jsonfroniusdata.Body.Data.IDC.Value -- Optional sensor, can be commented if not wanted 
    		--local IAC			= jsonfroniusdata.Body.Data.IAC.Value -- Optional sensor, can be commented if not wanted

                domoticz.log('PAC: '..PAC, domoticz.LOG_DEBUG)
                domoticz.log('Day Energy: '..DAY_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Year Energy: '..YEAR_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Total Energy: '..TOTAL_ENERGY, domoticz.LOG_DEBUG)
                --domoticz.log('UDC: '..UDC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('UAC: '..UAC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('IDC: '..IDC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('IAC: '..IAC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted

                -- to trigger also influxdb update. replaced with the url
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_DAY_ENERGY..'&nvalue=0&svalue='..DAY_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_YEAR_ENERGY..'&nvalue=0&svalue='..YEAR_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_TOTAL_ENERGY..'&nvalue=0&svalue='..TOTAL_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue='..PAC..';'..DAY_ENERGY)
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC) -- Optional sensor, can be commented if not wanted
				
            else
		domoticz.log('Fronius converter state (Statuscode:'..StatusCode..') other than running', domoticz.LOG_INFO) 
		local UDC = 0	local UAC = 0	local IDC = 0	local IAC = 0

                -- to trigger also influxdb update. replaced with the url
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC) -- Optional sensor, can be commented if not wanted
            end

        end
    end

}
lumjajo
Posts: 67
Joined: Wednesday 26 April 2017 20:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Germany
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by lumjajo »

Does this script still run after updating the Fronius Inverters?
I had some issues with openWB but they fixed it there ;-) However in Domoticz I need to adjust the script. Any idea how?
Thanks
Joachim
glsf91
Posts: 58
Joined: Tuesday 14 November 2017 21:56
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by glsf91 »

lumjajo wrote: Friday 20 August 2021 11:23 Does this script still run after updating the Fronius Inverters?
I had some issues with openWB but they fixed it there ;-) However in Domoticz I need to adjust the script. Any idea how?
Thanks
Joachim
There is a change in an url parameter.
I used before "DeviceID". It should be "DeviceId" now.
kniazio
Posts: 198
Joined: Thursday 06 October 2016 8:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7243
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by kniazio »

I am trying to run the script on my own and I have this error all the time

Code: Select all

 2021-08-25 06:52:29.960 Error: dzVents: Error: (3.0.2) error loading module 'Solar' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Solar.lua':
2021-08-25 06:52:29.960 .../pi/domoticz/scripts/dzVents/generated_scripts/Solar.lua:89: ']' expected near '=
Here is my setup

Code: Select all

   local IPFronius = '192.168.1.199'        -- IP adress of the Fronius converter
   local idx_PV = 182                -- idx of the virtual sensor, you need to change this to your own Device IDx (Moc Solarna wyprodukowana)
   local idx_VERBRAUCH = 183               -- idx of PowerMeter (Energy provider supply)(moc pobrana od ZE)
   local idx_EIGENVERBRAUCH = 185          -- idx of Eigenverbrauch (House consumption W and computed KWh)(Zuycie wlasne)
   local idx_PEIGENVERBRAUCH = 184          -- idx of PEigenverbrauch (House consumption own consumed W and computed KWh)(Zuzycie prosto z dachu)

   --Create Virtual Hardware via JSON script (Copy this URL manualy)
   --Change IP, Port and name accordingly
   --http://192.168.1.154:8084/json.htm?type=command&param=addhardware&htype=15&port=1&name=Fronius&enabled=true
   --Create Virtual sensors via JSON script (Copy these URL manual)
   --Change IP, Port and idx (idx is of the previously created hardware ID) accordingly
   --http://192.168.1.154:8084/json.htm?type=createvirtualsensor&idx=19&sensorname=Solar Power&sensortype=18
   --http://192.168.1.154:8084/json.htm?type=createvirtualsensor&idx=19&sensorname=House consumption&sensortype=18 (enable computed)
   --http://192.168.1.154:8084/json.htm?type=createvirtualsensor&idx=19&sensorname=Own consumption&sensortype=18 (enable computed)
      --Above sensor needs to be manual adjusted to Type: Return.
   --http://192.168.1.154:8084/json.htm?type=createvirtualsensor&idx=19&sensorname=Energy provider supply&sensortype=248

   JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()   -- For Linux

--   local function tprint (t, indent, done) -- introduced done to prevent infinite loops for tables with self-references
--      local done = done or {} 
--      indent = indent or 0
--      for key, value in pairs (t) do
--        pre = (string.rep (" ", indent)) -- indent it
--        if type (value) == "table" and not done[value] then
--          done [value] = true
--          print (pre .. tostring (key) .. ":");
--          tprint (value, indent + 2, done)
--        elseif type(value) == 'function' and not done[value] then
--          print( pre .. (tostring (key) .. "()"))
--        else
--          pre = pre .. (tostring (key) .. ": ")
--         print (pre .. tostring(value))
--        end
--      end
--    end

   commandArray = {}
      --Extract data from Fronius converter.
      froniusurl   = 'curl "http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CommonInverterData"'
      jsondata    = assert(io.popen(froniusurl))
      froniusdevice = jsondata:read('*all')
      jsondata:close()
      froniusdata = JSON:decode(froniusdevice)

      -- extract data from Fronius Power Meter 63A
      froniusurl2   = 'curl "http://'..IPFronius..'/solar_api/v1/GetMeterRealtimeData.cgi?Scope=System"'
      jsondata2    = assert(io.popen(froniusurl2))
      froniusdevice2 = jsondata2:read('*all')
      jsondata2:close()
      froniusdata2 = JSON:decode(froniusdevice2)

      --Process fronius data
      if (froniusdata ~= nil) then
      local StatusCode       = froniusdata['Body']['Data']['DeviceStatus']['StatusCode']
         if( StatusCode == 7) then --Fronius converter is Running
           local DAY_ENERGY   = froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
           local PAC          = froniusdata['Body']['Data']['PAC']['Value']
           local VERBRAUCH    = froniusdata2['Body']['Data']['0']['PowerReal_P_Sum']            
           local EIGENVERBRAUCH = VERBRAUCH + PAC
           
           if VERBRAUCH < 0 then
           MYEIGENVERBRAUCH = EIGENVERBRAUCH
           elseif VERBRAUCH > 0 then
           MYEIGENVERBRAUCH = PAC
           end
           
           local PEIGENVERBRAUCH = MYEIGENVERBRAUCH
           
           commandArray[1] = {['UpdateDevice'] = idx_PV .. "|0|" .. PAC .. ";" .. DAY_ENERGY}
           commandArray[2] = {['UpdateDevice'] = idx_VERBRAUCH .. "|0|" .. VERBRAUCH }
           commandArray[3] = {['UpdateDevice'] = idx_EIGENVERBRAUCH .. "|0|" .. EIGENVERBRAUCH }
           commandArray[4] = {['UpdateDevice'] = idx_PEIGENVERBRAUCH .. "|0|" .. PEIGENVERBRAUCH }


         else
           local PAC=0
           local PEIGENVERBRAUCH = 0
           local DAY_ENERGY  = froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
           local VERBRAUCH   = froniusdata2['Body']['Data']['0']['PowerReal_P_Sum']            
           commandArray[1] = {['UpdateDevice'] = idx_PV .. "|0|" .. PAC .. ";" .. DAY_ENERGY}
           commandArray[2] = {['UpdateDevice'] = idx_VERBRAUCH .. "|0|" .. VERBRAUCH }
           commandArray[3] = {['UpdateDevice'] = idx_EIGENVERBRAUCH .. "|0|" .. VERBRAUCH }
           commandArray[4] = {['UpdateDevice'] = idx_PEIGENVERBRAUCH .. "|0|" .. PEIGENVERBRAUCH }
         end   
  end

--      tprint(commandArray)
Please help. I care about this

P.S. The script works. You had to remove the last line that was not from the script, but somehow found itself in the frame on the forum.
Question:
How to limit readings to 1 minute?
SterkeJerke
Posts: 18
Joined: Friday 01 February 2019 20:57
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by SterkeJerke »

Meulderg wrote: Sunday 14 March 2021 15:43 as i was rebuilding my Domoticz from scratch, i rewatched my old post.
Thanks glsf91 for editing the script, to be more resource friendly.
On the old script, i never used the values UAC IAC UDC and IDC. so i commented them out of this script
Also added some more help on what sensors to create.
Hope this helps others.

Code: Select all

-- Script to read solardata from FroniusAPI
-- Original Autor : Meulderg  http://www.domoticz.com/forum/viewtopic.php?t=15037#p119999
-- Changed to dzVents by glsf91
-- Date : 14-03-2021
-- This script needs to be placed in the /home/pi/domoticz/scripts/dzVents/scripts/ directory
-- Warning: Don't forget to setup the Local Networks otherwize the device counters won't get updated if site security is used.

return {
    active = true,
	logging = {
    	--level = domoticz.LOG_DEBUG, -- Uncomment to override the dzVents global logging setting and use debug
    	marker = 'Fronius'
	},
   on = {
      timer = {
         'every minute'
      },
      httpResponses = { 'triggerFronius' }
   },
   execute = function(domoticz,item)
       
       	-- Variables to customize ------------------------------------------------
	      --Create Virtual Hardware via JSON script, dubble click the URL after changing the requred settings. The Virtual hardware is then automaticly created.
    	    --Change IP, Port and name accordingly
    	    --http://192.168.1.4:8080/json.htm?type=command&param=addhardware&htype=15&port=1&name=Fronius&enabled=true
    	
    	--Create Virtual sensors via JSON script, dubble click the URL after changing the requred settings. The Virtual hardware is then automaticly created.
    	  --Change IP, Port and idx (idx is of the previously created hardware ID) accordingly
    	  --http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_Opbrengst&sensortype=18
    		--Above sensor needs to be manual adjusted to Type: Return. (Open the device and edit the type).
    	
		--For these sensors below create a virtual sensor manualy. Sensor type = Custom sensor.
        --Fronius_Day_Energy with label 'kW' 
    	--Fronius_Year_Energy with label 'kW'
    	--Fronius_Total_Energy with label 'MW'
		
		--The Virtual sensors below can be used, but if not delete them from the rest of the script also		
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_UAC&sensortype=4
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_UDC&sensortype=4
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_IAC&sensortype=19
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_IDC&sensortype=19       

	   -- If you change the names of the sensor, also change it below
    	local IPdomiticz = domoticz.variables('UV_DomoticzIP').value    -- Set this value in Domoticz as User Variable "String" -> IP adress of the domoticz service + port
    	local IPFronius = domoticz.variables('UV_FroniusIP').value		-- Set this value in Domoticz as User Variable "String" -> IP adress of the Fronius converter
    	local idx_Fronius_Opbrengst = domoticz.devices('Fronius_Opbrengst').idx
    	local idx_DAY_ENERGY = domoticz.devices('Fronius_Day_Energy').idx
    	local idx_YEAR_ENERGY = domoticz.devices('Fronius_Year_Energy').idx
    	local idx_TOTAL_ENERGY = domoticz.devices('Fronius_Total_Energy').idx
   	    --local idx_UDC = domoticz.devices('Fronius_UDC').idx
    	--local idx_UAC = domoticz.devices('Fronius_UAC').idx
    	--local idx_IDC = domoticz.devices('Fronius_IDC').idx
    	--local idx_IAC = domoticz.devices('Fronius_IAC').idx
		
    	-- one-time load of the routines
    	--	JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
		local json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()	-- For Linux

        domoticz.log('Fronius script running', domoticz.LOG_DEBUG)

        if (item.isTimer) then
        	-- get current time & calculate sunrise and sunset.
        	time_now = os.date("*t")
        	minutes_now = time_now.min + time_now.hour * 60
        	
        	domoticz.log('Time in minutes: ' ..minutes_now, domoticz.LOG_DEBUG)
        	domoticz.log('Sunrise in minutes: ' ..timeofday['SunriseInMinutes'] - 60, domoticz.LOG_DEBUG)
        	domoticz.log('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60, domoticz.LOG_DEBUG)

        	-- Validation for sunrise and sunset.
        	if (minutes_now > timeofday['SunriseInMinutes'] - 30) and (minutes_now < timeofday['SunsetInMinutes'] + 30) then
                domoticz.log('Call Fronius url with callback', domoticz.LOG_DEBUG)
                -- ASYNC openUrl
                domoticz.openURL({
                    url = 'http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData',
                    method = 'GET',
                    callback = 'triggerFronius'
                })
            else                
                domoticz.log('Fronius inverter Offline because of SunSet', domoticz.LOG_INFO) 
            end
        end

        if (item.isHTTPResponse and item.ok) then
            domoticz.log('Callback from url requested', domoticz.LOG_DEBUG)
            domoticz.log('Data: '..item.data, domoticz.LOG_DEBUG)
            
            -- we know it is json but dzVents cannot detect this if there is no correct Content-Type
            -- convert to Lua
            local jsonfroniusdata = domoticz.utils.fromJSON(item.data)
            -- json is now a Lua table

            if (jsonfroniusdata == nil) then
                domoticz.log('Fronius inverter data is empty', domoticz.LOG_ERROR) 
                return
            end

            -- request successfull ?
            if ( jsonfroniusdata.Head.Status.Code ~= 0 ) then
                domoticz.log('Fronius inverter head status code data is not 0', domoticz.LOG_ERROR) 
                domoticz.log('Status code: ' + jsonfroniusdata.Head.Status.Code + ' Reason: ' + jsonfroniusdata.Head.Status.Reason + ' User message: ' + jsonfroniusdata.Head.Status.UserMessage, domoticz.LOG_ERROR) 
                return
            end


            local StatusCode = jsonfroniusdata.Body.Data.DeviceStatus.StatusCode 
            domoticz.log('json froniusdata Statuscode: '..StatusCode, domoticz.LOG_DEBUG) 
            
            if ( StatusCode == 7) then --Fronius converter is Running 
            		
    		local DAY_ENERGY	= jsonfroniusdata.Body.Data.DAY_ENERGY.Value/1000
    		local YEAR_ENERGY	= jsonfroniusdata.Body.Data.YEAR_ENERGY.Value/1000
    		local TOTAL_ENERGY	= jsonfroniusdata.Body.Data.TOTAL_ENERGY.Value/1000000
    		local PAC 			= jsonfroniusdata.Body.Data.PAC.Value
    		--local UDC			= jsonfroniusdata.Body.Data.UDC.Value -- Optional sensor, can be commented if not wanted
    		--local UAC			= jsonfroniusdata.Body.Data.UAC.Value -- Optional sensor, can be commented if not wanted 
    		--local IDC			= jsonfroniusdata.Body.Data.IDC.Value -- Optional sensor, can be commented if not wanted 
    		--local IAC			= jsonfroniusdata.Body.Data.IAC.Value -- Optional sensor, can be commented if not wanted

                domoticz.log('PAC: '..PAC, domoticz.LOG_DEBUG)
                domoticz.log('Day Energy: '..DAY_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Year Energy: '..YEAR_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Total Energy: '..TOTAL_ENERGY, domoticz.LOG_DEBUG)
                --domoticz.log('UDC: '..UDC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('UAC: '..UAC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('IDC: '..IDC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('IAC: '..IAC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted

                -- to trigger also influxdb update. replaced with the url
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_DAY_ENERGY..'&nvalue=0&svalue='..DAY_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_YEAR_ENERGY..'&nvalue=0&svalue='..YEAR_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_TOTAL_ENERGY..'&nvalue=0&svalue='..TOTAL_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue='..PAC..';'..DAY_ENERGY)
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC) -- Optional sensor, can be commented if not wanted
				
            else
		domoticz.log('Fronius converter state (Statuscode:'..StatusCode..') other than running', domoticz.LOG_INFO) 
		local UDC = 0	local UAC = 0	local IDC = 0	local IAC = 0

                -- to trigger also influxdb update. replaced with the url
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC) -- Optional sensor, can be commented if not wanted
            end

        end
    end

}

Suggestion for improvement: Between Sunset and Sunrise, every time set the value for the Power to 0. (mine keeps stuck at f.e. 2 watt)
SterkeJerke
Posts: 18
Joined: Friday 01 February 2019 20:57
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by SterkeJerke »

Hi,

After (I think) a firmware update of the Fronius device, I get these errors:

2022-01-30 10:34:00.275 Error: dzVents: Error: (3.1.8) Fronius: Fronius inverter head status code data is not 0
2022-01-30 10:34:00.275 Error: dzVents: Error: (3.1.8) Fronius: An error occurred when calling event handler Fronius
2022-01-30 10:34:00.275 Error: dzVents: Error: (3.1.8) Fronius: ...n/domoticz/scripts/dzVents/generated_scripts/Fronius.lua:102: attempt to perform arithmetic on a string value

After turning on debug:

2022-01-30 10:39:00.376 Status: dzVents: Debug: Fronius: Callback from url requested
2022-01-30 10:39:00.376 Status: dzVents: Debug: Fronius: Data: {
2022-01-30 10:39:00.376 "Body" : {
2022-01-30 10:39:00.376 "Data" : {}
2022-01-30 10:39:00.376 },
2022-01-30 10:39:00.376 "Head" : {
2022-01-30 10:39:00.376 "RequestArguments" : {
2022-01-30 10:39:00.376 "DataCollection" : "CommonInverterData",
2022-01-30 10:39:00.376 "DeviceClass" : "Inverter",
2022-01-30 10:39:00.376 "Scope" : "Device"
2022-01-30 10:39:00.376 },
2022-01-30 10:39:00.376 "Status" : {
2022-01-30 10:39:00.376 "Code" : 6,
2022-01-30 10:39:00.376 "Reason" : "CGI-Args: Invalid parameter '' for attribute 'DeviceId' (must be numeric)",
2022-01-30 10:39:00.376 "UserMessage" : ""
2022-01-30 10:39:00.376 },
2022-01-30 10:39:00.376 "Timestamp" : "2022-01-30T10:38:57+01:00"
2022-01-30 10:39:00.376 }
2022-01-30 10:39:00.376 }
2022-01-30 10:39:00.376
2022-01-30 10:39:00.376 Status: dzVents: Info: Fronius: ------ Finished Fronius



Where to start looking?
glsf91
Posts: 58
Joined: Tuesday 14 November 2017 21:56
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by glsf91 »

SterkeJerke wrote: Sunday 30 January 2022 10:37 2022-01-30 10:39:00.376 "Reason" : "CGI-Args: Invalid parameter '' for attribute 'DeviceId' (must be numeric)",
This is the problem. Look at my post a few above yours. It is caused by a firmware update.
Search for DeviceID and change to DeviceId. It is in the url sended to Fronius inverter.
SterkeJerke
Posts: 18
Joined: Friday 01 February 2019 20:57
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by SterkeJerke »

Wow Thanks! That solved it.

Had the device and installed and upadated in december, so did not think that would be relavent.
SterkeJerke
Posts: 18
Joined: Friday 01 February 2019 20:57
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by SterkeJerke »

Meulderg wrote: Sunday 14 March 2021 15:43 as i was rebuilding my Domoticz from scratch, i rewatched my old post.
Thanks glsf91 for editing the script, to be more resource friendly.
On the old script, i never used the values UAC IAC UDC and IDC. so i commented them out of this script
Also added some more help on what sensors to create.
Hope this helps others.

Code: Select all

-- Script to read solardata from FroniusAPI
-- Original Autor : Meulderg  http://www.domoticz.com/forum/viewtopic.php?t=15037#p119999
-- Changed to dzVents by glsf91
-- Date : 14-03-2021
-- This script needs to be placed in the /home/pi/domoticz/scripts/dzVents/scripts/ directory
-- Warning: Don't forget to setup the Local Networks otherwize the device counters won't get updated if site security is used.

return {
    active = true,
	logging = {
    	--level = domoticz.LOG_DEBUG, -- Uncomment to override the dzVents global logging setting and use debug
    	marker = 'Fronius'
	},
   on = {
      timer = {
         'every minute'
      },
      httpResponses = { 'triggerFronius' }
   },
   execute = function(domoticz,item)
       
       	-- Variables to customize ------------------------------------------------
	      --Create Virtual Hardware via JSON script, dubble click the URL after changing the requred settings. The Virtual hardware is then automaticly created.
    	    --Change IP, Port and name accordingly
    	    --http://192.168.1.4:8080/json.htm?type=command&param=addhardware&htype=15&port=1&name=Fronius&enabled=true
    	
    	--Create Virtual sensors via JSON script, dubble click the URL after changing the requred settings. The Virtual hardware is then automaticly created.
    	  --Change IP, Port and idx (idx is of the previously created hardware ID) accordingly
    	  --http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_Opbrengst&sensortype=18
    		--Above sensor needs to be manual adjusted to Type: Return. (Open the device and edit the type).
    	
		--For these sensors below create a virtual sensor manualy. Sensor type = Custom sensor.
        --Fronius_Day_Energy with label 'kW' 
    	--Fronius_Year_Energy with label 'kW'
    	--Fronius_Total_Energy with label 'MW'
		
		--The Virtual sensors below can be used, but if not delete them from the rest of the script also		
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_UAC&sensortype=4
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_UDC&sensortype=4
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_IAC&sensortype=19
    	--http://192.168.1.4:8080/json.htm?type=createvirtualsensor&idx=2&sensorname=Fronius_IDC&sensortype=19       

	   -- If you change the names of the sensor, also change it below
    	local IPdomiticz = domoticz.variables('UV_DomoticzIP').value    -- Set this value in Domoticz as User Variable "String" -> IP adress of the domoticz service + port
    	local IPFronius = domoticz.variables('UV_FroniusIP').value		-- Set this value in Domoticz as User Variable "String" -> IP adress of the Fronius converter
    	local idx_Fronius_Opbrengst = domoticz.devices('Fronius_Opbrengst').idx
    	local idx_DAY_ENERGY = domoticz.devices('Fronius_Day_Energy').idx
    	local idx_YEAR_ENERGY = domoticz.devices('Fronius_Year_Energy').idx
    	local idx_TOTAL_ENERGY = domoticz.devices('Fronius_Total_Energy').idx
   	    --local idx_UDC = domoticz.devices('Fronius_UDC').idx
    	--local idx_UAC = domoticz.devices('Fronius_UAC').idx
    	--local idx_IDC = domoticz.devices('Fronius_IDC').idx
    	--local idx_IAC = domoticz.devices('Fronius_IAC').idx
		
    	-- one-time load of the routines
    	--	JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
		local json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()	-- For Linux

        domoticz.log('Fronius script running', domoticz.LOG_DEBUG)

        if (item.isTimer) then
        	-- get current time & calculate sunrise and sunset.
        	time_now = os.date("*t")
        	minutes_now = time_now.min + time_now.hour * 60
        	
        	domoticz.log('Time in minutes: ' ..minutes_now, domoticz.LOG_DEBUG)
        	domoticz.log('Sunrise in minutes: ' ..timeofday['SunriseInMinutes'] - 60, domoticz.LOG_DEBUG)
        	domoticz.log('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60, domoticz.LOG_DEBUG)

        	-- Validation for sunrise and sunset.
        	if (minutes_now > timeofday['SunriseInMinutes'] - 30) and (minutes_now < timeofday['SunsetInMinutes'] + 30) then
                domoticz.log('Call Fronius url with callback', domoticz.LOG_DEBUG)
                -- ASYNC openUrl
                domoticz.openURL({
                    url = 'http://'..IPFronius..'/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData',
                    method = 'GET',
                    callback = 'triggerFronius'
                })
            else                
                domoticz.log('Fronius inverter Offline because of SunSet', domoticz.LOG_INFO) 
            end
        end

        if (item.isHTTPResponse and item.ok) then
            domoticz.log('Callback from url requested', domoticz.LOG_DEBUG)
            domoticz.log('Data: '..item.data, domoticz.LOG_DEBUG)
            
            -- we know it is json but dzVents cannot detect this if there is no correct Content-Type
            -- convert to Lua
            local jsonfroniusdata = domoticz.utils.fromJSON(item.data)
            -- json is now a Lua table

            if (jsonfroniusdata == nil) then
                domoticz.log('Fronius inverter data is empty', domoticz.LOG_ERROR) 
                return
            end

            -- request successfull ?
            if ( jsonfroniusdata.Head.Status.Code ~= 0 ) then
                domoticz.log('Fronius inverter head status code data is not 0', domoticz.LOG_ERROR) 
                domoticz.log('Status code: ' + jsonfroniusdata.Head.Status.Code + ' Reason: ' + jsonfroniusdata.Head.Status.Reason + ' User message: ' + jsonfroniusdata.Head.Status.UserMessage, domoticz.LOG_ERROR) 
                return
            end


            local StatusCode = jsonfroniusdata.Body.Data.DeviceStatus.StatusCode 
            domoticz.log('json froniusdata Statuscode: '..StatusCode, domoticz.LOG_DEBUG) 
            
            if ( StatusCode == 7) then --Fronius converter is Running 
            		
    		local DAY_ENERGY	= jsonfroniusdata.Body.Data.DAY_ENERGY.Value/1000
    		local YEAR_ENERGY	= jsonfroniusdata.Body.Data.YEAR_ENERGY.Value/1000
    		local TOTAL_ENERGY	= jsonfroniusdata.Body.Data.TOTAL_ENERGY.Value/1000000
    		local PAC 			= jsonfroniusdata.Body.Data.PAC.Value
    		--local UDC			= jsonfroniusdata.Body.Data.UDC.Value -- Optional sensor, can be commented if not wanted
    		--local UAC			= jsonfroniusdata.Body.Data.UAC.Value -- Optional sensor, can be commented if not wanted 
    		--local IDC			= jsonfroniusdata.Body.Data.IDC.Value -- Optional sensor, can be commented if not wanted 
    		--local IAC			= jsonfroniusdata.Body.Data.IAC.Value -- Optional sensor, can be commented if not wanted

                domoticz.log('PAC: '..PAC, domoticz.LOG_DEBUG)
                domoticz.log('Day Energy: '..DAY_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Year Energy: '..YEAR_ENERGY, domoticz.LOG_DEBUG)
                domoticz.log('Total Energy: '..TOTAL_ENERGY, domoticz.LOG_DEBUG)
                --domoticz.log('UDC: '..UDC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('UAC: '..UAC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('IDC: '..IDC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted
                --domoticz.log('IAC: '..IAC, domoticz.LOG_DEBUG) -- Optional sensor, can be commented if not wanted

                -- to trigger also influxdb update. replaced with the url
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_DAY_ENERGY..'&nvalue=0&svalue='..DAY_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_YEAR_ENERGY..'&nvalue=0&svalue='..YEAR_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_TOTAL_ENERGY..'&nvalue=0&svalue='..TOTAL_ENERGY)
                domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue='..PAC..';'..DAY_ENERGY)
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC) -- Optional sensor, can be commented if not wanted
				
            else
		domoticz.log('Fronius converter state (Statuscode:'..StatusCode..') other than running', domoticz.LOG_INFO) 
		local UDC = 0	local UAC = 0	local IDC = 0	local IAC = 0

                -- to trigger also influxdb update. replaced with the url
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC) -- Optional sensor, can be commented if not wanted
                --domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC) -- Optional sensor, can be commented if not wanted
            end

        end
    end

}
How would I set the "Fronius_Opbrengst" to 0 in the part after sunset/before sunrise?
Schermafbeelding 2022-01-30 180759.png
Schermafbeelding 2022-01-30 180759.png (14.86 KiB) Viewed 1512 times
glsf91
Posts: 58
Joined: Tuesday 14 November 2017 21:56
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by glsf91 »

SterkeJerke wrote: Sunday 30 January 2022 18:09 How would I set the "Fronius_Opbrengst" to 0 in the part after sunset/before sunrise?
Schermafbeelding 2022-01-30 180759.png
Do something like this after "domoticz.log('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60, domoticz.LOG_DEBUG)":
-- set day energie to 0 at start of day
if (minutes_now == 5) then
domoticz.log('Set day energy to zero at start of day', domoticz.LOG_INFO)
domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue=0;0')
end
This will run at 00:05
SterkeJerke
Posts: 18
Joined: Friday 01 February 2019 20:57
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by SterkeJerke »

HAd the bright idea to put it behind:
SterkeJerke
Posts: 18
Joined: Friday 01 February 2019 20:57
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by SterkeJerke »

HAd the bright idea to put it behind:

domoticz.log('Fronius inverter Offline because of SunSet', domoticz.LOG_INFO)
domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue=0;0')

Is there a way, to not override the last value? Or something like:

domoticz.openURL('http://'..IPdomiticz..'/json.htm?type=command&param=udevice&idx='..idx_Fronius_Opbrengst..'&nvalue=0&svalue=0;'..DAY_ENERGY)
glsf91
Posts: 58
Joined: Tuesday 14 November 2017 21:56
Target OS: Linux
Domoticz version:
Contact:

Re: Solved: Problem with LUA script reading from Fronius inverter

Post by glsf91 »

Get the current value of day energie and use this as last value.
domoticz.devices('Fronius_Total_Energy').counterToday
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests