Page 1 of 1

Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Sunday 15 November 2020 20:13
by Freakandel
Hi all,

I would like to get actual data from our Solar Invertor (Fronius) in Domoticz, so I can use this data for further automation of things. I found on this Forum a Python plugin but that is not working with my model invertor. It looks like it should be not too hard to get it working in dzVents.

The URL for connecting to the Fronius Inverter is in my case:
http://192.168.1.60/solar_api/v1/GetInv ... verterData

The output is as following:

Code: Select all

{
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 710.10000000000002
         },
         "DeviceStatus" : {
            "ErrorCode" : 522,
            "LEDColor" : 2,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : -1,
            "StateToReset" : true,
            "StatusCode" : 7
         },
         "FAC" : {
            "Unit" : "Hz",
            "Value" : 49.960000000000001
         },
         "IAC" : {
            "Unit" : "A",
            "Value" : 0.23999999999999999
         },
         "IDC" : {
            "Unit" : "A",
            "Value" : 0.52000000000000002
         },
         "PAC" : {
            "Unit" : "W",
            "Value" : 33
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 710
         },
         "UAC" : {
            "Unit" : "V",
            "Value" : 225.90000000000001
         },
         "UDC" : {
            "Unit" : "V",
            "Value" : 0.5
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 710.10000000000002
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CommonInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2020-11-15T14:20:52+01:00"
   }
}
It would be nice to get the Day Energy, PAC, Total Energy and Year Energy in Domoticz (text?) devices for further analysis and automation.

Can someone help me with a bit of scripting? Many thanks!

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Sunday 15 November 2020 21:09
by Toulon7559
As intermediary for information from Solar inverters Domoticz has nice Virtual Devices for Energy & Power:
why not use those Virtual Devices?
Just look into the examples in this Forum how to fill them, how to read them and how to apply for control functions.

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Sunday 15 November 2020 21:51
by waaren
Freakandel wrote: Sunday 15 November 2020 20:13 I would like to get actual data from our Solar Invertor (Fronius) in Domoticz,
It would be nice to get the Day Energy, PAC, Total Energy and Year Energy in Domoticz (text?) devices for further analysis and automation.
Could look like below script

Code: Select all

--[[    

         Fronius.lua

        Script to collect information from a Fronius solar inverter and store it in a virtual (text) sensor

        History:
        20201115 initial release

]]--

local scriptVar = 'Fronius'

