LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Friday 02 March 2018 23:22

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.
Greetings McMelloW

jake
Posts: 484
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by jake » Saturday 03 March 2018 0: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)

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Saturday 03 March 2018 12:47

jake wrote:
Saturday 03 March 2018 0: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.
Greetings McMelloW

jake
Posts: 484
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by jake » Saturday 03 March 2018 13:20


McMelloW wrote:
jake wrote:
Saturday 03 March 2018 0: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.

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Sunday 04 March 2018 12:14

jake wrote:
Saturday 03 March 2018 13: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.
Greetings McMelloW

jake
Posts: 484
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by jake » Sunday 04 March 2018 13: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.

clubeddie
Posts: 79
Joined: Saturday 19 March 2016 22:12
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by clubeddie » Sunday 04 March 2018 13:38

Can somebody tell me where to find the option to add RFXMeter. This option is not available in my setup?

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Sunday 04 March 2018 15:12

clubeddie wrote:
Sunday 04 March 2018 13: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
Last edited by McMelloW on Sunday 04 March 2018 15:51, edited 2 times in total.
Greetings McMelloW

clubeddie
Posts: 79
Joined: Saturday 19 March 2016 22:12
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by clubeddie » Sunday 04 March 2018 15:39

McMelloW wrote:
Sunday 04 March 2018 15:12
clubeddie wrote:
Sunday 04 March 2018 13: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

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Sunday 04 March 2018 16:27

jake wrote:
Sunday 04 March 2018 13: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
Last edited by McMelloW on Monday 05 March 2018 17:06, edited 1 time in total.
Greetings McMelloW

jake
Posts: 484
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by jake » Sunday 04 March 2018 21: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.
For checking device data, it is better to use the dzVents way of handling them. Easier to understand and manipulate.

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Sunday 04 March 2018 23:05

jake wrote:
Sunday 04 March 2018 21: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
Greetings McMelloW

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Monday 05 March 2018 17:09

Discovered some errors in yesterdays script (04-03-2018) Placed the new code in the posting of yesterday
Greetings McMelloW

User avatar
McMelloW
Posts: 195
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by McMelloW » Thursday 08 March 2018 11: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.
Image

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
Greetings McMelloW

gajotnt
Posts: 68
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: Azores
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by gajotnt » Friday 13 July 2018 11:59

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 :/

jake
Posts: 484
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by jake » Friday 13 July 2018 18:34

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.

gajotnt
Posts: 68
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: Azores
Contact:

Re: LUA – Virtual Solar Battery: simulate the presence of a battery for storing harvested solar energy

Post by gajotnt » Friday 13 July 2018 21:21

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
Screenshot 2018-07-13 15.49.49.png (127.86 KiB) Viewed 506 times

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest