dzVents script for PVOutput.

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

Moderator: leecollings

Post Reply
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

dzVents script for PVOutput.

Post by MikeyMan »

Hi guys,

me again with some messy tries to get a script running.
I'm currently trying to get this script to work:

Code: Select all

--[[
        dzVents version of pvOutput script.
pvOutput.lua downloaded van https://www.huizebruin.nl/domoticz/pvoutput-systeem-koppelen-aan-domoticz-v2020-1/
Bron : https://domoticz.com/forum/viewtopic.php?f=61&t=4714&start=100

Om een functie aan of uit te zetten haal of plaats de 2 streepjes voor de regel weg.

        api-key and id stored in domoticz uservariables:

            PVoutput_API     
           PVSettings.api

        both as type string

        v1 - energy generated
        v2 - power generated            W from 
        v3 - energy consumption
        v4 - power consumption
        v5 - temperature
        v6 - voltage

        c1 - Cumulatieve vlag 1 Zowel v1- als v3-waarden zijn levenslange energiewaarden.
Het verbruik en de opgewekte energie worden aan het begin van de dag op 0 gezet. 
2 Alleen v1 gegenereerd is een levenslange energiewaarde. 
3 Alleen v3-verbruik is een levenslange energiewaarde.

        
        n  - Net flag          Indien ingesteld op 1, geeft dit aan dat de vermogenswaarden netto export / import zijn.
                                in plaats van bruto gegenereerd / verbruik. 
            Deze optie wordt gebruikt voor apparaten die dat wel zijn niet in staat om bruto consumptiegegevens te rapporteren. 
            De verstrekte import- / exportgegevens worden samengevoegd met bestaande gegenereerde gegevens om het verbruik af te leiden.

        Donation mode only parms
        ========================
        '&delay=' .. Delay
        '&v7=' .. WaterConsumption
        '&v8=' .. InverterFrequency
        '&v11=' .. InverterTemp
        '&v12=' .. GasConsumption

]]

local scriptVar = 'PVOutput'

return 
{
    on =    
    { 
        timer = 
        { 
            'every 1 minutes', --om de 10 minuten is voor mij voldoende
        },
        httpResponses = 
        { 
            scriptVar,
        },
    },
 
    logging =    
    {   
        level = domoticz.LOG_DEBUG, -- change to LOG_ERROR when OK - was LOG_DEBUG
        marker = scriptVar,
    },

    execute = function(domoticz, dz, device, item, timer)

        local function post2PVOutput(PVSettings, postData)
            domoticz.openURL({
                url = PVSettings.url,
                method = 'POST',
                headers = {
                    ['X-Pvoutput-Apikey'] = PVSettings.api,
                    ['X-Pvoutput-SystemId'] = PVSettings.id
                },
                callback = scriptVar,
                postData = postData
            })
        end

        local function makepostData()
        local P1 = domoticz.devices(14).usage                  -- P1-Slimme meter\
        local generated = domoticz.devices(649)           -- Uitvoer van S0 meter omvormer\
  --      local generated = dz.devices('Youless').counterDeliveredToday
  --    local consumed = dz.devices('Consumption')          -- Verbruik virtueel apparaat \
        local temperature = domoticz.devices(49)       -- Temperatuur sensor \
  --    local voltageDevice = dz.devices('uac1')            --  Voltage meting van \
        local round = domoticz.utils.round

       --    local voltageString = voltageDevice.sValue
       --    local voltage = round(tonumber(voltageString:match('%d*%.*%d*')),1) -- To prevent error if 'V' is part of the string
                        
--          domoticz.log('P1         : ' .. P1.sValue,domoticz.LOG_FORCE)
        --  dz.log('generated  : ' .. generated.nValue .. ';' .. generated.sValue,dz.LOG_DEBUG)
        --  dz.log('consumed   : ' .. consumed.nValue .. ';' .. consumed.sValue,dz.LOG_DEBUG)
            domoticz.log('Temperature: ' .. temperature.temperature,domoticz.LOG_DEBUG)
        --   dz.log('voltage    : ' .. voltage,dz.LOG_DEBUG)
        --       domoticz.log('P1: '..P1)
            domoticz.log('generated: '..generated)
            domoticz.log('temperature: '..temperature)
            
            local postdDataAsString = 
                    'd=' .. os.date("%Y%m%d") ..
                    '&t=' .. os.date("%H:%M") .. 
                    
                    -- Gebruik deze als u netto productie van zonnepanelen wilt
             --       '&v1=' .. round(generated.WhTotal,1) ..  -- produced
             --       '&v2=' .. round(generated.actualWatt,1) ..
                    
                    -- Gebruik deze als u wilt dat je de gegevens van je slimme meter wilt gebruiken voor teruggave
                    -- '&v1=' .. P1.return1 + P1.return2 ..  -- returned to the grid
                    -- '&v2=' .. P1.usageDelivered ..
                    
                    -- Gebruik deze als u wilt dat je de gegevens van je slimme meter wilt gebruiken voor gebruik
                     '&v3=' .. P1.usage1 + P1.usage2 .. -- net values from your smart meter
                    -- '&v4=' .. P1.usage ..
                    
                    -- Gebruik deze als je wilt wat er wordt berekend
                   -- '&v3=' .. round(consumed.WhTotal,1) .. -- consumed 
                   -- '&v4=' .. round(consumed.actualWatt,1) ..
                       
                   -- Gebruik deze als je temperatuur en je voltage wilt uploaden
                    '&v5=' .. round(temperature.temperature,1) .. 
                   -- '&v6=' .. voltage ..

                    '&c1=1'

            return postdDataAsString
        end

        --hieronder hoef je niets te wijzigen

        if item.isHTTPResponse then
            domoticz.log("Return from PVOutput ==>> " .. item.data,domoticz.LOG_FORCE)
        else
            PVSettings = 
            {
            url = 'HTTPS://pvoutput.org/service/r2/addstatus.jsp',
            api = domoticz.variables('PVoutput_API').value,
            id  = domoticz.variables('PVoutput_ID').value,
            }
            post2PVOutput(PVSettings, makepostData())
        end
    end
}
After several errors, which i think are now fiex, i'm now getting one i cant seem to fix...

Code: Select all

2023-03-27 13:45:00.201 Error: dzVents: Error: (3.1.8) PVOutput: An error occurred when calling event handler PVOutput
2023-03-27 13:45:00.201 Error: dzVents: Error: (3.1.8) PVOutput: .../domoticz/scripts/dzVents/generated_scripts/PVOutput.lua:132: attempt to index a nil value (local 'item')
Any clue what's going wrong here? Somehow the logging isn't showing up too much either.
User avatar
waltervl
Posts: 6691
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script for PVOutput.

Post by waltervl »

It would help if you indicated what linenr 132 is where variable item is used.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents script for PVOutput.

Post by MikeyMan »

Sorry, expected the linenumbers to be there with the code tag.

Its the following:
if item.isHTTPResponse then

Line 132 till end:

Code: Select all

        if item.isHTTPResponse then
            domoticz.log("Return from PVOutput ==>> " .. item.data,domoticz.LOG_DEBUG)
        else
            PVSettings = 
            {
            url = 'HTTPS://pvoutput.org/service/r2/addstatus.jsp',
            api = domoticz.variables('PVoutput_API').value,
            id  = domoticz.variables('PVoutput_ID').value,
            }
            post2PVOutput(PVSettings, makepostData())
        end
    end
}
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents script for PVOutput.

Post by MikeyMan »

Im now thinking its the following line going wrong:

execute = function(domoticz, dz, device, item, timer)
User avatar
waltervl
Posts: 6691
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents script for PVOutput.

Post by waltervl »

You could try it with only item eg

execute = function(domoticz, item)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents script for PVOutput.

Post by MikeyMan »

waltervl wrote: Monday 27 March 2023 15:56 You could try it with only item eg

execute = function(domoticz, item)
Yup, that was part of the solution. Only two items can be placed there.
MikeyMan
Posts: 237
Joined: Wednesday 17 October 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents script for PVOutput.

Post by MikeyMan »

In case anyone is looking for a similar solution... This is my script:

Code: Select all

local scriptVar = 'PVOutput'
 
return {
  on = {
    timer = {
      'every 5 minutes',
    },
    httpResponses = {
      scriptVar,
    },
  },

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

  execute = function(domoticz, item)

    local function post2PVOutput(PVSettings, postData)
      domoticz.openURL({
        url = PVSettings.url,
        method = 'POST',
        headers = {
          ['X-Pvoutput-Apikey'] = PVSettings.api,
          ['X-Pvoutput-SystemId'] = PVSettings.id,
        },
        callback = scriptVar,
        postData = postData,
      })
    end

    local function makePostData()
      local generatedWhTotal = domoticz.devices(649).WhTotal -- Opgetelde uitvoer solaredge + goodwe
      local generatedActual = domoticz.devices(649).actualWatt
      local consumedWhTotal = domoticz.devices(655).WhTotal -- Verbruik virtueel apparaat
      local consumedActual = domoticz.devices(655).actualWatt
      local temperature = domoticz.devices(49).temperature -- Temperatuur sensor
      local round = domoticz.utils.round


      domoticz.log('PVOGeneratedActual: ' .. generatedActual, domoticz.LOG_DEBUG)
      domoticz.log('PVOConsumedWhTotal: ' .. consumedWhTotal, domoticz.LOG_DEBUG)
      domoticz.log('PVOConsumedActual: ' .. consumedActual, domoticz.LOG_DEBUG)
      domoticz.log('Temperature: ' .. temperature, domoticz.LOG_DEBUG)

      local postDataAsString =
        'd=' .. os.date('%Y%m%d') ..
        '&t=' .. os.date('%H:%M') ..
        '&v1=' .. round(generatedWhTotal, 1) .. -- produced
        '&v2=' .. round(generatedActual, 1) ..
        '&v3=' .. round(consumedWhTotal, 1) .. -- Consumption from script
        '&v4=' .. round(consumedActual, 1) .. -- Consumption from script
        '&v5=' .. round(temperature, 1) ..
        '&c1=1'

      return postDataAsString
    end

    -- hieronder hoef je niets te wijzigen

    if item.isHTTPResponse then
      domoticz.log('Return from PVOutput ==>> ' .. item.data, domoticz.LOG_DEBUG)

    else
      local PVSettings = {
        url = 'https://pvoutput.org/service/r2/addstatus.jsp',
        api = domoticz.variables('PVoutput_API').value,
        id = domoticz.variables('PVoutput_ID').value,
      }
      post2PVOutput(PVSettings, makePostData())
    end
  end,
}
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest