You may know about the PZEM-004T (https://www.aliexpress.com/item/AC-Digi ... 69106.html) : A very neat and accurate power measurement device returning :
- Voltage
- Current
- Active power
- Active Energy consumption
All of this for a very fair price
This device is getting supported by espeasy (https://www.letscontrolit.com/forum/vie ... f=5&t=2595) enabling a very convenient way to get values in domoticz.
However, we dont have (yet ?) in domoticz a sensor V-A-W-Wh do conveniently receive the data from espeasy. So, here is a workaround and a tutorial to get the values in domoticz. The principle is to create a dummy of type "3Ph Ampers" in which espeasy will write the raw values. Then with a device script, we will read the raw values from the 3Ph Amper dummy and dispatch them in separates dummies for V, A and power.
1/ Creates the dummies in domoticz
- If not already existing in your configuration, go to setup->hardware and create a dummy hardware :
- Then click on "Create Virtual sensors" and create the following dummies with the indicated types (you can of course customize the names) :
- Now edit the switch "Elec happy hours" and creates a planning to put it "On" at the beginning of happy hours and "Off" at the end
2/ Create a variable to store the energy index from the pzem
- Go to setup->more-> user variables and creates an integer variable named like the 3ph amp dummy followed by "_idx" :
3/ Create the script to read the value coming from espeasy and dispatch it in the correct dummies
- Go to setup->more->events
- Create a Lua Device script with the name of your choice
Remove the code automatically inserted by domoticz inside the script and copy/paste the following code instead :
Code: Select all
--[[Script to read infos from espeasy/pzem-004t and disptach it in correct devices
]]--
-- Edit the dummies name below to match yours
local dev_HC ="Elec happy hours"
local dev_pzem1 ="pzem1"
local dev_V1 ="Voltage"
local dev_A1 ="Current"
local dev_W1 ="Power"
-- End of user-edit part
function UpdateVar(variable,valeur)
--Update une variable Domoticz
commandArray[#commandArray+1] = {['Variable:'..variable] = tostring(valeur)}
end
function UpdateDev(device,nvalue,svalues)
--Met à jour un device numérique Domoticz
commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[device]..'|'..tostring(nvalue)..'|'..tostring(svalues)}
end
function pzem_read(pzem,HC,dev_V,dev_A,dev_W)
--[[Read values from a 3ph-amp dummies and dispatch them in sensor for voltage, current, and power
Input :
pzem : type 3ph amp, updated by espeasy
HC : switch "On"=happy hours for electricity
Output :
dev_V : type voltage
dev_A : type current
dev_W : P1 smart counter
]]--
local u1, u2, p1, p2
local val_V,val_A,val_W,val_Wh
local local_dbg=0
old_Wh=uservariables[pzem.."_idx"]
u1, u2, p1, p2 = string.match(otherdevices_svalues[dev_W],"(.-);(.-);(.-);(.-);.*")
-- Usefull for newly created counters
u1=u1 or 0
u2=u2 or 0
p1=p1 or 0
p2=p2 or 0
val_V,val_A,val_W,val_Wh=string.match(otherdevices_svalues[pzem],"(.-);(.-);(%d*);(%d*)")
if local_dbg==1 then
print(otherdevices_svalues[pzem])
print(val_V)
print(val_A)
print(val_W)
print(val_Wh)
end
if tonumber(val_V)>1 then UpdateDev(dev_V,0,val_V) end
if tonumber(val_A)>1 then UpdateDev(dev_A,0,val_A) end
if tonumber(val_Wh)>=tonumber(old_Wh) then
if otherdevices[HC] == 'Off' then
-- svalue=USAGE1;USAGE2;RETURN1;RETURN2;CONS;PROD
UpdateDev(dev_W,0,tostring(u1+val_Wh-old_Wh)..';'..tostring(u2)..';0;0;'..tostring(val_W)..';0')
else
UpdateDev(dev_W,0,tostring(u1)..';'..tostring(u2+val_Wh-old_Wh)..';0;0;'..tostring(val_W)..';0')
end
UpdateVar(pzem.."_idx",val_Wh)
end
end
commandArray = {}
if devicechanged[dev_pzem1] then pzem_read(dev_pzem1,dev_HC,dev_V1,dev_A1,dev_W1) end
return commandArray
You're done
Enjoy you 15$ wifi power meter !!