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

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

Script to get data from Solar Invertor to textdevices in Domoticz

Post 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!
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

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

Post 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.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

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

Post by Freakandel »

Thanks Waaren, again, for your input. I will try this out today!
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

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

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

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

Post 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!
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

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

Post 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?
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

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

Post by Freakandel »

PS. you shure can call me a noob, I am so I will not be offended ;-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Freakandel
Posts: 24
Joined: Tuesday 18 June 2019 14:23
Target OS: Linux
Domoticz version:
Location: Netherlands, Breda region
Contact:

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

Post 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!
User avatar
psubiaco
Posts: 194
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

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

Post 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, ....
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
Post Reply

Who is online

Users browsing this forum: TiXav and 0 guests