return
{
    on =
    {
        timer =
        {
            'every minute', -- set to the required frequency
        },

        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        local textDevice = dz.devices('Fronius')

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://192.168.1.60/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data
            local text = ''

            text = text .. 'Current power: ' .. (rt.PAC.Value or '-') .. ' W\n'
            text = text .. 'Day energy: ' .. ( rt.DAY_ENERGY.Value or '-' )   .. ' Wh\n'
            text = text .. 'Total energy: ' .. (rt.TOTAL_ENERGY.Value or '-' ) .. ' Wh\n'
            text = text .. 'Year energy: ' .. (rt.YEAR_ENERGY.Value or '-' ) .. ' Wh'

            dz.log('\n' .. text,dz.LOG_DEBUG)
            textDevice.updateText(text)
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 10:55
by Freakandel
Thanks Waaren, again, for your input. I will try this out today!

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 11:07
by Freakandel
@Waaren: the script is working and the readings from Fronius are in the Dummy Sensor in Domoticz now. I would like to get 4 different dummy sensors (energy based, so there is graphics behind the sensor) in stead of text for all values in one dummy device. Is this much effort to change in the script?

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 12:22
by waaren
@Freakandel

It is not much effort to make these changes in the script. Please have a look at the dzVents wiki and examples and give it a try.
When you got stuck or have a question please feel free to ask.

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 14:43
by Freakandel
Hi Waaren, I will give it a try, but your script looks already to complicated for me to get my head around ;-). I will get into the documents and hope I can figure it out. Thanks for your effort!

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 21:56
by Freakandel
- keep loosing my post here, there looks something broken in the editor on this forum -

I think I'm on the right track but keep getting errormessages in Domoticz. I've used your script and changed the lower part of it to get the values from the Fronium in the 4 'energy' dummy devices. It seems however that the Fronius changes the output in the API when there is no sunlight: the PAC value is no longer in the json output. In that case, the value of the switch should be 0 (zero).

I can't copy the script in this Quick Reply editor, but the normal editor is not working, at least for me. This is my current version:

local scriptVar = 'Fronius'

return
{
on =
{
timer =
{
'every minute', -- set to the required frequency
},

httpResponses =
{
scriptVar,
},
},

logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},

execute = function(dz, item)
local textDevice = dz.devices('Fronius')

if item.isTimer then
dz.openURL(
{
url = 'http://192.168.1.60/solar_api/v1/GetInv ... verterData',
callback = scriptVar,
})
return
end

if item.ok and item.isJSON then
local rt = item.json.Body.Data
--domoticz.devices('curSolEnergy').updateEnergy(rt.PAC.Value)
domoticz.devices('daySolEnergy').updateEnergy(rt.DAY_ENERGY.Value)
domoticz.devices('totalSolEnergy').updateEnergy(rt.TOTAL_ENERGY.Value)
domoticz.devices('yearSolEnergy').updateEnergy(rt.YEAR_ENERGY.Value)

-- dz.log('\n' .. text,dz.LOG_DEBUG)

else
dz.log('There was a problem handling the request', dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
end

end
}


This is the error I receive in the log of Domoticz:

2020-11-16 21:38:03.131 Error: dzVents: Error: (3.0.16) Fronius: An error occurred when calling event handler Script #2
2020-11-16 21:38:03.131 Error: dzVents: Error: (3.0.16) Fronius: ...domoticz/scripts/dzVents/generated_scripts/Script #2.lua:51: attempt to index a nil value (global

A little help please?

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 21:58
by Freakandel
PS. you shure can call me a noob, I am so I will not be offended ;-)

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Monday 16 November 2020 22:48
by waaren
Freakandel wrote: Monday 16 November 2020 21:56Fronius: ...domoticz/scripts/dzVents/generated_scripts/Script #2.lua:51:
You are already quite close. Main issue is that you used domoticz as name for the domoticz object while the script refers to it as dz

Can you try this one?

Code: Select all

local scriptVar = 'Fronius'

return
{
    on =
    {
        timer =
        {
            'every minute', -- set to the required frequency
        },

        httpResponses =
        {
        scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://192.168.1.60/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data
            dz.devices('curSolEnergy').updateEnergy( (rt.PAC and rt.PAC.Value ) or 0) -- if rt.PAC exists it will take its Value and if not it will be 0
            dz.devices('daySolEnergy').updateEnergy(rt.DAY_ENERGY.Value)
            dz.devices('totalSolEnergy').updateEnergy(rt.TOTAL_ENERGY.Value)
            dz.devices('yearSolEnergy').updateEnergy(rt.YEAR_ENERGY.Value)
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}


Re: Script to get data from Solar Invertor to textdevices in Domoticz  [Solved]

Posted: Tuesday 17 November 2020 9:24
by Freakandel
Hi Waaren, tested this script and get rid of a little typo in the URL, now it works like a charme! Thanks again for your help. I'm a bit closer to understanding how dzEvents works thanks to you!

Re: Script to get data from Solar Invertor to textdevices in Domoticz

Posted: Thursday 10 February 2022 18:39
by psubiaco
I've created a wiki page about Fronius Inverter: https://www.domoticz.com/wiki/Fronius_inverter
You can find there a simple LUA script that fetches data from the inverter, no need for any plugin.
Of course, anyone is able to modify the page to integrate plugins, other scripts, ....