A long time ago, but thanks for your script.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.
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¶m=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¶m=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_DAY_ENERGY..'&nvalue=0&svalue='..DAY_ENERGY)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_YEAR_ENERGY..'&nvalue=0&svalue='..YEAR_ENERGY)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_TOTAL_ENERGY..'&nvalue=0&svalue='..TOTAL_ENERGY)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=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¶m=udevice&idx='..idx_UDC..'&nvalue=0&svalue='..UDC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_IDC..'&nvalue=0&svalue='..IDC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_UAC..'&nvalue=0&svalue='..UAC)
domoticz.openURL('http://'..IPdomiticz..':8080/json.htm?type=command¶m=udevice&idx='..idx_IAC..'&nvalue=0&svalue='..IAC)
end
end
end
}
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.