scripts percentage solar panels

Moderator: leecollings

Post Reply
gvandick
Posts: 36
Joined: Saturday 01 July 2017 12:48
Target OS: Windows
Domoticz version:
Contact:

scripts percentage solar panels

Post by gvandick »

I try to make a script that calcutate the perc of own use generation kWh solar panels. Advandcely the goal is an overview of weekly and monthly report of % own use kWh solar panels. For now I use a excel file.

Therefore I made a dummy percentage. For measuring I use the smartmeter and a meter for generation kWh solar panels

To calculate the formule (P-I)/P)*100 where P = generation solar panels and I delevering (injection) to the net.

The script doesn't work and I don't know how to solve it. Maybe someone to help.

The log file domoticz

2025-06-02 10:11:45.074 Error: dzVents: Percentage: ...omoticz/scripts/dzVents/generated_scripts/Percentage.lua:98: attempt to index a nil value (global 'Percentage')
2025-06-02 10:11:50.098 Error: dzVents: Percentage: An error occurred when calling event handler Percentage
2025-06-02 10:11:50.098 Error: dzVents: Percentage: ...omoticz/scripts/dzVents/generated_scripts/Percentage.lua:98: attempt to index a nil value (global 'Percentage')
2025-06-02 10:11:55.077 Error: dzVents: Percentage: An error occurred when calling event handler Percentage
Attachments
Percentage Solar.lua.txt
(4.28 KiB) Downloaded 71 times
User avatar
habahabahaba
Posts: 233
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: scripts percentage solar panels

Post by habahabahaba »

You have

Code: Select all

Percentage.updatePercentage(PercZonnepanelen)
What is "Percentage" ? You didnt define such device or variable
gvandick
Posts: 36
Joined: Saturday 01 July 2017 12:48
Target OS: Windows
Domoticz version:
Contact:

Re: scripts percentage solar panels

Post by gvandick »

Percentage is the "marker".
HvdW
Posts: 605
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: scripts percentage solar panels

Post by HvdW »

In my opinion Domoticz excels in the way one can use python and dzVents to manipulate anything you want.
The thing I don mot like is that each item has it's own sensor which clutters the screen.
That is why I'm using textSensors where information can be displayed.

Here is a bit of script for your percentage using a textSensor to display.

Code: Select all

-- Helper functions
local function spaces(count)
    return (" "):rep(count)
end

local function round(num, decimals)
    local mult = 10^(decimals or 0)
    return math.floor(num * mult + 0.5) / mult
    end

local function formatPowerValue(value)
    return math.floor(value * 100 + 0.5) / 100
end

-- Energy calculations
local function calculateEigenGebruik(domoticz)
    local solar = domoticz.devices('Zonnepanelen').counterToday
    local returned = domoticz.devices('Power').counterDeliveredToday
    return round(solar - returned, 2)
end

local function getPowerUsageInfo(domoticz)
    local powerDevice = domoticz.devices('Power')
    local isUsing = tonumber(powerDevice.rawData[5]) > 0
    
    return {
        colorFlag = isUsing and "red" or "green",
        balance = formatPowerValue(isUsing and powerDevice.usage/1000 or -powerDevice.usage/1000),
        type = isUsing and 'usage' or 'return'
    }
end

