Skip to content

Commit

Permalink
Added script_device_power2p1.lua to manage a virtual P1 meter from an…
Browse files Browse the repository at this point in the history
… existing general kWh meter
  • Loading branch information
CreasolTech committed Jun 11, 2024
1 parent fd0fd9a commit 920c436
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
1 change: 1 addition & 0 deletions script_device_master.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ end

if (run_power==1) then
dofile "scripts/lua/power.lua"
-- dofile "scripts/lua/power2p1.lua"
end

if (run_pushbuttons==1) then
Expand Down
88 changes: 88 additions & 0 deletions script_device_power2p1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
-- scripts/lua/script_device_power2p1.lua - Called by script_device_master.lua
-- Written by Creasol, https://creasol.it [email protected]
-- Used to fill a virtual P1 meter with import and export power/energy. P1 meter is used by the Energy Dashboard

GRID_DEV="PowerMeter Grid" -- source used to measure the grid power (negative when producing)
P1_DEV="PowerMeter Grid P1" -- virtual P1 meter that should be feed by this script
TARIFF1_START=420 -- minutes since midnight when TARIFF1 starts (420=7:00)
TARIFF1_STOP=1360 -- minutes since midnight when TARIFF2 stops (1360=23:00)

commandArray={}
if (devicechanged[GRID_DEV]~=nil) then
devValue=devicechanged[GRID_DEV] -- general kWh meter returns power;energy
local power=0
local energy=0
local energyOld=0
local energyDiff=0
local str=""
local i=0
for str in devValue:gmatch("[^;]+") do
if (i==0) then
power=tonumber(str)
i=1
else
energy=tonumber(str)
end
end
-- check that uservariables[zP1Energy] exists
if (uservariables['zP1Energy']~=nil) then
energyOld=tonumber(uservariables['zP1Energy'])
else
-- zP1Energy variables does not exist: create it!
energyOld=energy
os.execute('curl -m 1 "http://127.0.0.1:8080/json.htm?type=command&param=adduservariable&vname=zP1Energy&vtype=0&vvalue='..energyOld..'"') -- Create variable, type integer
print("Power2P1: create new variable zP1Energy")
end
energyDiff=energy-energyOld

local usage1,usage2,return1,return2,powerin,powerout
i=0
for str in otherdevices_svalues[P1_DEV]:gmatch("[^;]+") do
if (i==0) then
usage1=tonumber(str)
i=1
elseif (i==1) then
usage2=tonumber(str)
i=2
elseif (i==2) then
return1=tonumber(str)
i=3
elseif (i==3) then
return2=tonumber(str)
i=4
break
end
end
local timeNow = os.date("*t")
local minutesNow = timeNow.min + timeNow.hour * 60 -- number of minutes since midnight


if (energyDiff>=0) then
-- usage have to be incremented
if (minutesNow>=TARIFF1_START and minutesNow<TARIFF1_STOP) then
usage1=usage1+energyDiff
else
usage2=usage2+energyDiff
end
else
-- return have to be incremented
if (minutesNow>=TARIFF1_START and minutesNow<TARIFF1_STOP) then
return1=return1-energyDiff
else
return2=return2-energyDiff
end
end
if (power>=0) then
powerin=power
powerout=0
else
powerin=0
powerout=0-power
end
str=usage1..';'..usage2..';'..return1..';'..return2..';'..powerin..';'..powerout
commandArray[0]={['UpdateDevice']=otherdevices_idx[P1_DEV].."|0|"..str}
commandArray['Variable:zP1Energy']=tostring(energy)
-- print('Power2P1: energy='..energy..'Wh energyOld='..energyOld..'Wh energyDiff='..energyDiff..'Wh => Update P1 meter with values '..str)
end
return commandArray

4 changes: 2 additions & 2 deletions script_time_earthquake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ DEBUG_PREFIX="EarthQuake: "
EARTHQUAKE_DEV="EarthQuake" -- Create this text device by yourself!
MAXRADIUS=8 -- used to restrict data from the earthquake source (default 4)
MAXDISTANCE=1000 -- Max distance in km
MINMAGNITUDE=2.5 -- Min Richter magnitude
TELEGRAMMAGNITUDE=3.5 -- Min magnitude to send notification on smartphone
MINMAGNITUDE=3.5 -- Min Richter magnitude
TELEGRAMMAGNITUDE=4.2 -- Min magnitude to send notification on smartphone
LATITUDE=45.88 -- Your latitude
LONGITUDE=12.18 -- Your longitude
MAXAGE=24 -- Remove quakes earthquakes older than MAXAGE hours
Expand Down
13 changes: 8 additions & 5 deletions script_time_heatpump_emmeti.lua
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ if (HPlevel~="Off") then
end
diffMax=math.floor(diffMax*100)/100
if (HPforce=="Night") then
diffMax=diffMax+1 -- force starting
diffMax=diffMax+2 -- force starting
end

-- diffMax=-0.56 --DEBUG
Expand Down Expand Up @@ -785,18 +785,21 @@ end

-- now scan DEVlist and enable/disable all devices based on the current level HP['Level']
if (HPmode == 'Summer') then
devLevel=4
if (otherdevices[HPSummer]~='On') then
commandArray[HPSummer]='On'
end
devLevel=4 -- used to select the proper column in DEVlist structure
compressorPerc=compressorPercOld+(prodPower-500)/30 -- try have 500W exported
if (compressorPerc<5) then
-- no enough power: set heat pump to minimum, and disable VMC DEHUMIDIFY if enabled
compressorPerc=5
deviceOff(VENTILATION_DEHUMIDIFY_DEV,HP,'DD')
end
if (minutesnow>=HPNightStart or minutesnow<HPNightEnd) then
if (minutesnow>=timeofday['SunsetInMinutes'] or minutesnow<timeofday['SunriseInMinutes']) then
if (compressorPerc>30) then compressorPerc=30 end -- avoid too noise in the night
end
else
devLevel=2 -- default: Winter
devLevel=2 -- default: Winter. Column in DEVlist structure
end
if (HP['OL']~=0 and heatingCoolingEnabled~=0) then
-- overlimit on : track power
Expand Down Expand Up @@ -845,7 +848,7 @@ updateValves() -- enable/disable the valve for each zone
-- other customizations....
-- Make sure that radiant circuit is enabled when outside temperature goes down, or in winter, because heat pump starts to avoid any damage with low temperatures

if (outdoorTemperature<=4 or (HPmode=='Winter' and HP['Level']>LEVEL_OFF) or (HPmode=='Summer' and HP['Level']>=1 and otherdevices[HPLevel]~='Dehum') or GasHeaterOn==1) then
if (outdoorTemperature<=4 or (HPmode=='Winter' and HP['Level']>LEVEL_OFF) or (HPmode=='Summer' and HP['Level']>=1 and otherdevices[HPLevel]~='Dehum' and otherdevices[HPLevel]~='DehumNight') or GasHeaterOn==1) then
if (otherdevices['Valve_Radiant_Coil']~='On') then
commandArray['Valve_Radiant_Coil']='On'
HP['trc']=0
Expand Down

0 comments on commit 920c436

Please sign in to comment.