Page 1 of 1
scripts percentage solar panels
Posted: Monday 02 June 2025 10:23
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
Re: scripts percentage solar panels
Posted: Tuesday 03 June 2025 21:29
by habahabahaba
You have
Code: Select all
Percentage.updatePercentage(PercZonnepanelen)
What is "Percentage" ? You didnt define such device or variable
Re: scripts percentage solar panels
Posted: Wednesday 04 June 2025 8:45
by gvandick
Percentage is the "marker".
Re: scripts percentage solar panels
Posted: Wednesday 04 June 2025 10:38
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
}
Re: scripts percentage solar panels
Posted: Wednesday 04 June 2025 14:43
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
Re: scripts percentage solar panels
Posted: Thursday 05 June 2025 12:30
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 (" "):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
Re: scripts percentage solar panels
Posted: Thursday 05 June 2025 12:37
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.
Re: scripts percentage solar panels
Posted: Thursday 05 June 2025 13:09
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
}
Re: scripts percentage solar panels
Posted: Thursday 05 June 2025 13:54
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..
Re: scripts percentage solar panels
Posted: Thursday 05 June 2025 13:57
by waltervl
@RonkA there is more wrong, I also cleaned up the script and probably made it work in my previous post.
Re: scripts percentage solar panels
Posted: Friday 06 June 2025 8:57
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
Re: scripts percentage solar panels
Posted: Friday 06 June 2025 9:12
by gvandick

- Schermafbeelding 2025-06-06 090609.png (225.13 KiB) Viewed 67 times