-- Update functions
local function updateEnergyInfo(domoticz)
    local powerInfo = getPowerUsageInfo(domoticz)
    local eigenGebruik = calculateEigenGebruik(domoticz)
    local solarDevice = domoticz.devices('Zonnepanelen')
    local eigenGebruikPercentage = math.floor((eigenGebruik * 100) / solarDevice.counterToday)
    
    local text = string.format(
        'Gas today'..spaces(19)..'%s m³\n' ..
        'Gas yesterday'..spaces(11)..' %s m³\n' ..
        'Gas the day before'..spaces(3)..' %s m³\n\n\n' ..
        spaces(15)..'Solar actual'..spaces(16)..'%s <font color="green">kW</font>\n' ..
        spaces(15)..'Solar today'..spaces(17)..'%s kWh\n\n' ..
        spaces(15)..'Power actual %s'..spaces(2)..' %.2f <font color="%s">kW</font>\n' ..
        spaces(15)..'Power usage today'..spaces(4)..'%.2f <font color="red">kWh</font>\n' ..
        spaces(15)..'Power return today'..spaces(4)..'%.2f <font color="green">kWh</font>\n\n' ..
        spaces(15)..'Eigen gebruik'..spaces(14)..'%s kWh/%s%%\n',
        
        domoticz.devices('Gas').counterToday,
        domoticz.devices('Gas gisteren').sensorValue,
        domoticz.devices('Gas eergisteren').sensorValue,
        
        round(solarDevice.usage / 1000, 2),
        round(solarDevice.counterToday, 2),
        
        powerInfo.type, powerInfo.balance, powerInfo.colorFlag,
        round(domoticz.devices('Power').counterToday, 2),
        round(domoticz.devices('Power').counterDeliveredToday, 2),
        
        eigenGebruik,
        eigenGebruikPercentage
    )
    
    domoticz.devices('Energie').updateText(text)
end

-- Main script
return {
    on = {
        timer = {'every 1 minutes'},
    },
    
    logging = {
        level = domoticz.LOG_ERROR,
        marker = '---- Alles in text -----'
    },

    execute = function(domoticz, item)

            updateEnergyInfo(domoticz)
    end
}
Bugs bug me.
User avatar
RonkA
Posts: 106
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: scripts percentage solar panels

Post by RonkA »

Hi, i suspect you used https://wiki.domoticz.com/DzVents:_next ... Percentage to use as reference for your script,
but that is not how to use a dummy device in a script..
in dzVents you do something like this:

Code: Select all

local percentageDevice = domoticz.devices(IDX)
use the IDX of your dummydevice; now do your calculation and then:

Code: Select all

percentageDevice.updatePercentage(PercZonnepanelen)
For your reference: to avoid problems don't use names that can be needed for code-execution.
For example don't use 'percentage' as name, use 'xyz-percentage' or something like that
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
gvandick
Posts: 36
Joined: Saturday 01 July 2017 12:48
Target OS: Windows
Domoticz version:
Contact:

Re: scripts percentage solar panels

Post by gvandick »

HvdW wrote: Wednesday 04 June 2025 10:38 In my opinion Domoticz excels in the way one can use python and dzVents to manipulate anything you want.
The thing I don mot like is that each item has it's own sensor which clutters the screen.
That is why I'm using textSensors where information can be displayed.

Here is a bit of script for your percentage using a textSensor to display.

Code: Select all

-- Helper functions
local function spaces(count)
    return ("&nbsp;"):rep(count)
end

local function round(num, decimals)
    local mult = 10^(decimals or 0)
    return math.floor(num * mult + 0.5) / mult
    end

local function formatPowerValue(value)
    return math.floor(value * 100 + 0.5) / 100
end

-- Energy calculations
local function calculateEigenGebruik(domoticz)
    local solar = domoticz.devices('Zonnepanelen').counterToday
    local returned = domoticz.devices('Power').counterDeliveredToday
    return round(solar - returned, 2)
end

local function getPowerUsageInfo(domoticz)
    local powerDevice = domoticz.devices('Power')
    local isUsing = tonumber(powerDevice.rawData[5]) > 0
    
    return {
        colorFlag = isUsing and "red" or "green",
        balance = formatPowerValue(isUsing and powerDevice.usage/1000 or -powerDevice.usage/1000),
        type = isUsing and 'usage' or 'return'
    }
end

