Page 1 of 2

Solved: Problem with LUA script reading from Fronius inverter

Posted: Saturday 24 December 2016 11:10
by Reinder
Hi,

I´m trying to make a LUA script that reads the data from my Fronius inverter into Domoticz. For some reason it doesn´t work when I try to read directly from the url. When I download the file that´s created from calling the url and I save that to my pi, then the script does work.

The error I get when reading from the url is
Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_get_solar.lua: /home/pi/domoticz/scripts/lua/script_device_get_solar.lua:27: attempt to index field 'Body' (a nil value)

Code: Select all

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

local config=assert(io.popen('curl http://192.168.1.11/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'))
-- local config=assert(io.open('/home/pi/domoticz/scripts/lua/test.js')) --with this line it works
local productie = config:read('*all')
config:close()
local jsonProductie = json:decode(productie)

dagenergie = jsonProductie.Body.Data.FAC.Unit

Any suggestions what´s going on?

Re: Problem with LUA script reading from Fronius inverter

Posted: Monday 26 December 2016 14:47
by Reinder
Fixed it! The http-address needs to be enclosed with "http: etc" because of the &.

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

Posted: Friday 27 January 2017 16:01
by caveman85
Can you please share the complete (renewed) code? I have a Fronius Inverter and want to test with getting the data into Domoticz also!

Solved: Problem with LUA script reading from Fronius inverter

Posted: Thursday 16 February 2017 12:44
by Meulderg
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.

Code: Select all

-- Script to read solardata from FroniusAPI
-- Autor : Meulderg
-- Date : 29-Jan-2017
-- Name: script_time_Fronius.lua
-- Warning: Don't forget to setup the Local Networks otherwize the device counters won't get updated if site security is used.

		-- Variables to customize ------------------------------------------------
	local DEBUG = 0            				-- 0 is off, 1 for domoticz log, 2 for fronius JSON data to log
	local IPdomiticz = '192.168.xxx.xxx:8080'	-- IP adress of the domoticz service + port
	local IPFronius = '192.168.xxx.xxx'		-- IP adress of the Fronius converter
	local idx_Fronius_Opbrengst = 19		-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_UDC = 16 						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_UAC = 15						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_IDC = 18						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_IAC = 17						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_DAY_ENERGY = 12				-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_YEAR_ENERGY = 13				-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_TOTAL_ENERGY = 14				-- idx of the virtual sensor, you need to change this to your own Device IDx
	
	
	--Create Virtual Hardware via JSON script (Copy this URL manualy)
	--Change IP, Port and name accordingly
	--http://127.0.0.1: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://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Opbrengst&sensortype=18
		--Above sensor needs to be manual adjusted to Type: Return.
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Day_Energy&sensortype=248
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Year_Energy&sensortype=248
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Total_Energy&sensortype=248
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_UAC&sensortype=4
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_UDC&sensortype=4
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_IAC&sensortype=19
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_IDC&sensortype=19
	
	
	-- one-time load of the routines
		JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
	--	JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()	-- For Linux
	
	commandArray = {}
	if(DEBUG == 1) then print ('Fronius script running!!') end
	-- get current time & calculate sunrise and sunset.
	time_now = os.date("*t")
	minutes_now = time_now.min + time_now.hour * 60
    if(DEBUG == 1) then print ('Time in minutes: ' ..minutes_now) end
	if(DEBUG == 1) then print ('Sunrise in minutes: ' ..timeofday['SunriseInMinutes'] - 60) end
	if(DEBUG == 1) then print ('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60) end	
	
	-- Validation for sunrise and sunset.
	if (minutes_now > timeofday['SunriseInMinutes'] - 30) and (minutes_now < timeofday['SunsetInMinutes'] + 30) then
    
		--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)
	
		--Process fronius data
		if (froniusdata ~= nil) then
		if(DEBUG == 2) then print (froniusdevice) end
		local StatusCode 		= froniusdata['Body']['Data']['DeviceStatus']['StatusCode'] 
			if( StatusCode == 7) then --Fronius converter is Running
		
			local DAY_ENERGY	= froniusdata['Body']['Data']['DAY_ENERGY']['Value']
			local YEAR_ENERGY	= froniusdata['Body']['Data']['YEAR_ENERGY']['Value']
			local TOTAL_ENERGY	= froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
			local PAC 			= froniusdata['Body']['Data']['PAC']['Value']
			local UDC			= froniusdata['Body']['Data']['UDC']['Value']
			local UAC			= froniusdata['Body']['Data']['UAC']['Value']
			local IDC			= froniusdata['Body']['Data']['IDC']['Value']
			local IAC			= froniusdata['Body']['Data']['IAC']['Value']
			local UDC			= froniusdata['Body']['Data']['UDC']['Value']				
	
			if(DEBUG == 1) then print ('PAC: '..PAC) end
			if(DEBUG == 1) then print ('Day Energy: '..DAY_ENERGY) end
			if(DEBUG == 1) then print ('Year Energy: '..YEAR_ENERGY) end
			if(DEBUG == 1) then print ('Total Energy: '..TOTAL_ENERGY) end
			if(DEBUG == 1) then print ('UDC: '..UDC) end
			if(DEBUG == 1) then print ('UAC: '..UAC) end
			if(DEBUG == 1) then print ('IDC: '..IDC) end
			if(DEBUG == 1) then print ('IAC: '..IAC) end
			
			--To update the devices.
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_Fronius_Opbrengst.."&nvalue=0&svalue="..PAC..";"..DAY_ENERGY..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UDC.."&nvalue=0&svalue="..UDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IDC.."&nvalue=0&svalue="..IDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UAC.."&nvalue=0&svalue="..UAC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IAC.."&nvalue=0&svalue="..IAC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_DAY_ENERGY.."&nvalue=0&svalue="..DAY_ENERGY..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_YEAR_ENERGY.."&nvalue=0&svalue="..YEAR_ENERGY..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_TOTAL_ENERGY.."&nvalue=0&svalue="..TOTAL_ENERGY..""}
			
			else
			print ('Fronius converter state other than running')
			local UDC = 0	local UAC = 0	local IDC = 0	local IAC = 0
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UDC.."&nvalue=0&svalue="..UDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IDC.."&nvalue=0&svalue="..IDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UAC.."&nvalue=0&svalue="..UAC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IAC.."&nvalue=0&svalue="..IAC..""}
			end	
			
		else
		print ('Fronius converter data is empty or unreachable')
		end
	else	
	print ('Fronius converter Offline')	
	end
	return commandArray


-- Example of the data returned by the Fronius converter	
--[[
{
	"Head" : {
		"RequestArguments" : {
			"DataCollection" : "CommonInverterData",
			"DeviceClass" : "Inverter",
			"DeviceId" : "1",
			"Scope" : "Device"
		},
		"Status" : {
			"Code" : 0,
			"Reason" : "",
			"UserMessage" : ""
		},
		"Timestamp" : "2017-01-29T12:16:22+01:00"
	},
	"Body" : {
		"Data" : {
			"DAY_ENERGY" : {
				"Value" : 1535.1,
				"Unit" : "Wh"
			},
			"FAC" : {
				"Value" : 50,
				"Unit" : "Hz"
			},
			"IAC" : {
				"Value" : 4.68,
				"Unit" : "A"
			},
			"IDC" : {
				"Value" : 3.49,
				"Unit" : "A"
			},
			"PAC" : {
				"Value" : 1062,
				"Unit" : "W"
			},
			"TOTAL_ENERGY" : {
				"Value" : 839677.06,
				"Unit" : "Wh"
			},
			"UAC" : {
				"Value" : 231.1,
				"Unit" : "V"
			},
			"UDC" : {
				"Value" : 342.7,
				"Unit" : "V"
			},
			"YEAR_ENERGY" : {
				"Value" : 119222.5,
				"Unit" : "Wh"
			},
			"DeviceStatus" : {
				"StatusCode" : 7,
				"MgmtTimerRemainingTime" : -1,
				"ErrorCode" : 0,
				"LEDColor" : 2,
				"LEDState" : 0,
				"StateToReset" : false
			}
		}
	}
}
]]--
This code is used on a windows based domoticz.
Sensors Fronius
Sensors Fronius
2017-02-16_Fronius.png (100.13 KiB) Viewed 7498 times

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

Posted: Wednesday 01 March 2017 16:00
by randriese
After configuring this script on my machine i'm getting log entries that the script takes more than 10 seconds to run.
also, Domoticz itself gets unresponsive and laggy...

anyone else having this problem? I'm running Domoticz on a Raspberry Pi 2B

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

Posted: Friday 03 March 2017 11:56
by Meulderg
Today i change from a windows machine to a Raspberry PI 3B.
Only had to change the location of the LUA script and the IDs of the sensors.
works for me :-)

Also i don't have any errors as you can see in my log.

Code: Select all

2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Fetching url... 
2016-11-23 14:29:01.123 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_Fronius.lua 
2016-11-23 14:29:02.107 (Fronius) General/Voltage (Fronius_UDC) 
2016-11-23 14:29:02.119 (Fronius) General/Voltage (Fronius_UAC) 
2016-11-23 14:29:02.129 (Fronius) General/Custom Sensor (Fronius_Day_Energy) 
2016-11-23 14:29:02.136 (Fronius) General/Custom Sensor (Fronius_Year_Energy) 
2016-11-23 14:29:02.140 (Fronius) General/Custom Sensor (Fronius_Total_Energy) 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Fetching url... 
2016-11-23 14:30:01.041 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_Fronius.lua 
2016-11-23 14:30:02.027 (Fronius) General/Voltage (Fronius_UDC) 
2016-11-23 14:30:02.046 (Fronius) General/Voltage (Fronius_UAC) 
2016-11-23 14:30:02.058 (Fronius) General/Custom Sensor (Fronius_Day_Energy) 
2016-11-23 14:30:02.062 (Fronius) General/Custom Sensor (Fronius_Year_Energy) 
2016-11-23 14:30:02.066 (Fronius) General/Custom Sensor (Fronius_Total_Energy) 
2016-11-23 14:31:00.842 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Fetching url... 
2016-11-23 14:31:00.843 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_Fronius.lua 
2016-11-23 14:31:01.834 (Fronius) General/Voltage (Fronius_UDC) 
2016-11-23 14:31:01.844 (Fronius) General/Voltage (Fronius_UAC) 
2016-11-23 14:31:01.856 (Fronius) General/Custom Sensor (Fronius_Day_Energy) 
2016-11-23 14:31:01.859 (Fronius) General/Custom Sensor (Fronius_Year_Energy) 
2016-11-23 14:31:01.864 (Fronius) General/Custom Sensor (Fronius_Total_Energy) 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Fetching url... 
2016-11-23 14:32:01.207 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_Fronius.lua 
2016-11-23 14:32:02.194 (Fronius) General/Voltage (Fronius_UDC) 
2016-11-23 14:32:02.207 (Fronius) General/Voltage (Fronius_UAC) 
2016-11-23 14:32:02.219 (Fronius) General/Custom Sensor (Fronius_Day_Energy) 
2016-11-23 14:32:02.223 (Fronius) General/Custom Sensor (Fronius_Year_Energy) 
2016-11-23 14:32:02.228 (Fronius) General/Custom Sensor (Fronius_Total_Energy) 

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

Posted: Friday 05 May 2017 10:56
by lumjajo
Hi Meulderg, I used your script, to read out the data of my Fronius inverter. Works almost perfect, except, it does really put a load on the raspberry as well as on the network, by constantly fetching the JSON data. Do you still run it this way or did you already modify. I was trying to put a loop or wait into it, but nothing really worked.
I thin its enough to read out data every 5min. And that would be really helpful. Can you or anyone else support me in this?
Thanks
Jo

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

Posted: Friday 05 May 2017 18:35
by Meulderg
Hi Jo,
Yes, i'm still running the script as i posted it here.
Can't say that mine is running with a high load.
DomoticsCPU.jpg
DomoticsCPU.jpg (90.72 KiB) Viewed 7273 times
I believe it is possible to change the runtime of the script by adding
time = os.date("*t")
if ((time.min % 15)==0) then


Hope this helps.

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

Posted: Sunday 02 July 2017 12:53
by Tonnie
Hi Meulberg
I'm using your script. In my log I can see the results. I like to add the hardware and other sensors.
Can you tell me how must I do that ?

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

Posted: Friday 28 July 2017 16:10
by Meulderg
Hi Tonnie,

Sorry for my late resonse, but in the script there are also url's to create hardware and sensors.
That is how i did it.
Create Virtual Hardware via JSON script (Copy this URL manualy)
--Change IP, Port and name accordingly
--http://127.0.0.1:8080/json.htm?type=com ... abled=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://127.0.0.1:8080/json.htm?type=cre ... sortype=18
--Above sensor needs to be manual adjusted to Type: Return.
--http://127.0.0.1:8080/json.htm?type=cre ... ortype=248
--http://127.0.0.1:8080/json.htm?type=cre ... ortype=248
--http://127.0.0.1:8080/json.htm?type=cre ... ortype=248
--http://127.0.0.1:8080/json.htm?type=cre ... nsortype=4
--http://127.0.0.1:8080/json.htm?type=cre ... nsortype=4
--http://127.0.0.1:8080/json.htm?type=cre ... sortype=19
--http://127.0.0.1:8080/json.htm?type=cre ... sortype=19

of course you can/must adjust the ip address and the name of the sensor.

Hope this will help you.

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

Posted: Friday 25 August 2017 11:57
by kinghuzi
Hi Meulderg,

First of all thank you for the above script, works like a charm. I also have a fronius smart meter attached to my inverter. Would it be possible to create a version of your script that pulls data off the smart meter (Its related to consumption).

The JSON URL is http://192.168.1.230/solar_api/v1/GetMe ... ope=System

The data is presented as below, it would be great to have devices for the below particularly important is the "PowerReal_P_Sum". I tried creating one myself but have no clue on LUA coding and kept getting all sorts of errors. Also could you advice how to add a delay to the script to make it run every 5 seconds.

Code: Select all

{
   "Body" : {
      "Data" : {
         "0" : {
            "Current_AC_Phase_1" : 4.5040002139285207,
            "Current_AC_Sum" : 4.5040002139285207,
            "Details" : {
               "Manufacturer" : "Fronius",
               "Model" : "Smart Meter 63A-1",
               "Serial" : "17081671"
            },
            "Enable" : 1,
            "EnergyReactive_VArAC_Phase_1_Consumed" : 480,
            "EnergyReactive_VArAC_Phase_1_Produced" : 808800,
            "EnergyReactive_VArAC_Sum_Consumed" : 480,
            "EnergyReactive_VArAC_Sum_Produced" : 808800,
            "EnergyReal_WAC_Minus_Absolute" : 89241,
            "EnergyReal_WAC_Phase_1_Consumed" : 116174,
            "EnergyReal_WAC_Phase_1_Produced" : 89241,
            "EnergyReal_WAC_Plus_Absolute" : 116174,
            "EnergyReal_WAC_Sum_Consumed" : 116174,
            "EnergyReal_WAC_Sum_Produced" : 89241,
            "Frequency_Phase_Average" : 50.00000074505806,
            "Meter_Location_Current" : 0,
            "PowerApparent_S_Phase_1" : 1058.8899763319641,
            "PowerApparent_S_Sum" : 1058.8899763319641,
            "PowerFactor_Phase_1" : 0.74999998323619366,
            "PowerFactor_Sum" : 0.74999998323619366,
            "PowerReactive_Q_Phase_1" : -519.99998837709427,
            "PowerReactive_Q_Sum" : -519.99998837709427,
            "PowerReal_P_Phase_1" : 802.07998207211494,
            "PowerReal_P_Sum" : 802.07998207211494,
            "TimeStamp" : 1503649834,
            "Visible" : 1,
            "Voltage_AC_Phase_1" : 235.1000111666508
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DeviceClass" : "Meter",
         "Scope" : "System"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2017-08-25T18:30:34+10:00"
   }
}

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

Posted: Friday 25 August 2017 13:00
by Meulderg
Hi Kinghuzi,
As i sad before i'm no scripting guy myself, it took me a long time of experimenting to get things working.

Try to adjust my script to get data from your Smartmeter.
If the URL (which i cannot access). you posted gives you the correct data.
Than try if it gives you any data in the Domoticz logging and work up from there.
Also set the DEBUG to 1 in the script to see what the data is giving you.

And i don't think you can read data every 5 seconds, as the minimum for launching a script is 1 minute.
(but correct me if i'm wrong).

Code: Select all

		-- Variables to customize ------------------------------------------------
	local DEBUG = 1            				-- 0 is off, 1 for domoticz log, 2 for fronius JSON data to log
	local IPdomiticz = '192.168.xxx.xxx:8080'	-- IP adress of the domoticz service + port
	local IPFronius = '192.168.1.230'		-- IP adress of the Fronius converter
	
	-- one-time load of the routines
		JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
	--	JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()	-- For Linux
	
	commandArray = {}
	if(DEBUG == 1) then print ('Fronius script running!!') end
	    
		--Extract data from Fronius Smartmeter.
		froniusurl	= 'curl "http://'..IPFronius..'/solar_api/v1/GetMeterRealtimeData.cgi?Scope=System"'
		jsondata    = assert(io.popen(froniusurl))
		froniusdevice = jsondata:read('*all')
		jsondata:close()
		froniusdata = JSON:decode(froniusdevice)
	
		--Process fronius data
		if (froniusdata ~= nil) then
		if(DEBUG == 2) then print (froniusdevice) end
		local StatusCode 		= froniusdata['Body']['Data']['0']['Enable']
			if( StatusCode == 1) then --Fronius Smartmeter is Running
		
			local PowerReal-P-Sum	= froniusdata['Body']['Data']['0']['PowerReal_P_Sum']
						
			if(DEBUG == 1) then print ('PowerReal P Sum: '..PowerReal-P-Sum) end
			
			
		else
		print ('Fronius converter data is empty or unreachable')
		end
	else	
	print ('Fronius converter Offline')	
	end
	return commandArray

Hope this will help you.

Kind regards,
Geert-Jan

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

Posted: Sunday 27 August 2017 6:44
by kinghuzi
Managed to get consumption data from fronius. Still struggling to add delay in the script so it doesn't run every few ms as it's using near 25% rp3 cpu.

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

Posted: Monday 04 September 2017 11:26
by kinghuzi
Has anyone had issue of blocklys not working after this lua runs for a day or so. Only a Domo restart works.

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

Posted: Monday 15 January 2018 10:13
by fmal
Hi Meulderg, I'm using your script for my Fronius Primo Inverter and it works fine. The only minor problem is that the inverter firmware gives a wrong value for DAY_ENERGY, it's a known bug and they will fix it.

But I did not understand this comment in your script:
--http://127.0.0.1:8080/json.htm?type=cre ... sortype=18
--Above sensor needs to be manual adjusted to Type: Return.
What do you mean by "manual adjusted"? I do not know how to change the sensor type in Domoticz, and it looks as working this way.
My sensors are these:

Code: Select all

Idx Hardware ID Unit Nome Modello Sottotipo Dati 
73	Fronius	00082073	1	Fronius_UAC	General	Voltage	223.2 V	
72	Fronius	82072	1	Fronius_Year_Energy	Usage	Electric	73035.8 Watt	
71	Fronius	82071	1	Fronius_Day_Energy	Usage	Electric	269.9 Watt	
70	Fronius	00082070	1	Fronius_Prod	General	kWh	0.2699 kWh	
Ty for your great help!

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

Posted: Monday 15 January 2018 11:31
by fmal
Some minor errors in this scripts:
in line 86:
commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_Fronius_Opbrengst.."&nvalue=0&svalue="..PAC..";"..DAY_ENERGY..""}

there is DAY_ENERGY and I think it should be:

commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_Fronius_Opbrengst.."&nvalue=0&svalue="..PAC..""}

Then the types of the sensors for DAY_ENERGY, YEAR_ENERGY and TOTAL_ENERGY report Watts as measurement unit. I think that ony PAC is the punctual power from FV and is expressed in W, the cumulated values should be kWh.

Another thing: now I only collect PAC and UAC, but these two values are changing forever, and my logs are growing until system crashes. I have debug = 0 but the data logged are really too big, I have two calls to the script each second. How can I slower the check?
TY

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

Posted: Tuesday 06 February 2018 12:31
by emulan
even if i have correct ip and can read my fronius primo inverter, i get :fronius converter data is empty or unreachable

any suggestion?

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

Posted: Saturday 12 May 2018 23:09
by Davil
Meulderg wrote: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.

Code: Select all

-- Script to read solardata from FroniusAPI
-- Autor : Meulderg
-- Date : 29-Jan-2017
-- Name: script_time_Fronius.lua
-- Warning: Don't forget to setup the Local Networks otherwize the device counters won't get updated if site security is used.

		-- Variables to customize ------------------------------------------------
	local DEBUG = 0            				-- 0 is off, 1 for domoticz log, 2 for fronius JSON data to log
	local IPdomiticz = '192.168.xxx.xxx:8080'	-- IP adress of the domoticz service + port
	local IPFronius = '192.168.xxx.xxx'		-- IP adress of the Fronius converter
	local idx_Fronius_Opbrengst = 19		-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_UDC = 16 						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_UAC = 15						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_IDC = 18						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_IAC = 17						-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_DAY_ENERGY = 12				-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_YEAR_ENERGY = 13				-- idx of the virtual sensor, you need to change this to your own Device IDx
	local idx_TOTAL_ENERGY = 14				-- idx of the virtual sensor, you need to change this to your own Device IDx
	
	
	--Create Virtual Hardware via JSON script (Copy this URL manualy)
	--Change IP, Port and name accordingly
	--http://127.0.0.1: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://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Opbrengst&sensortype=18
		--Above sensor needs to be manual adjusted to Type: Return.
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Day_Energy&sensortype=248
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Year_Energy&sensortype=248
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_Total_Energy&sensortype=248
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_UAC&sensortype=4
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_UDC&sensortype=4
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_IAC&sensortype=19
	--http://127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=4&sensorname=Fronius_IDC&sensortype=19
	
	
	-- one-time load of the routines
		JSON = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")()		-- For Windows
	--	JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()	-- For Linux
	
	commandArray = {}
	if(DEBUG == 1) then print ('Fronius script running!!') end
	-- get current time & calculate sunrise and sunset.
	time_now = os.date("*t")
	minutes_now = time_now.min + time_now.hour * 60
    if(DEBUG == 1) then print ('Time in minutes: ' ..minutes_now) end
	if(DEBUG == 1) then print ('Sunrise in minutes: ' ..timeofday['SunriseInMinutes'] - 60) end
	if(DEBUG == 1) then print ('Sunset in minutes: ' ..timeofday['SunsetInMinutes'] + 60) end	
	
	-- Validation for sunrise and sunset.
	if (minutes_now > timeofday['SunriseInMinutes'] - 30) and (minutes_now < timeofday['SunsetInMinutes'] + 30) then
    
		--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)
	
		--Process fronius data
		if (froniusdata ~= nil) then
		if(DEBUG == 2) then print (froniusdevice) end
		local StatusCode 		= froniusdata['Body']['Data']['DeviceStatus']['StatusCode'] 
			if( StatusCode == 7) then --Fronius converter is Running
		
			local DAY_ENERGY	= froniusdata['Body']['Data']['DAY_ENERGY']['Value']
			local YEAR_ENERGY	= froniusdata['Body']['Data']['YEAR_ENERGY']['Value']
			local TOTAL_ENERGY	= froniusdata['Body']['Data']['TOTAL_ENERGY']['Value']
			local PAC 			= froniusdata['Body']['Data']['PAC']['Value']
			local UDC			= froniusdata['Body']['Data']['UDC']['Value']
			local UAC			= froniusdata['Body']['Data']['UAC']['Value']
			local IDC			= froniusdata['Body']['Data']['IDC']['Value']
			local IAC			= froniusdata['Body']['Data']['IAC']['Value']
			local UDC			= froniusdata['Body']['Data']['UDC']['Value']				
	
			if(DEBUG == 1) then print ('PAC: '..PAC) end
			if(DEBUG == 1) then print ('Day Energy: '..DAY_ENERGY) end
			if(DEBUG == 1) then print ('Year Energy: '..YEAR_ENERGY) end
			if(DEBUG == 1) then print ('Total Energy: '..TOTAL_ENERGY) end
			if(DEBUG == 1) then print ('UDC: '..UDC) end
			if(DEBUG == 1) then print ('UAC: '..UAC) end
			if(DEBUG == 1) then print ('IDC: '..IDC) end
			if(DEBUG == 1) then print ('IAC: '..IAC) end
			
			--To update the devices.
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_Fronius_Opbrengst.."&nvalue=0&svalue="..PAC..";"..DAY_ENERGY..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UDC.."&nvalue=0&svalue="..UDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IDC.."&nvalue=0&svalue="..IDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UAC.."&nvalue=0&svalue="..UAC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IAC.."&nvalue=0&svalue="..IAC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_DAY_ENERGY.."&nvalue=0&svalue="..DAY_ENERGY..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_YEAR_ENERGY.."&nvalue=0&svalue="..YEAR_ENERGY..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_TOTAL_ENERGY.."&nvalue=0&svalue="..TOTAL_ENERGY..""}
			
			else
			print ('Fronius converter state other than running')
			local UDC = 0	local UAC = 0	local IDC = 0	local IAC = 0
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UDC.."&nvalue=0&svalue="..UDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IDC.."&nvalue=0&svalue="..IDC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_UAC.."&nvalue=0&svalue="..UAC..""}
			commandArray[#commandArray + 1]={['OpenURL']="http://"..IPdomiticz.."/json.htm?type=command&param=udevice&idx="..idx_IAC.."&nvalue=0&svalue="..IAC..""}
			end	
			
		else
		print ('Fronius converter data is empty or unreachable')
		end
	else	
	print ('Fronius converter Offline')	
	end
	return commandArray


-- Example of the data returned by the Fronius converter	
--[[
{
	"Head" : {
		"RequestArguments" : {
			"DataCollection" : "CommonInverterData",
			"DeviceClass" : "Inverter",
			"DeviceId" : "1",
			"Scope" : "Device"
		},
		"Status" : {
			"Code" : 0,
			"Reason" : "",
			"UserMessage" : ""
		},
		"Timestamp" : "2017-01-29T12:16:22+01:00"
	},
	"Body" : {
		"Data" : {
			"DAY_ENERGY" : {
				"Value" : 1535.1,
				"Unit" : "Wh"
			},
			"FAC" : {
				"Value" : 50,
				"Unit" : "Hz"
			},
			"IAC" : {
				"Value" : 4.68,
				"Unit" : "A"
			},
			"IDC" : {
				"Value" : 3.49,
				"Unit" : "A"
			},
			"PAC" : {
				"Value" : 1062,
				"Unit" : "W"
			},
			"TOTAL_ENERGY" : {
				"Value" : 839677.06,
				"Unit" : "Wh"
			},
			"UAC" : {
				"Value" : 231.1,
				"Unit" : "V"
			},
			"UDC" : {
				"Value" : 342.7,
				"Unit" : "V"
			},
			"YEAR_ENERGY" : {
				"Value" : 119222.5,
				"Unit" : "Wh"
			},
			"DeviceStatus" : {
				"StatusCode" : 7,
				"MgmtTimerRemainingTime" : -1,
				"ErrorCode" : 0,
				"LEDColor" : 2,
				"LEDState" : 0,
				"StateToReset" : false
			}
		}
	}
}
]]--
This code is used on a windows based domoticz.
2017-02-16_Fronius.png
Hello Meulderg!

I am trying to understand how to do this on my Raspberry pi but unfortunately I am a newbie with Lua-stuff Image️.

I have created that Hardware dummy and all the virtual sensors. But then Im stuck and don't understand what /how to do then?! I also understand how to read from fronius API, but not how to transfer the reading to the virtual sensors.

Im so frustrated that Im such a noob. I wish someone could fixed it remotely for me hehe... I really want this to work!

Best regards /David from Sweden

Skickat från min HTC U11 plus via Tapatalk


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

Posted: Saturday 19 January 2019 12:42
by tjabas
I also have a fronius inverter want to connect it to domoticz, but i wonder how i install this script on my raspberry, im not good at programming.

thanks

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

Posted: Tuesday 05 February 2019 17:46
by tjabas
i finally got it working ,all of the sensors are uppdating, but now the blockly wont work, this script is updating the sensors every second, and it seems to freeze all my blocky configuration, is there a way to decrease the updating frequency?