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

Moderator: leecollings

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

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

Post by jake »

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

Background
With new legislation in The Netherlands, starting from 2020, it is likely that solar panel owners in the Netherlands will receive less money for energy that they feed into the electricity grid then what they pay for energy that they retrieve from that same electricity grid.

Currently, solar panel owners can use the electricity grid as one big solar battery with a 100% efficiency. The value of the ‘generate’ meter will be deducted from the ‘usage’ meter, meaning that both meters are considered to be equal in value per kWh. However, the biggest portion of the kWh price is (energy) taxes, but also partially usage of the electricity grid. Considering the value of generated and used electricity equal in value can be considered as subsidies. Unfortunately for solar panel owners the Dutch government wants to use those subsidies to develop other opportunities for clean energy, since solar energy is considered to be mature by now.

Since this forum is not meant for political topics, but for technical solutions, let’s move on to counter measures, how to optimize our systems for maximum financial / electrical profit. With a solar system it is inevitable that generation and usage of electricity is not balanced. A battery, like for instance the Tesla Powerwall, will be a good solution to store the surplus of generated energy locally ‘behind the meter’. There where the solar panel owner is in charge.

The question is, what size battery should I buy? It is not recommended to be too small, it will be full and empty much too fast. Not too big either, it doesn’t make sense to fill the battery on a sunny summer day to the top, use it for your nightly consumption and find the battery the next morning still half full, leaving only half the battery available for charging on day 2. Ideally the battery tips the ‘full’ and the ‘empty’ point each day without overshoot in both directions.

Technical
Well, Domoticz can be used for making the right choice. With 3 virtual devices the presence of a solar battery behind the meter can be simulated. It is up to the user to tweak the capacity of the virtual battery to find the sweet spot for max use of the battery through the year. In my case I started with a 4000 Wh virtual battery. Already in the spring I saw that the battery reached ‘full’ and ‘empty’ point on a lot of days in a row. I therefore increased the battery (for free ;-) ) to 5000Wh, in order to store more solar energy and consume from it as well. In my case the 5000 Wh battery works much better than the 4000Wh one. In the attached graph it can be seen that the ‘purchase’ of the additional 1000 Wh was done in the middle of August of this year.

The script doesn’t take into consideration the cost/reward for a bigger/small battery. The meter logs can be exported to Excel for more precise financial calculation.

Update:
02-04-2021: DzVents version of the script, with additional functionality, can be found on GitHub!
19-04-2021: Version 0.2 released with better registration of all elements that impact maximum efficiency of solar battery usage.

DEPRECIATED INFORMATION ABOUT THE OLD LUA SCRIPT CAN BE FOUND HERE BELOW
Basis for the solar battery script:
The script expects a Domoticz kWh meter, like the P1 smart meter, that outputs a total electricity value, the Wh meter value becomes higher when energy consumed and lower when energy is fed back into the electricity grid.

Code: Select all

--Script_time_solar_battery.lua

--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 electricity_meter = 'Electricity'					-- Existing P1 Electricity device name in 'Utility'

--To be created virtual devices in the hardware section of Domoticz:
local solar_battery = 'Virtual Solar Battery'				-- (1) Custom Sensor name for the 'Virtual Solar Battery'
local solar_battery_idx = 189
local battery_use = 'Solar Battery Usage'				-- (2) RFXMeter counter name for the 'Solar Battery Usage'
local battery_use_idx = 195
local lost_energy = 'Lost Solar Energy'					-- (3) RFXMeter counter name for the 'Lost Solar Energy'
local lost_energy_idx = 194

--User Variables:
local battery_capacitylevel = 'SolarBatteryCapacity'		-- User Variable name for the 'Solar Battery Capacity'. Type 'String'. Value in Wh (4kWh battery = '4000')
local total_electricity = 'TotalElectricity'				-- 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:
local empty_battery = 1							-- 1 =	Include (in other words, with either more solar panels to fill the battery or a 
											--	bigger 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)

--Define variables, used in this script. Don't change!!!
local battery_capacity
local old_total_electricity
local new_total_electricity
local electricity_consumption
local old_battery_value
local new_battery_value
local lost_energy_value
local old_lost_energy_value
local new_lost_energy_value
local battery_usage
local old_battery_usage
local new_battery_usage