-- Update functions
local function updateEnergyInfo(domoticz)
    local powerInfo = getPowerUsageInfo(domoticz)
    local eigenGebruik = calculateEigenGebruik(domoticz)
    local solarDevice = domoticz.devices('Zonnepanelen')
    local eigenGebruikPercentage = math.floor((eigenGebruik * 100) / solarDevice.counterToday)
    
    local text = string.format(
        'Gas today'..spaces(19)..'%s m³\n' ..
        'Gas yesterday'..spaces(11)..' %s m³\n' ..
        'Gas the day before'..spaces(3)..' %s m³\n\n\n' ..
        spaces(15)..'Solar actual'..spaces(16)..'%s <font color="green">kW</font>\n' ..
        spaces(15)..'Solar today'..spaces(17)..'%s kWh\n\n' ..
        spaces(15)..'Power actual %s'..spaces(2)..' %.2f <font color="%s">kW</font>\n' ..
        spaces(15)..'Power usage today'..spaces(4)..'%.2f <font color="red">kWh</font>\n' ..
        spaces(15)..'Power return today'..spaces(4)..'%.2f <font color="green">kWh</font>\n\n' ..
        spaces(15)..'Eigen gebruik'..spaces(14)..'%s kWh/%s%%\n',
        
        domoticz.devices('Gas').counterToday,
        domoticz.devices('Gas gisteren').sensorValue,
        domoticz.devices('Gas eergisteren').sensorValue,
        
        round(solarDevice.usage / 1000, 2),
        round(solarDevice.counterToday, 2),
        
        powerInfo.type, powerInfo.balance, powerInfo.colorFlag,
        round(domoticz.devices('Power').counterToday, 2),
        round(domoticz.devices('Power').counterDeliveredToday, 2),
        
        eigenGebruik,
        eigenGebruikPercentage
    )
    
    domoticz.devices('Energie').updateText(text)
end

-- Main script
return {
    on = {
        timer = {'every 1 minutes'},
    },
    
    logging = {
        level = domoticz.LOG_ERROR,
        marker = '---- Alles in text -----'
    },

    execute = function(domoticz, item)

            updateEnergyInfo(domoticz)
    end
}
I installed the script and for nw I can follow daily own used Solar consumption kWh/%. But I like to follow a trend weekly and monthly
gvandick
Posts: 36
Joined: Saturday 01 July 2017 12:48
Target OS: Windows
Domoticz version:
Contact:

Re: scripts percentage solar panels

Post by gvandick »

RonkA wrote: Wednesday 04 June 2025 14:43 Hi, i suspect you used https://wiki.domoticz.com/DzVents:_next ... Percentage to use as reference for your script,
but that is not how to use a dummy device in a script..
in dzVents you do something like this:

Code: Select all

local percentageDevice = domoticz.devices(IDX)
use the IDX of your dummydevice; now do your calculation and then:

Code: Select all

percentageDevice.updatePercentage(PercZonnepanelen)
For your reference: to avoid problems don't use names that can be needed for code-execution.
For example don't use 'percentage' as name, use 'xyz-percentage' or something like that
Thanks now I understand the way to use update. The formule gives still nil. When filll in a value in the script Perczonnepanelen = 10, then the value upload to perc device. So it is a chalenge to make the formule working.
User avatar
waltervl
Posts: 5791
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: scripts percentage solar panels

Post by waltervl »

I updated your script. You mixed device attributes with value attributes. I created for the device the attribute "DevPercZonnepanelen"
Below script should work (not tested)

Code: Select all

-- This script collects the values below from Domoticz
--   * The Power and energy values (import and export) from a smartmeter 
--   * The Power and energy values from a Solar power inverter
-- It then calculates the consumed power and energy from the values above with the formula's
--   * EnergyConsumption = EnergyGeneration + EnergyImport - EnergyExport
--   * PowerConsumption = PowerGeneration + PowerImport - PowerExport
-- It then updates a virtual device which displays the % consumed energy Solar panels own use.
 

return {

    on = {
        devices = {
					-- Devices on which this calculation script should trigger. These should be the same as the IDX of Smartmeter and/or IDX of Solar Panels
                    171, -- IDX of Smart meter (youless or P1)
                    258, -- IDX of Solar Panels
                   -- 1081 -- IDX of Aandeel zonnepanelen
                }
        },

    logging = {
        level = domoticz.LOG_INFO,
      --  level = domoticz.LOG_ERROR,
        marker = "Percentage"
               },   

	execute = function(dz, item)

	   
		----------------------------------------------------------------------------------------------------------
		-- Domoticz IDX of the needed devices
		----------------------------------------------------------------------------------------------------------
   
        local Smartmeter = dz.devices(171)   -- IDX of Smart meter (youless or P1)
        local Generation = dz.devices(258)   -- IDX of Solar Panels
        local DevPercZonnepanelen = dz.devices(1081)  -- IDX of Percentage

        -- Smartmeter

        local EnergyExportLow = Smartmeter.rawData[3]
        local EnergyExportHigh = Smartmeter.rawData[4]

        -- Generation

        local EnergyGeneration = Generation.rawData[2]           -- (P) Productie zonnepalen
        local EnergyExport = EnergyExportLow + EnergyExportHigh  -- (I) Export naar electriciteitsnet

        local PercZonnepanelen = tonumber((( EnergyGeneration - EnergyExport))/EnergyGeneration) * 100
        dz.log('PercZonnepanelen: ' .. PercZonnepanelen, dz.LOG_INFO)
        
		--Update
	
        DevPercZonnepanelen.updatePercentage(PercZonnepanelen) -- Update device DevPercZonnepanelen with value PercZonnepanelen
    end
}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
RonkA
Posts: 106
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: scripts percentage solar panels

Post by RonkA »

First of all, the script you have in the textfile looks horrible! effectively this is wat your script is if you remove all clutter:

Code: Select all

return {
    on = {
        devices = { 171, 258 }
        },
    logging = {
        level = domoticz.LOG_INFO,
        marker = "Percentage"
               },   

    execute = function(dz, item)
        local Smartmeter = dz.devices(171)   		-- IDX of Smart meter (youless or P1)
        local Generation = dz.devices(258)			-- IDX of Solar Panels
        local PercZonnepanelen = dz.devices(1081)  	-- IDX of Percentage

        local EnergyExportLow = Smartmeter.rawData[3]
        local EnergyExportHigh = Smartmeter.rawData[4]
        local EnergyGeneration = Generation.rawData[2]           -- (P) Productie zonnepalen

        local EnergyExport = EnergyExportLow + EnergyExportHigh  -- (I) Export naar electriciteitsnet
        local PercZonnepanelen = tonumber((( EnergyGeneration - EnergyExport))/EnergyGeneration) * 100,

        Percentage.updatePercentage(PercZonnepanelen)
    end
}
As my understanding of rawdata is that is a string and not a number so you should use 'tonumber' on it so

Code: Select all

      local EnergyExportLow = Smartmeter.rawData[3]
and should become:

Code: Select all

      local EnergyExportLow = tonumber(Smartmeter.rawData[3])
so this should be done with all string data to get numbers to make calculations.
Then we see:

Code: Select all

        local PercZonnepanelen = tonumber((( EnergyGeneration - EnergyExport))/EnergyGeneration) * 100,
now the tonumber is not necessary but is no problem.. At the end of the line there is a comma, that shouldn't be there..
Last edited by RonkA on Thursday 05 June 2025 14:19, edited 2 times in total.
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
User avatar
waltervl
Posts: 5791
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: scripts percentage solar panels

Post by waltervl »

@RonkA there is more wrong, I also cleaned up the script and probably made it work in my previous post.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
gvandick
Posts: 36
Joined: Saturday 01 July 2017 12:48
Target OS: Windows
Domoticz version:
Contact:

Re: scripts percentage solar panels

Post by gvandick »

Hi every one, Thanks for all help.

I adjust the script "TextSensor" to % Sensor that works for me. I Used the textSensor en % Sensor devices adding to Energy dashboard. The script percentage solar panals is also working. For now I test if over a longer period .

The goal is to research the effect of the homebattery on the efficency own used generation Solar Panels. Till now the effficiency is approx 35%.
The sellers of homebattery promise up to 60%.

I will see if this is to realize
gvandick
Posts: 36
Joined: Saturday 01 July 2017 12:48
Target OS: Windows
Domoticz version:
Contact:

Re: scripts percentage solar panels

Post by gvandick »

Schermafbeelding 2025-06-06 090609.png
Schermafbeelding 2025-06-06 090609.png (225.13 KiB) Viewed 77 times
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest