Electric energy calculation using only usage(watts)?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
gajotnt
Posts: 70
Joined: Monday 06 February 2017 12:48
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Azores
Contact:

Electric energy calculation using only usage(watts)?

Post by gajotnt »

Hello there, is there anyway to have domoticz receive power used (watts) and then calculate the energy usage?

I have a "Total" energy/usage counter, and a "Solar" energy/usage counter, they both work great, but i wanted to have a idea of how much energy i waste, so i tweaked a LUA script to update a "Waste" energy/usage counter, the problem is it registers the wasted energy but doesnt calculate the energy usage based on the value of the wasted energy.

I read somewere that LUA doesnt work correctly when calculating energy usage, and efectivly it does not do the computation, it just takes the energy value from the other counter, is there anyway to make this work on dzvents?
Thanks in advance :)

Code: Select all

-- Get current date & time
t1 = os.time()
local currentDate = os.date("*t");  -- sets up currentDate.[table] 
-- (currentDate.year [full], .month [1-12], .day [1-31], .hour [0-23], .min [0-59], .sec [0-59], .wday [0-6 {Sun-Sat}])
sCurrentTime = currentDate.year .. "-" .. currentDate.month .. "-" .. currentDate.day .. " " .. currentDate.hour .. ":" .. currentDate.min .. ":" .. currentDate.sec

function update(device, id, power, energy, index)

commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. power .. ";" .. energy}

end 

commandArray = {}

TotalPowerUsage, TotalEnergyUsage = otherdevices_svalues["Total"]:match("([^;]+);([^;]+)")

if tonumber(TotalPowerUsage) <=0  then

WastePowerBalance = -(TotalPowerUsage)
WasteEnergyBalance = TotalEnergyUsage 

end

if tonumber(TotalPowerUsage) >=1  then

WastePowerBalance = 0
WasteEnergyBalance = TotalEnergyUsage 
end

update("Waste", 98, WastePowerBalance, WasteEnergyBalance, 2) -- 98 is the ID of the dummy incoming device

return commandArray
gajotnt
Posts: 70
Joined: Monday 06 February 2017 12:48
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Azores
Contact:

Re: Electric energy calculation using only usage(watts)?

Post by gajotnt »

Did lots and lots of reading and trial and error finally got this working

So "Total" is my main Electricity counter (SDM120) that gives me positive and negative values (when solar panels producing more than the house consumes).
The idea is that i have a register of how many Euros I have wasted by not using 100% of the Solar Power. Since i have a Sonoff POW telling me how much they generated, this way i can subtract the wasted from the generated and really know my savings from solar power.

"Wasted" dummy device must be an Electric (Instant + Counter) and energy read must be set to compute

Code: Select all

return {
	on = {
	          devices = {
         'Total' --Name of the counter you are reading from (this ensures your new dummy device gets updated when this one is) 
      }
	},
	
	
	execute = function(domoticz, device)
		    if domoticz.devices('Total').WhActual < -1 then --change total to your device name
		       currentpower = -(domoticz.devices('Total').WhActual) --turns the negative value to positive and defines the variable from the main counter
		        domoticz.openURL({
                        url = 'http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue='..currentpower..';0', --idx=98 is my Waste counter ID, set this by your setup
                        method = 'GET'})
            else
                		        domoticz.openURL({
                        url = 'http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue=0;0',
                        method = 'GET'})
            end
	end
}
karibe66
Posts: 1
Joined: Thursday 13 September 2018 11:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Electric energy calculation using only usage(watts)?

Post by karibe66 »

Thank you very much for sharing it!!
I was getting crazy trying to make domoticz compute energy, many days and no way with LUA script
This is very helpful for me
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest