02 septembrie 2023

PZEM Domoticz script

 Yeah, sometime I forgot where I backup some little things...

This is for my own remind...

The script code for PZEM integration into Domoticz...


--[[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 ="Energy"
-- 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)
UpdateDev(dev_A,0,val_A) end
-- if tonumber(val_A)>0 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
view raw PZEM script.txt hosted with ❤ by GitHub

Niciun comentariu:

Most viewed posts in last 30 days