commandArray = {} 

	battery_capacity = tonumber (uservariables[battery_capacitylevel])
	if tonumber (uservariables[battery_capacitylevel]) == nil then
		battery_capacity = 2000
		print ("Battery capacity in User Variables was not a number. Capacity will be set to 2000Wh")
		commandArray ['Variable:'.. battery_capacitylevel] = tostring(battery_capacity)
	else
		battery_capacity = tonumber (uservariables[battery_capacitylevel])
	end

	sPlusT1, sPlusT2, sMinT1, sMinT2 = otherdevices_svalues[electricity_meter]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
		sPlusT1 = tonumber(sPlusT1); 	-- energy usage, low tarrif
	sPlusT2 = tonumber(sPlusT2);	-- energy usage, high tarrif
	sMinT1 = tonumber(sMinT1);		-- energy generated, low tarrif
	sMinT2 = tonumber(sMinT2);		-- energy generated, high tarrif
	--print (sPlusT1 .." " .. sPlusT2 .." " .. sMinT1 .." " .. sMinT2)
	--Calculate the new total meter value in Wh (both incoming meters minus both outgoing meters)
	new_total_electricity = (sPlusT1 + sPlusT2) - (sMinT1 + sMinT2)
	--print ("new electricity level is " .. new_total_electricity)
	
	-- Download the previous total meter value from the variable to start the comparison
	if tonumber (uservariables[total_electricity]) == nil then
		old_total_electricity = new_total_electricity
		print (total_electricity .." in User Variables was not a number. Value will be filled with current electricity meter sum")
		commandArray ['Variable:'.. total_electricity] = tostring(new_total_electricity)
	else
		old_total_electricity = tonumber (uservariables[total_electricity])
	end
	--print ("old electricity level is " .. old_total_electricity)
	--Check the energy usage since last update. A positive number means electricity usage, a negative number means electricity generated
	electricity_consumption = (new_total_electricity - old_total_electricity)
	--print ("consumption of electricity is " .. electricity_consumption .." Wh")
	--Write the new energy value to the variable
	commandArray ['Variable:'.. total_electricity] = tostring(new_total_electricity)
	--print ("variable TotalElectricity updated to "..tostring(new_total_electricity))
	--Check if there is a valid value for the virtual solar battery level, if not (like after inital start of the virtual sensor) create a '0'
	if tonumber(otherdevices_svalues[solar_battery]) == nil then
		old_battery_value = 0
		print ("old battery value was NIL")
	else
		--Download the previous value of the virtual solar battery
		old_battery_value = tonumber(otherdevices_svalues[solar_battery])*1000
	end
	--Calculate the new battery value by subtracting the electricity consumption from the previous battery value. 
	--When consumption is positive, it therefore depletes the battery. If the energy consumption is negative, it means that the battery will be charged.
	new_battery_value = old_battery_value - electricity_consumption
	--print ('new battery level is old battery level of '.. old_battery_value .." Wh minus electricity consumption of ".. electricity_consumption.. " Wh = ".. new_battery_value .." Wh")
	
	--Battery can not be further depleted than 'empty' and therefore not < 0 Wh
	if new_battery_value > 0 then
		--Check if the new battery value will not be more than it's capacity
		if new_battery_value <= battery_capacity then
			--print ("Battery value updated to " .. new_battery_value .." Wh")
		-- Check if the new battery value will be higher than the battery capacity
		elseif new_battery_value > battery_capacity then
			--New battery value will be the maximum battery capacity
         new_battery_value = battery_capacity
			--print ("Solar battery maximum capacity of ".. battery_capacity .." Wh reached")
		end
	
   elseif new_battery_value <= 0 then
      new_battery_value = 0
		--print ("Solar battery is empty, energy is taken from the grid")
	end
   commandArray [1] = {['UpdateDevice'] = solar_battery_idx .. '|0|' .. new_battery_value/1000}
--end

-----------------------------------
--Virtual Solar Battery Use
-----------------------------------

