-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script_device_power2p1.lua to manage a virtual P1 meter from an…
… existing general kWh meter
- Loading branch information
CreasolTech
committed
Jun 11, 2024
1 parent
fd0fd9a
commit 920c436
Showing
4 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¶m=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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters