Page 2 of 6
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Friday 02 March 2018 22:22
by McMelloW
Thanks, I will look at it.
Perhaps you are right. It is a bit of over kill every 10 sec for the purpose of the script. I am running on a Rpi 2. First I have to take some time to see how thing work out.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Friday 02 March 2018 23:26
by jake
Until your panels start producing, you should only see the 'lost energy' device increasing in value (if you enabled the setting empty_battery to 1 only).
For a headstart you could cheat the battery charge by manipulating the uservariable TotalEnergy, by simply ADDING (for instance) 4000 to the total value mentioned there. The script will think the next minute that you delivered this amount back to the grid (new P1 value - old P1 value = +/- -4000Wh = virtual battery charge)
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Saturday 03 March 2018 11:47
by McMelloW
jake wrote: ↑Friday 02 March 2018 23:26
Until your panels start producing, you should only see the 'lost energy' device increasing in value (if you enabled the setting empty_battery to 1 only).
For a headstart you could cheat the battery charge by manipulating the uservariable TotalEnergy, by simply ADDING (for instance) 4000 to the total value mentioned there. The script will think the next minute that you delivered this amount back to the grid (new P1 value - old P1 value = +/- -4000Wh = virtual battery charge)
I did the headstart suggestion last night. The figures are more realistic now. When you wrote this script, did you made a kind of flow chart or just started out of the blue with an idea in your head.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Saturday 03 March 2018 12:20
by jake
McMelloW wrote:jake wrote: ↑Friday 02 March 2018 23:26
Until your panels start producing, you should only see the 'lost energy' device increasing in value (if you enabled the setting empty_battery to 1 only).
For a headstart you could cheat the battery charge by manipulating the uservariable TotalEnergy, by simply ADDING (for instance) 4000 to the total value mentioned there. The script will think the next minute that you delivered this amount back to the grid (new P1 value - old P1 value = +/- -4000Wh = virtual battery charge)
I did the headstart suggestion last night. The figures are more realistic now. When you wrote this script, did you made a kind of flow chart or just started out of the blue with an idea in your head.
I have to confess that I started with just the idea in my head, after a discussion with a colleague about the subject. Programming is not my profession and I see it all the time that a simple idea becomes complex quite quickly, because at the start it is hard to see all inputs and disturbing factors.
However, with the print commands and the commentary in each section of the script, I have tried to visualize the steps that were necessary. The print commands acted as checks for me to see whether things were turning out as expected.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 11:14
by McMelloW
jake wrote: ↑Saturday 03 March 2018 12:20
I have to confess that I started with just the idea in my head, after a discussion with a colleague about the subject. Programming is not my profession and I see it all the time that a simple idea becomes complex quite quickly, because at the start it is hard to see all inputs and disturbing factors.
However, with the print commands and the commentary in each section of the script, I have tried to visualize the steps that were necessary. The print commands acted as checks for me to see whether things were turning out as expected.
Getting the idea of it, after some time. The script is running OK and without errors. I only had to change the device names/idx and create the user variables to make it run. Done a great job.
Only had some very odd figures in the Lost Solar Energy.
Code: Select all
2018-03-03 18:00:00 18446744073709552000
Shift + leftmouse click does not delete it. But this is Peanuts. I will keep you informed.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 12:08
by jake
Nice.
If there is a lower value on a counter that is only supposed to go up, it shows these weird numbers. I therefore check in the script before adding a new number.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 12:38
by clubeddie
Can somebody tell me where to find the option to add RFXMeter. This option is not available in my setup?
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 14:12
by McMelloW
clubeddie wrote: ↑Sunday 04 March 2018 12:38
Can somebody tell me where to find the option to add RFXMeter. This option is not available in my setup?
Create a dummy hardware (do nothing) On this new dummy hardware press the blue button an create a Counter (without incremental)
That's all. Read the manual on domoticz.com It is all there
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 14:39
by clubeddie
McMelloW wrote: ↑Sunday 04 March 2018 14:12
clubeddie wrote: ↑Sunday 04 March 2018 12:38
Can somebody tell me where to find the option to add RFXMeter. This option is not available in my setup?
Create a dummy hardware (doe nothing) On this new dummy hardware press the blue button an create a Counter (without incremental)
That's all. Reaad the manual on domoticz.com It is all there
Thank you
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 15:27
by McMelloW
jake wrote: ↑Sunday 04 March 2018 12:08
Nice.
If there is a lower value on a counter that is only supposed to go up, it shows these weird numbers. I therefore check in the script before adding a new number.
Your lua-script is translated to dzVents 2.4 At this moment it is running quit OK. I will compare the output for some days and see what happens.
Code: Select all
--dzVents script Solar_Battery - Created and run in Domoticz events
--Time based Script to simulate a battery to store harvested solar energy at times it is not consumed directly by the appliances in the house/office/factory
--3 Devices will be calculated:
-- (1): Virtual solar battery
-- (2): Energy counter to see how much energy is consumed from the solar battery. Are you using your battery capacity every day,
-- not only for charging, but also for emptying. At big energy consumption, the battery can be used 'multiple' times in a day,
-- when the energy harvesting < temporary big energy consumption
-- (3): Energy counter to see how much energy is lost with the given battery capacity. This is measured both for a full battery while still
-- harvesting energy as for an empty battery while there is energy consumption (optional, see settings variable)
--Manually create and/or define below (newly created virtual) devices and 'User Variables' by user given names
local P1data = 33 -- Electra, P1 Smart Meter device
-- To be created virtual devices in the hardware section of Domoticz:
local idxvsb = 79 -- (1) Custom Sensor name for the 'Virtual Solar Battery'
local idxsbu = 80 -- (2) RFXMeter counter name for the 'Solar Battery Usage'
local idxlse = 81 -- (3) RFXMeter counter name for the 'Lost Solar Energy'
-- User Variables:
local battery = 'SolarBatCap' -- User Variable name for the 'Solar Battery Capacity'. Type 'String'. Value in Wh (5kWh battery = '5000')
local tot_elec = 'Total_Elec' -- User Variable to store the total sum of P1 electricity meter. Type 'String'. Value initially must be '0'
--[[ Settings variable for the 'Lost Solar Energy' meter to include/exclude energy consumption while the battery is already empty:
1 = Include (in other words, with either more solar panels to fill the battery or a bigger battery capacity,
energy consumption from the grid could have been avoided)
0 = Exclude (in other words, the meter shows in the monthly graph exactly how big the battery should have been
to catch all generated solar energy)
]]--
local empty_bat = 1
-- Define variables, used in this script. Don't change!!!
local bat_cap
local new_elec
local old_elec
local elec_cons
local old_bat_value
local new_bat_value
local bat_usage
local old_bat_usage
local new_bat_usage
local lost_energy
local old_lost_energy
local new_lost_energy
local ScriptVersion = '0.0.7'
return {
logging = {
-- level = domoticz.LOG_DEBUG, -- Uncomment this line to override the dzVents global logging setting
marker = 'VSB '.. ScriptVersion
},
on = {
timer = { 'every minute' }
},
execute = function(domoticz, triggerItem)
if (triggerItem.isTimer) then
bat_cap = tonumber(domoticz.variables(battery).value)
-- Get values from device P1Data of the Smart Meter
local SMdata = domoticz.devices(P1data).rawData
-- Get P1 Smart Meter values
new_elec = tonumber(SMdata[1] + SMdata[2]) - (SMdata[3] + SMdata[4])
domoticz.log(SMdata, domoticz.LOG_DEBUG)
old_elec = tonumber(domoticz.variables(tot_elec).value)
domoticz.log('new electricity = '.. new_elec, domoticz.LOG_DEBUG)
-- Check the energy consumption since last update.
-- A positive number means electricity usage, na negative number means electricity generated
elec_cons = (new_elec - old_elec)
domoticz.log('Electrity Consumption = '.. elec_cons, domoticz.LOG_DEBUG)
domoticz.variables(tot_elec).set(new_elec)
-- Get the last value from the Virtual Solar Battery
local vsb = domoticz.devices(idxvsb).rawData
old_bat_value = tonumber(vsb[1] * 1000)
-- Calculate the new battery value by subtracting the electricity consumption from the previous battery value.
-- When consumption is positive, therefore it dischargess the battery.
-- If the energy consumption is negative, it means that the battery will be charged.
new_bat_value = (old_bat_value - elec_cons)
-- Check for battery limits > 0 or <= then battery capacity
if new_bat_value <= 0 then
new_bat_value = 0
domoticz.log('Battery is empty, energy taken fromn grid', domoticz.LOG_DEBUG)
elseif new_bat_value > bat_cap then
new_bat_value = bat_cap
domoticz.log('Battery is completly charged.', domoticz.LOG_DEBUG)
elseif new_bat_value < bat_cap then
domoticz.log('New battery level = '.. (new_bat_value / 1000), domoticz.LOG_DEBUG)
end
domoticz.devices(idxvsb).updateCustomSensor(new_bat_value / 1000)
-----------------------------------
-- Virtual Solar Battery Use
-----------------------------------
-- Count the energy used from the battery (meaning that the electricity consumption must be a positve number
if elec_cons > 0 then
bat_usage = (old_bat_value - new_bat_value)
else
bat_usage = 0
end
-- Download the previous value of the solar battery energy usage in Wh
local sbu = domoticz.devices(idxsbu).rawData
old_bat_usage = tonumber(sbu[1])
domoticz.log('Old Solar Battery Usage = '.. old_bat_usage, domoticz.LOG_DEBUG)
-- Calculate the new value for the solar battery energy usage in Wh
new_bat_usage = (old_bat_usage + bat_usage)
if new_bat_usage >= old_bat_usage then
domoticz.devices(idxsbu).updateCounter(new_bat_usage)
domoticz.log('New Solar Battery Usage = '.. new_bat_usage, domoticz.LOG_DEBUG)
else
domoticz.log('Something went wrong, the new Solar Battery Usage value is smaller than the previous value', domoticz.LOG_DEBUG)
end
-----------------------------------
-- Virtual Lost Solar Energy calculation
-----------------------------------
-- Energy value above the battery capacity and at empty battery is 'lost'
-- and needs to be measured to know what capacity should have been chosen
-- Download the previous value of the virtual lost energy sensor
local lse = domoticz.devices(idxlse).rawData
old_lost_energy = tonumber(lse[1])
domoticz.log('Old Lost Solar Energy = '.. old_lost_energy, domoticz.LOG_DEBUG)
-- Calculate how much energy is lost after the battery reached it's maximum capacity
lost_energy = old_bat_value - elec_cons - bat_cap
if lost_energy > 0 then -- meaning a negative electricity_consumption, meaning energy generated while battery was at max. capacity
-- Calculate the new value for the virtual lost energy sensor
new_lost_energy = (old_lost_energy + lost_energy)
elseif (old_bat_value - elec_cons) < 0 and empty_bat == 1 then --meaning that there is energy consumption while battery is empty
--Calculate the new value for the virtual lost energy sensor
new_lost_energy = (old_lost_energy - (old_bat_value - elec_cons))
-- battery value would have been negative, therefore subtracted ( minus x minus = plus) to come to a total lost_energy
else
new_lost_energy = old_lost_energy
end
--Virtual counter new value must be higher than the old value to prevent errors in the virtual counter calculation
if new_lost_energy >= old_lost_energy then
domoticz.devices(idxlse).updateCounter(new_lost_energy)
domoticz.log('New Lost Solar Energy = '.. new_lost_energy, domoticz.LOG_DEBUG)
else
domoticz.log('Something went wrong, the new Lost Solar Energy value of is smaller than the previous value', domoticz.LOG_DEBUG)
end
end -- triggerItem
end -- execute
}
Comments and improvements on the script are welcome
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 20:49
by jake
Thanks for starting the dzVents version.
There are improvements to be made, but it is a good start. I will look into it in the next days.
2 things that attracted my attention: I had thought to remain using the battery capacity variable to enable easy adaptation through the domoticz GUI, but for the electricity meter a dzVents variable.
For checking device data, it is better to use the dzVents way of handling them. Easier to understand and manipulate.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Sunday 04 March 2018 22:05
by McMelloW
jake wrote: ↑Sunday 04 March 2018 20:49
Thanks for starting the dzVents version.
There are improvements to be made, but it is a good start. I will look into it in the next days.
2 things that attracted my attention: I had thought to remain using the battery capacity variable to enable easy adaptation through the domoticz GUI, but for the electricity meter a dzVents variable.
If you mean by "the electricity meter" the variable "TotalElectricity" I don't know how to keep a value outside a dzVents script. The user variable works OK en it is easy to read and to update domoticz.variables(var_name)
For checking device data, it is better to use the dzVents way of handling them. Easier to understand and manipulate.
I am lua a beginner, but not in script programming. dzVents is much more readable and easier. Although you have to learn and understand the way of thinking with in dzVent
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Monday 05 March 2018 16:09
by McMelloW
Discovered some errors in yesterdays script (04-03-2018) Placed the new code in the posting of yesterday
Re: Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Thursday 08 March 2018 10:54
by McMelloW
With a little dzVent script, all 6 meter values of the P1 Smart Meter are showed on different devices and the Virtual Solar Baterry as well.
Here is the dzVents script how the values are parsed from the P1 Smart Meter device. In due time I will combine this script with the Virtual Solar Battery script and run it all in one.
Code: Select all
--[[ dzVents script to Parse P1 Smart Meter Electricity value into seperated Meter Readings.
]]-- Creat the following virtual devices for your environment. Get the 'Idx' or 'Name' of the Device tab.
local P1data = 33 -- Electra, P1 Smart Meter device
local idxu1 = 42 -- Meter Usage low, Virtual device, counter incremental
local idxu2 = 43 -- Meter Usage High, Virtual device, counter incremental
local idxr1 = 44 -- Meter Return Low, Virtual device, counter incremental
local idxr2 = 45 -- Meter Return High, Virtual device, counter incremental
local idxcons = 74 -- Meter Actual Usage, Virtual device, counter incremental
local idxprod = 75 -- Meter Actual Production, Virtual device, counter incremental
local ScriptVersion = '0.1.7'
return {
logging = {
level = domoticz.LOG_DEBUG, -- Uncomment this line to override the dzVents global logging setting
marker = 'SME '.. ScriptVersion
},
on = {
timer = { 'every minute' }
},
execute = function(domoticz, triggerItem)
if (triggerItem.isTimer) then
-- Get values from device P1Data of the Smart Meter
local SMdata = domoticz.devices(P1data).rawData
-- Update the device and Debug meassages with the accessory values from table SMdata
domoticz.devices(idxu1).updateCounter(SMdata[1])
domoticz.log('Gebruik laag = '.. SMdata[1], domoticz.LOG_DEBUG)
domoticz.devices(idxu2).updateCounter(SMdata[2])
domoticz.log('Gebruik hoog = '.. SMdata[2], domoticz.LOG_DEBUG)
domoticz.devices(idxr1).updateCounter(SMdata[3])
domoticz.log('Levering laag = '.. SMdata[3], domoticz.LOG_DEBUG)
domoticz.devices(idxr2).updateCounter(SMdata[4])
domoticz.log('Levering hoog = '.. SMdata[4], domoticz.LOG_DEBUG)
domoticz.devices(idxcons).updateCounter(SMdata[5])
domoticz.log('Actuele Gebruik = '.. SMdata[5], domoticz.LOG_DEBUG)
domoticz.devices(idxprod).updateCounter(SMdata[6])
domoticz.log('Actuele Levering = '.. SMdata[6], domoticz.LOG_DEBUG)
end -- triggerItem
end -- execute
}
As always, comments and improvements are welcome
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Friday 13 July 2018 11:59
by gajotnt
Awesome idea :O
I dont have a P1 meter, but have 3 solar panels and a SDM120 MODBUS that gives me negative values when im sending energy to the grid.
Can you make a version of your script where it gets the information from the SDM120 dummy device (that has positive and negative values) and sends only the positive values to another Virtual Energy counter (that would be the "Used Energy") and another Virtual Energy counter that would receive the negative values convert them to positive and would be the "Wasted Energy".
That way one could better understand how to use 100% of the generated energy.
I think it would be an simpler version of your script, been trying to figure out how to adapt it to my needs, but programming isnt my strong suit :/
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Friday 13 July 2018 18:34
by jake
gajotnt wrote:Awesome idea :O
I dont have a P1 meter, but have 3 solar panels and a SDM120 MODBUS that gives me negative values when im sending energy to the grid.
Can you make a version of your script where it gets the information from the SDM120 dummy device (that has positive and negative values) and sends only the positive values to another Virtual Energy counter (that would be the "Used Energy") and another Virtual Energy counter that would receive the negative values convert them to positive and would be the "Wasted Energy".
That way one could better understand how to use 100% of the generated energy.
I think it would be an simpler version of your script, been trying to figure out how to adapt it to my needs, but programming isnt my strong suit :/
Thanks for your interest in the script. I assume your dummy counter is a 'power' meter? An energy meter can only add up, as far as I know. Therefore it wouldn't allow negative values.
The principle of the script is based on energy, because that is what is stored in a battery.
Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Friday 13 July 2018 21:21
by gajotnt
It is a dummy usage meter (kWh)
Maybe you can take a look at this post were I explain what im looking for and have a partial working code:
viewtopic.php?f=61&t=24142#p186211

- Screenshot 2018-07-13 15.49.49.png (127.86 KiB) Viewed 3615 times
Re: Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Wednesday 28 November 2018 12:25
by megamarco83
McMelloW wrote: ↑Thursday 08 March 2018 10:54
With a little dzVent script, all 6 meter values of the P1 Smart Meter are showed on different devices and the Virtual Solar Baterry as well.
Here is the dzVents script how the values are parsed from the P1 Smart Meter device. In due time I will combine this script with the Virtual Solar Battery script and run it all in one.
hi, thanks for the script.
i have a photovoltaic and i'm interested on evaluate adding solar battery, so i'm very interested on it!!
my system of monitoring production and consumption is working since 4 year but is a little different, like the picture:

- Immagine.jpg (239.95 KiB) Viewed 3096 times
i have this meter to see
consumption of the house (value indipendent from the photovoltaic production)
production of the photovoltaic
enery taken from the grid = calcolated as: ( consumption - production ) and reported only if value is > 0
energy give back to the grid = calcolated as: (production - consumption) and reported only if the value is > 0
energy self cunsumption = calcolated = production and reported only if (production-consumption) return vale > 0
is it possible to modify the script to monitoring a virtual battery and so calcolate a correct dimension of that one?
thanks
Re: Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Wednesday 28 November 2018 22:27
by jake
megamarco83 wrote: ↑Wednesday 28 November 2018 12:25
McMelloW wrote: ↑Thursday 08 March 2018 10:54
With a little dzVent script, all 6 meter values of the P1 Smart Meter are showed on different devices and the Virtual Solar Baterry as well.
Here is the dzVents script how the values are parsed from the P1 Smart Meter device. In due time I will combine this script with the Virtual Solar Battery script and run it all in one.
is it possible to modify the script to monitoring a virtual battery and so calculate a correct dimension of that one?
thanks
You've done most of the splitting of energy usage and generation already. All you need to do is using this part of the script where you start adding up the returned energy to the grid in an additional electricity virtual device and cap that at you chosen battery capacity.
Re: Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy
Posted: Thursday 29 November 2018 13:43
by megamarco83
jake wrote: ↑Wednesday 28 November 2018 22:27
You've done most of the splitting of energy usage and generation already. All you need to do is using this part of the script where you start adding up the returned energy to the grid in an additional electricity virtual device and cap that at you chosen battery capacity.
hi jake and thanks for answering, i'm a newbie so if you can help me, i need to be a little bit addressed

you are referring to your script lua, or for Dzvents ?
if i look at your lua script i see:
Local electricity_meter = 'Electricity' -- Existing P1 Electricity device name in 'Utility'
but here what i have to select from my configuration?
i have in my configuration already a counter "immissioni" (with live value and cumulated today value) that is related to energy returned to the grid (that could be only 0=no energy back to grid, or >0 if enery go back to grif, NEVER < 0)
if i take energy from the grid, i have another counter "prelievi" (with live value and cumulated today value) and if is =0 -> no asking energy to grid ; >0 asking energy to grid ; could never be <0
then i have the production
the consumption (real value of consumption without consindering the energy that i produce)
the self consumption (the amount of energy that i produce and use)
so how i have to change your script with my configuration?
thanks so much for help me!