--Count the energy used from the battery (meaning that the electricity consumption must be a positve number
if electricity_consumption > 0 then
   battery_usage = old_battery_value - new_battery_value
else
   battery_usage = 0
end

if tonumber(otherdevices_svalues[battery_use]) == nil then
	old_battery_usage = 0
	print (otherdevices_svalues[battery_use])
	print ("old battery energy usage value was NIL. Either it is a first start of the script (new virtual sensor), or something is wrong")
else
	--Download the previous value of the solar battery energy usage in Wh
	old_battery_usage = tonumber(otherdevices_svalues[battery_use])
end
--print ("Old value of the solar battery energy usage is " .. old_battery_usage .. "Wh")

--Calculate the new value for the solar battery energy usage in Wh
new_battery_usage = old_battery_usage + battery_usage
--Virtual counter new value must be higher than the old value to prevent errors in the virtual counter calculation
--print ("New solar battery usage value of " ..new_battery_usage .. ' Wh = Old solar battery usage value of ' ..  old_battery_usage .. ' Wh + battery energy usage of ' ..  battery_usage .. ' Wh')
if new_battery_usage >= old_battery_usage then
	commandArray [2] = {['UpdateDevice'] = battery_use_idx .. '|0|' .. new_battery_usage }
	--print ("New battery usage information sent to sensor "..battery_use_idx.." with value of ".. new_battery_usage)
else
	print ("Something went wrong, the new solar battery usage value of "..new_battery_usage.." is smaller than the previous value of "..old_battery_usage)
end


-----------------------------------
--Virtual Solar Energy Loss 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

--Check if there is a valid value for the virtual lost energy level, if not (like after inital start of the virtual sensor) create a '0' value
if tonumber(otherdevices_svalues[lost_energy]) == nil then
	old_lost_energy_value = 0
	print ("old lost energy was NIL")
else
	--Download the previous value of the virtual lost energy sensor
	old_lost_energy_value = tonumber(otherdevices_svalues[lost_energy])
end

--print ("Old lost energy value is " .. old_lost_energy_value .. "Wh")

--Calculate how much energy is lost after the battery reached it's maximum capacity
lost_energy_value = old_battery_value - electricity_consumption - battery_capacity

if lost_energy_value > 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_value = old_lost_energy_value + lost_energy_value
	--print ("New lost energy of " ..new_lost_energy_value .. ' Wh = Old Lost Energy value of ' ..  old_lost_energy_value .. ' Wh + Lost Energy value of ' ..  lost_energy_value .. ' Wh')
elseif ((old_battery_value - electricity_consumption) < 0 and empty_battery == 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_value = old_lost_energy_value - (old_battery_value - electricity_consumption) --battery value would have been negative, therefore subtracted ( minus x minus = plus) to come to a total lost_energy
		--print ("New lost energy of " ..new_lost_energy_value .. ' Wh = Old Lost Energy value of ' ..  old_lost_energy_value .. ' Wh - Lost Energy value of negative battery level ' ..  (old_battery_value - electricity_consumption) .. ' Wh')
else
	new_lost_energy_value = old_lost_energy_value
end

--Virtual counter new value must be higher than the old value to prevent errors in the virtual counter calculation
if new_lost_energy_value >= old_lost_energy_value then
	commandArray['UpdateDevice'] = lost_energy_idx .. '|0|' .. new_lost_energy_value
else
	print ("Something went wrong, the new lost energy value of "..new_lost_energy_value.." is smaller than the previous value of "..old_lost_energy_value)
end
return commandArray
LUA.-Solar Battery devices.JPG
LUA.-Solar Battery devices.JPG (45.68 KiB) Viewed 5964 times
LUA.-Solar Battery graphs.JPG
LUA.-Solar Battery graphs.JPG (130.72 KiB) Viewed 5964 times
Last edited by jake on Monday 19 April 2021 14:14, edited 2 times in total.
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

@Jake
This looks great. I sure will dig into this.

For my understanding:
Solar Battery Usage = Energy produced by your Solarpanels measured at your inverter
Lost Solar Energy = Solar Energy delivered to the grid measured at your P1 Smart Meter

It also gives me also a brainwave for another kind of solution. I have a Solar hotwater boiler installed. The temperature is measured every minute and it is only heated by the sun-collector. I really would like to know the amount of energy the sun puts into my boiler. A virtual battery might do the trick.
Last edited by McMelloW on Thursday 01 March 2018 13:17, edited 1 time in total.
Greetings McMelloW
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

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

Post by Derik »

Dear Jake,

Try to make this working..

Code: Select all

2018-03-01 12:58:42.444 LUA: old lost energy was NIL
2018-03-01 12:58:42.449 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:42.678 LUA: old lost energy was NIL
2018-03-01 12:58:42.683 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:42.710 LUA: old lost energy was NIL
2018-03-01 12:58:42.716 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:42.730 (D.M.: Zwave) Usage (Z: Slaapkamer)
2018-03-01 12:58:42.733 LUA: old lost energy was NIL
2018-03-01 12:58:42.734 (D.M.: Zwave) General/kWh (Z: Slaapkamer)
2018-03-01 12:58:42.739 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:42.753 LUA: old lost energy was NIL
2018-03-01 12:58:42.758 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:42.864 (D.M.: Zwave) Usage (Z: Slaapkamer)
2018-03-01 12:58:42.874 (D.M.: Zwave) General/kWh (Z: Slaapkamer)
2018-03-01 12:58:42.988 LUA: old lost energy was NIL
2018-03-01 12:58:42.994 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:43.008 LUA: old lost energy was NIL
2018-03-01 12:58:43.013 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:43.027 LUA: old lost energy was NIL
2018-03-01 12:58:43.032 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:43.066 (D.M.: Zwave) General/kWh (Z: Pellet Totaal)
2018-03-01 12:58:45.779 (D.M.: Zwave) General/kWh (Z: Slaapkamer)
2018-03-01 12:58:45.836 (D.M.: Zwave) Usage (Z: Slaapkamer)
2018-03-01 12:58:45.839 (D.M.: Zwave) General/kWh (Z: Slaapkamer)
2018-03-01 12:58:45.935 (D.M.: Zwave) Usage (Z: Jongens)
2018-03-01 12:58:45.938 (D.M.: Zwave) General/kWh (Z: Jongens)
2018-03-01 12:58:45.983 LUA: old lost energy was NIL
2018-03-01 12:58:45.989 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.003 LUA: old lost energy was NIL
2018-03-01 12:58:46.008 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.022 LUA: old lost energy was NIL
2018-03-01 12:58:46.027 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.040 (D.M.: Zwave) General/kWh (Z: Pellet Totaal)
2018-03-01 12:58:46.043 LUA: old lost energy was NIL
2018-03-01 12:58:46.049 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.063 LUA: old lost energy was NIL
2018-03-01 12:58:46.068 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.084 LUA: old lost energy was NIL
2018-03-01 12:58:46.089 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.103 LUA: old lost energy was NIL
2018-03-01 12:58:46.108 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.121 LUA: old lost energy was NIL
2018-03-01 12:58:46.127 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.140 LUA: old lost energy was NIL
2018-03-01 12:58:46.145 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.159 LUA: old lost energy was NIL
2018-03-01 12:58:46.225 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.240 LUA: old lost energy was NIL
2018-03-01 12:58:46.247 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.346 (D.M.: Zwave) Usage (Z: Scherm)
2018-03-01 12:58:46.350 (D.M.: Zwave) General/kWh (Z: Scherm Totaal)
2018-03-01 12:58:46.374 (D.M.: Zwave) General/kWh (Z: Scherm Totaal)
2018-03-01 12:58:46.434 (Bpi) General/Percentage (Bpi: CPU)
2018-03-01 12:58:46.502 LUA: old lost energy was NIL
2018-03-01 12:58:46.508 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.523 LUA: old lost energy was NIL
2018-03-01 12:58:46.529 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.545 LUA: old lost energy was NIL
2018-03-01 12:58:46.550 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.766 (D.M.: Zwave) Usage (Z: Slaapkamer)
2018-03-01 12:58:46.770 (D.M.: Zwave) General/kWh (Z: Slaapkamer)
2018-03-01 12:58:46.780 (ESP-1xx: Dummy) General/Custom Sensor (ESP: Terrarium)
2018-03-01 12:58:46.798 LUA: old lost energy was NIL
2018-03-01 12:58:46.804 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.818 LUA: old lost energy was NIL
2018-03-01 12:58:46.824 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.838 LUA: old lost energy was NIL
2018-03-01 12:58:46.843 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.857 LUA: old lost energy was NIL
2018-03-01 12:58:46.863 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.877 LUA: old lost energy was NIL
2018-03-01 12:58:46.882 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.896 LUA: old lost energy was NIL
2018-03-01 12:58:46.899 (D.M.: Zwave) Usage (Z: Slaapkamer)
2018-03-01 12:58:46.903 (D.M.: Zwave) General/kWh (Z: Slaapkamer)
2018-03-01 12:58:46.906 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:46.920 LUA: old lost energy was NIL
2018-03-01 12:58:46.925 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:47.043 (D.M.: Zwave) Usage (Z: Pellet Actueel)
2018-03-01 12:58:47.046 (D.M.: Zwave) General/kWh (Z: Pellet Totaal)
2018-03-01 12:58:47.156 LUA: old lost energy was NIL
2018-03-01 12:58:47.161 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:47.175 LUA: old lost energy was NIL
2018-03-01 12:58:47.181 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:47.194 LUA: old lost energy was NIL
2018-03-01 12:58:47.200 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:47.215 LUA: old lost energy was NIL
2018-03-01 12:58:47.220 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:47.234 LUA: old lost energy was NIL
2018-03-01 12:58:47.239 EventSystem: Script event triggered: Lua: Virtuele Batterij
2018-03-01 12:58:47.328 (D.M.: Zwave) Usage (Z: Scherm)
2018-03-01 12:58:47.331 (D.M.: Zwave) General/kWh (Z: Scherm Totaal)
2018-03-01 12:58:47.355
Is this correct?
ScreenShot162.png
ScreenShot162.png (30.7 KiB) Viewed 5767 times
Only 1 is counting?
ScreenShot163.png
ScreenShot163.png (6.9 KiB) Viewed 5767 times
And my Code:

Code: Select all

--Script_time_solar_battery.lua

--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 electricity_meter = 'D.M. P1: Stroom'					-- Existing P1 Electricity device name in 'Utility'

--To be created virtual devices in the hardware section of Domoticz:
local solar_battery = 'Virtual Solar Battery'				-- (1) Custom Sensor name for the 'Virtual Solar Battery'
local solar_battery_idx = 10116
local battery_use = 'Solar Battery Usage'				-- (2) RFXMeter counter name for the 'Solar Battery Usage'
local battery_use_idx = 10115
local lost_energy = 'Lost Solar Energy'					-- (3) RFXMeter counter name for the 'Lost Solar Energy'
local lost_energy_idx = 10114

--User Variables:
local battery_capacitylevel = 'SolarBatteryCapacity'		-- User Variable name for the 'Solar Battery Capacity'. Type 'String'. Value in Wh (4kWh battery = '4000')
local total_electricity = 'TotalElectricity'				-- 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:
local empty_battery = 1							-- 1 =	Include (in other words, with either more solar panels to fill the battery or a 
											--	bigger 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)

--Define variables, used in this script. Don't change!!!
local battery_capacity
local old_total_electricity
local new_total_electricity
local electricity_consumption
local old_battery_value
local new_battery_value
local lost_energy_value
local old_lost_energy_value
local new_lost_energy_value
local battery_usage
local old_battery_usage
local new_battery_usage

commandArray = {} 

	battery_capacity = tonumber (uservariables[battery_capacitylevel])
	if tonumber (uservariables[battery_capacitylevel]) == nil then
		battery_capacity = 5000
		print ("Battery capacity in User Variables was not a number. Capacity will be set to 2000Wh")
		commandArray ['Variable:'.. battery_capacitylevel] = tostring(battery_capacity)
	else
		battery_capacity = tonumber (uservariables[battery_capacitylevel])
	end

	sPlusT1, sPlusT2, sMinT1, sMinT2 = otherdevices_svalues[electricity_meter]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
		sPlusT1 = tonumber(sPlusT1); 	-- energy usage, low tarrif
	sPlusT2 = tonumber(sPlusT2);	-- energy usage, high tarrif
	sMinT1 = tonumber(sMinT1);		-- energy generated, low tarrif
	sMinT2 = tonumber(sMinT2);		-- energy generated, high tarrif
	--print (sPlusT1 .." " .. sPlusT2 .." " .. sMinT1 .." " .. sMinT2)
	--Calculate the new total meter value in Wh (both incoming meters minus both outgoing meters)
	new_total_electricity = (sPlusT1 + sPlusT2) - (sMinT1 + sMinT2)
	--print ("new electricity level is " .. new_total_electricity)
	
	-- Download the previous total meter value from the variable to start the comparison
	if tonumber (uservariables[total_electricity]) == nil then
		old_total_electricity = new_total_electricity
		print (total_electricity .." in User Variables was not a number. Value will be filled with current electricity meter sum")
		commandArray ['Variable:'.. total_electricity] = tostring(new_total_electricity)
	else
		old_total_electricity = tonumber (uservariables[total_electricity])
	end
	--print ("old electricity level is " .. old_total_electricity)
	--Check the energy usage since last update. A positive number means electricity usage, a negative number means electricity generated
	electricity_consumption = (new_total_electricity - old_total_electricity)
	--print ("consumption of electricity is " .. electricity_consumption .." Wh")
	--Write the new energy value to the variable
	commandArray ['Variable:'.. total_electricity] = tostring(new_total_electricity)
	--print ("variable TotalElectricity updated to "..tostring(new_total_electricity))
	--Check if there is a valid value for the virtual solar battery level, if not (like after inital start of the virtual sensor) create a '0'
	if tonumber(otherdevices_svalues[solar_battery]) == nil then
		old_battery_value = 0
		print ("old battery value was NIL")
	else
		--Download the previous value of the virtual solar battery
		old_battery_value = tonumber(otherdevices_svalues[solar_battery])*1000
	end
	--Calculate the new battery value by subtracting the electricity consumption from the previous battery value. 
	--When consumption is positive, it therefore depletes the battery. If the energy consumption is negative, it means that the battery will be charged.
	new_battery_value = old_battery_value - electricity_consumption
	--print ('new battery level is old battery level of '.. old_battery_value .." Wh minus electricity consumption of ".. electricity_consumption.. " Wh = ".. new_battery_value .." Wh")
	
	--Battery can not be further depleted than 'empty' and therefore not < 0 Wh
	if new_battery_value > 0 then
		--Check if the new battery value will not be more than it's capacity
		if new_battery_value <= battery_capacity then
			--print ("Battery value updated to " .. new_battery_value .." Wh")
		-- Check if the new battery value will be higher than the battery capacity
		elseif new_battery_value > battery_capacity then
			--New battery value will be the maximum battery capacity
         new_battery_value = battery_capacity
			--print ("Solar battery maximum capacity of ".. battery_capacity .." Wh reached")
		end
	
   elseif new_battery_value <= 0 then
      new_battery_value = 0
		--print ("Solar battery is empty, energy is taken from the grid")
	end
   commandArray [1] = {['UpdateDevice'] = solar_battery_idx .. '|0|' .. new_battery_value/1000}
--end

-----------------------------------
--Virtual Solar Battery Use
-----------------------------------

--Count the energy used from the battery (meaning that the electricity consumption must be a positve number
if electricity_consumption > 0 then
   battery_usage = old_battery_value - new_battery_value
else
   battery_usage = 0
end

if tonumber(otherdevices_svalues[battery_use]) == nil then
	old_battery_usage = 0
	print (otherdevices_svalues[battery_use])
	print ("old battery energy usage value was NIL. Either it is a first start of the script (new virtual sensor), or something is wrong")
else
	--Download the previous value of the solar battery energy usage in Wh
	old_battery_usage = tonumber(otherdevices_svalues[battery_use])
end
--print ("Old value of the solar battery energy usage is " .. old_battery_usage .. "Wh")

--Calculate the new value for the solar battery energy usage in Wh
new_battery_usage = old_battery_usage + battery_usage
--Virtual counter new value must be higher than the old value to prevent errors in the virtual counter calculation
--print ("New solar battery usage value of " ..new_battery_usage .. ' Wh = Old solar battery usage value of ' ..  old_battery_usage .. ' Wh + battery energy usage of ' ..  battery_usage .. ' Wh')
if new_battery_usage >= old_battery_usage then
	commandArray [2] = {['UpdateDevice'] = battery_use_idx .. '|0|' .. new_battery_usage }
	--print ("New battery usage information sent to sensor "..battery_use_idx.." with value of ".. new_battery_usage)
else
	print ("Something went wrong, the new solar battery usage value of "..new_battery_usage.." is smaller than the previous value of "..old_battery_usage)
end


-----------------------------------
--Virtual Solar Energy Loss 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

--Check if there is a valid value for the virtual lost energy level, if not (like after inital start of the virtual sensor) create a '0' value
if tonumber(otherdevices_svalues[lost_energy]) == nil then
	old_lost_energy_value = 0
	print ("old lost energy was NIL")
else
	--Download the previous value of the virtual lost energy sensor
	old_lost_energy_value = tonumber(otherdevices_svalues[lost_energy])
end

--print ("Old lost energy value is " .. old_lost_energy_value .. "Wh")

--Calculate how much energy is lost after the battery reached it's maximum capacity
lost_energy_value = old_battery_value - electricity_consumption - battery_capacity

if lost_energy_value > 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_value = old_lost_energy_value + lost_energy_value
	--print ("New lost energy of " ..new_lost_energy_value .. ' Wh = Old Lost Energy value of ' ..  old_lost_energy_value .. ' Wh + Lost Energy value of ' ..  lost_energy_value .. ' Wh')
elseif ((old_battery_value - electricity_consumption) < 0 and empty_battery == 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_value = old_lost_energy_value - (old_battery_value - electricity_consumption) --battery value would have been negative, therefore subtracted ( minus x minus = plus) to come to a total lost_energy
		--print ("New lost energy of " ..new_lost_energy_value .. ' Wh = Old Lost Energy value of ' ..  old_lost_energy_value .. ' Wh - Lost Energy value of negative battery level ' ..  (old_battery_value - electricity_consumption) .. ' Wh')
else
	new_lost_energy_value = old_lost_energy_value
end

--Virtual counter new value must be higher than the old value to prevent errors in the virtual counter calculation
if new_lost_energy_value >= old_lost_energy_value then
	commandArray['UpdateDevice'] = lost_energy_idx .. '|0|' .. new_lost_energy_value
else
	print ("Something went wrong, the new lost energy value of "..new_lost_energy_value.." is smaller than the previous value of "..old_lost_energy_value)
end
return commandArray
Do i need to set time/device or variable?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren »

Interresting. Could is also form the basis for a solar power aware household ?
One approach would be to Switch on Boiler, washer, dishwasher when we a have a surplus of solar generated power but maybe other ideas ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »


McMelloW wrote:@Jake
This looks great. I sure will dig into this.

For my understanding:
Solar Battery Usage = Energy produced by your Solarpanels measured at your inverter
Lost Solar Energy = Solar Energy delivered to the grid measured at your P1 Smart Meter
Solar battery usage =
Energy that you take out of your virtual battery:
Example, in 1 hour:
Your solar panels produce 1000Wh
A heater consumes a constant 1500W -> 1500Wh

The above example will have the result that all generated 1000Wh is consumed on the spot by the heater, but that the heater needs an additional 500Wh from the grid.
In reality, currently, the P1 electricity meter will increase with 500Wh. More desirable would be that you had a full solar battery that could deliver the extra 500Wh.
The 'solar battery usage' will show you the consumption of this 500Wh, only of course when the virtual solar battery itself is charged.
The interesting thing to watch on some days, is that the 'virtual solar battery usage' value is bigger than the solar battery capacity itself. That is because during the day there is sometimes energy consumption bigger than the solar panels can deliver (see example of the heater). This then will drain the 'virtual solar battery' and leaving room to 'refill' it again on the same day.

Lost solar Energy=
2 ways of lost energy:
1: The solar energy that is harvested, while your 'virtual battery' is already full. This means feeding into the grid again, with the low reward (when new government legislation will be introduced 'sometime' in the future).
2: (Optional in the script) The same energy consumption as described for the 'solar battery usage', but only when the 'virtual solar battery' is empty. In summertime you then wish you bought a bigger battery, in the winter time (when the sun is not strong enough) you wish it were summer again ;-)
It also gives me also a brainwave for another kind of solution. I have a Solar hotwater boiler installed. The temperature is measured every minute and it is only heated by the sun-collector. I really would like to know the amount of energy the sun puts into my boiler. A virtual battery might do the trick.
I have a solar hot water boiler myself as well. I'm afraid you can't calculate from the temperatures that you log: the solar boiler will have temperature layers. The hottest water is on the top, the coldest on the bottom. I therefore think that it will not be easy to make energy calculations based on this temperature sensor. Maybe, when you measure the temperature of the tube to the solar panel and on the tube from the solar panel, while you either measure or know exactly what kind of water flow you have towards the panel.

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

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

Post by jake »

Derik wrote: Thursday 01 March 2018 13:02 ...
Do i need to set time/device or variable?
I started with 'device', but realized that updating the script with every P1 meter update (every 10 seconds) was a little crazy and overly accurate. Since the basis of the code is the P1 meter with it's total value meters, it will only be a minor mistake in the calculations. Only when there is a big consumer that runs shorter than the timer, we would miss it. The overall picture will still be correct. Therefore I changed to code to a 'timer' script (removing the 'devicechanged' calls)
In the screenshots:
- I see a device with the name 'TotalElectricity'. This device name is not in your code, but 'TotalElectricity' is an user variable (which you created correctly and seems to hold the P1 meter values (combined) accurately)
- I don't see the device 'Lost Solar Energy'. Therefore the script can't set the initial value of '0', giving the message in the log. When the script is run for the first time, this message is OK, but it shouldn't show more than once

BTW, you can give the devices any name you want, as long as you update that first part of the script with your own names as well.
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »

waaren wrote: Thursday 01 March 2018 13:56 Interresting. Could is also form the basis for a solar power aware household ?
One approach would be to Switch on Boiler, washer, dishwasher when we a have a surplus of solar generated power but maybe other ideas ?
Well, the 'virtual solar battery' stays 'virtual' only. Therefore, running the script 'only' gives insight in what kind of capacity would be sufficient for your household.
I have had thoughts to send a notification to (my wife's ;-) ) phone when the returned wattage is > x. I wish we had more smart products in the house:
- smart fridge with remotely adjustable temperature setpoint: I would lower the temperature temporarily when there is a surplus of energy, creating a ice cube battery
- smart dishwasher/washing machine: starting their program at the time of surplus energy.
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

Image
The figure of Grid Delivery (Lost Solar Energy) is far to high. Also the decimal point is not correct
Greetings McMelloW
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »

McMelloW wrote:Image
The figure of Grid Delivery (Lost Solar Energy) is far to high. Also the decimal point is not correct
That number looks weird indeed. It looks like your virtual device wants to be fed with kWh numbers, while it receives them currently as Wh.
Are you on a current new version? While this script runs for me fine for almost a year, I have strange issues with newly created virtual meters that I use in another, new, script.

You can delete the high value in the graphical log off the device by left-clicking on the bad number while holding the shift button. Domoticz will ask you if it may delete the value.
If the problem is the same tomorrow, you can try to change the code at the very bottom of the script by adding a '/1000……' at the point where the lost energy device is updated.

Btw, the 'grid usage' name doesn't cover the content of the meter nor the intention of the script. But, 'what's in a name?' :-)
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

The figure does not match with the figures of my Solar Panels, nor with my P1 Smart Meter. I am on V3.8975 and created the devices today
Greetings McMelloW
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

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

Post by freijn »

Super idea !

I have been thinking on this topic as well for future. Very interesting !

I have an electrical car and are "testing" by charging it during enough sunlight. As you know, today it does not mather charging it
during the day or night. But after 2020 it might become very interesting!

Now.. I have seen a project for my E-car where you can charge the car batteries during the day and use this charge for lights during the evening.
imagine the battery has a capacity of 12kWh / 40Ah :-)
This feature can only be enabled by special ( not public available yet ) software.
Making the car charge only when the sun is powerfull enough would be a nice task for Domoticz.
Of course the manufacture of the chargedevice likes to earn some money on the feature starting and stopping charging the car feature :-(

Still, interesting topic !

THanks

Frank
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »

McMelloW wrote: Friday 02 March 2018 0:32 The figure does not match with the figures of my Solar Panels, nor with my P1 Smart Meter. I am on V3.8975 and created the devices today
For now I can only recommend to delete yesterdays value as described above and see how things go today. It can have to do with the absence of the 'lost energy' device yesterday. You can follow in the log files as well, what is going on with the meter calculation by enabling some of the print commands. Maybe I should update the code to enable/disable all logs with a parameter in the top of the code.

Only with your 'empty battery' you can make a good comparison with the solar panel yield and the P1 meter. As soon as this battery has some charge in it, you will have to compare all 3 devices to make sense of it:
- you first empty the battery before the 'lost energy' starts counting when you consume energy (optional, when setting empty_battery = 1)
- only with a full battery, 'lost energy' starts counting when you generate energy
- 'battery usage' starts counting when the P1 meter goes up, but stops as soon as the battery is empty.
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

Thanks for the updates. Try to understand the calculations in your script. But the results are not constant and not accurate.
So I started a first bit in dzVents and see what the results are. I can not follow it.
Greetings McMelloW
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »

McMelloW wrote: Friday 02 March 2018 15:51 Thanks for the updates. Try to understand the calculations in your script. But the results are not constant and not accurate.
So I started a first bit in dzVents and see what the results are. I can not follow it.
Please enable the print commands in the script. Then you can follow each calculation step by step in the logs.
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

jake wrote: Friday 02 March 2018 15:52 Please enable the print commands in the script. Then you can follow each calculation step by step in the logs.
I did before. Show a part of the log for one run of the script

Code: Select all

2018-03-02 16:37:53.725 EventSystem: Script event triggered: script_time_Solar-Battery
2018-03-02 16:37:54.677 LUA: 805247 858793 69325 146798
2018-03-02 16:37:54.677 LUA: new electricity level is 1447917
2018-03-02 16:37:54.677 LUA: old electricity level is 1447917
2018-03-02 16:37:54.677 LUA: consumption of electricity is 0 Wh
2018-03-02 16:37:54.677 LUA: variable TotalElectricity updated to 1447917
2018-03-02 16:37:54.678 LUA: new battery level is old battery level of 52 Wh minus electricity consumption of 0 Wh = 52 Wh
2018-03-02 16:37:54.678 LUA: Battery value updated to 52 Wh
2018-03-02 16:37:54.678 LUA: Old value of the solar battery energy usage is 0Wh
2018-03-02 16:37:54.678 LUA: New solar battery usage value of 0 Wh = Old solar battery usage value of 0 Wh + battery energy usage of 0 Wh
2018-03-02 16:37:54.678 LUA: New battery usage information sent to sensor 77 with value of 0
2018-03-02 16:37:54.679 LUA: Old lost energy value is 0Wh
2018-03-02 16:37:54.697 EventSystem: Script event triggered: script_time_Solar-Battery
The value for the 'Lost Solar Energy' shows 1447.91 kWh. That is not possible. Set all variables to 0 and with json command all devices as well
Greetings McMelloW
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »

McMelloW wrote: Friday 02 March 2018 16:51
jake wrote: Friday 02 March 2018 15:52 Please enable the print commands in the script. Then you can follow each calculation step by step in the logs.
I did before. Show a part of the log for one run of the script

Code: Select all

2018-03-02 16:37:53.725 EventSystem: Script event triggered: script_time_Solar-Battery
2018-03-02 16:37:54.677 LUA: 805247 858793 69325 146798
2018-03-02 16:37:54.677 LUA: new electricity level is 1447917
2018-03-02 16:37:54.677 LUA: old electricity level is 1447917
2018-03-02 16:37:54.677 LUA: consumption of electricity is 0 Wh
2018-03-02 16:37:54.677 LUA: variable TotalElectricity updated to 1447917
2018-03-02 16:37:54.678 LUA: new battery level is old battery level of 52 Wh minus electricity consumption of 0 Wh = 52 Wh
2018-03-02 16:37:54.678 LUA: Battery value updated to 52 Wh
2018-03-02 16:37:54.678 LUA: Old value of the solar battery energy usage is 0Wh
2018-03-02 16:37:54.678 LUA: New solar battery usage value of 0 Wh = Old solar battery usage value of 0 Wh + battery energy usage of 0 Wh
2018-03-02 16:37:54.678 LUA: New battery usage information sent to sensor 77 with value of 0
2018-03-02 16:37:54.679 LUA: Old lost energy value is 0Wh
2018-03-02 16:37:54.697 EventSystem: Script event triggered: script_time_Solar-Battery
The value for the 'Lost Solar Energy' shows 1447.91 kWh. That is not possible. Set all variables to 0 and with json command all devices as well
2 questions:
- Do you see these numbers change every minute? 2018-03-02 16:37:54.677 LUA: 805247 858793 69325 146798
No? Find out why the P1 meter is not updating your meter values (mine does every 10sec.)
-Do you see the user variable 'TotalElectricity' being updated every minute?
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

jake wrote: Friday 02 March 2018 17:26
2 questions:
- Do you see these numbers change every minute? 2018-03-02 16:37:54.677 LUA: 805247 858793 69325 146798
No? Find out why the P1 meter is not updating your meter values (mine does every 10sec.)
-Do you see the user variable 'TotalElectricity' being updated every minute?
It is a bit hard to see, because the script is runing almost every second. It is in the events and saved as script_time_Solar-Battery.lua But as far as I can see there is no trigger line. When triggered on the change of Electricity it should run every 10 sec. My Electrcity device is updated every 10 sec and the 3 devices for this script as well.
'TotalElectricity' it is updated faster then every minute.

Another do you use the variables sPlusT1, sPlusT2, sMinT1, sMinT2 and not just sCons and sProd from the P1
Greetings McMelloW
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »


McMelloW wrote:
jake wrote: Friday 02 March 2018 17:26
2 questions:
- Do you see these numbers change every minute? 2018-03-02 16:37:54.677 LUA: 805247 858793 69325 146798
No? Find out why the P1 meter is not updating your meter values (mine does every 10sec.)
-Do you see the user variable 'TotalElectricity' being updated every minute?
It is a bit hard to see, because the script is runing almost every second. It is in the events and saved as script_time_Solar-Battery.lua But as far as I can see there is no trigger line. When triggered on the change of Electricity it should run every 10 sec. My Electrcity device is updated every 10 sec and the 3 devices for this script as well.
'TotalElectricity' it is updated faster then every minute.

Another do you use the variables sPlusT1, sPlusT2, sMinT1, sMinT2 and not just sCons and sProd from the P1
The script has the same name for me. Do you execute it within the Domoticz event editor (I have no experience with that one, but recent versions have a selection option for trigger: device, time, all and variable. Choose time) or did you put it in domoticz/scripts/lua folder (in that case the _time_ should run only once per minute and no trigger is needed, since the timer takes care of the 1 minute trigger.

I don't use the sCons and sProd, since measuring the totals is more accurate, cons and prod change over time, the totals will tell the real story.

In the log I see that your script runs every second. It maybe doesn't have time to finish the script in those cases. You will have to figure it out how to get it to run only once per minute.
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

jake wrote: Friday 02 March 2018 18:27
The script has the same name for me. Do you execute it within the Domoticz event editor (I have no experience with that one, but recent versions have a selection option for trigger: device, time, all and variable. Choose time) or did you put it in domoticz/scripts/lua folder (in that case the _time_ should run only once per minute and no trigger is needed, since the timer takes care of the 1 minute trigger.

I don't use the sCons and sProd, since measuring the totals is more accurate, cons and prod change over time, the totals will tell the real story.

In the log I see that your script runs every second. It maybe doesn't have time to finish the script in those cases. You will have to figure it out how to get it to run only once per minute.
Changed the location of the script to domoticz/scripts/lua. Now it is running every minute. I will wait a couple of days to see what happens and hoping it is running OK
I am not very good in Lua. Can I make it triggered on change of the device Electricity. Then it wil run then every 10 sec.
Greetings McMelloW
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

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

Post by jake »

McMelloW wrote: Friday 02 March 2018 19:38
jake wrote: Friday 02 March 2018 18:27
The script has the same name for me. Do you execute it within the Domoticz event editor (I have no experience with that one, but recent versions have a selection option for trigger: device, time, all and variable. Choose time) or did you put it in domoticz/scripts/lua folder (in that case the _time_ should run only once per minute and no trigger is needed, since the timer takes care of the 1 minute trigger.

I don't use the sCons and sProd, since measuring the totals is more accurate, cons and prod change over time, the totals will tell the real story.

In the log I see that your script runs every second. It maybe doesn't have time to finish the script in those cases. You will have to figure it out how to get it to run only once per minute.
Changed the location of the script to domoticz/scripts/lua. Now it is running every minute. I will wait a couple of days to see what happens and hoping it is running OK
I am not very good in Lua. Can I make it triggered on change of the device Electricity. Then it wil run then every 10 sec.
Good to see that you got it running once a minute. I wouldn't recommend to run it with every P1 update. It won't improve too much on the accuracy, especially with the target of the script in mind. It will also occupy your little Pi more than necessary for the task you want it to achieve.

In order to quickly turn the same script into a device driven one, take one of these 2 dirty solutions:

For both: change the file name from script_time_xxxx into script_device_xxx

Option 1: give the script the same name as your P1 electricity meter. That will make the script trigger only on an update of the device with the same name: script_device_NameOfYourElectricty.lua
Option 2:
Just below the commandArray = {} , put in an extra line with the text: 'if devicechanged[electricity_meter] then'

Just before the last 'return commandArray ' put the line with the single word 'end'
(of course both lines without the ' ' signs) those 2 lines will take care that only when the mentioned electricity meter is changed all the code will be excecuted.

More properly would be to rework the script completely to do it right, but I won't do that since the reward isn't worth the effort and mentioned negative consequences of wasting CPU cycles.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests