Page 1 of 4

Energy measurement with espeasy and pzem-004t

Posted: Saturday 09 December 2017 22:28
by aleph0
Hello All !

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 :
Image
- Then click on "Create Virtual sensors" and create the following dummies with the indicated types (you can of course customize the names) :
Image
Image
Image
Image
Image
- 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
Image

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" :
Image

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
Image

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
If you have customized the dummiez name when you created them, you have to update the part with the "local dev_" definition to reflect your custom names

You're done :)
Enjoy you 15$ wifi power meter !!

Image
Image

Re: Energy measurement with espeasy and pzem-004t

Posted: Sunday 10 December 2017 11:05
by ikke1
Hi,

Thank you very much for your detailed workaround.
Unfortunately, I'm not able to get the values to display on my virtual devices.
I used custom names, they all seem to match. But somehow it doesn't work?
Maybe you see something I messed up? ;-)

Image

Re: Energy measurement with espeasy and pzem-004t

Posted: Sunday 10 December 2017 11:56
by aleph0
Yes, something must be wrong, we must diagnose further ; Do you have any errors in the error log of domoticz ? Please turn on debugging by setting local local_dbg=1 inside the pzem_read function and post a screenshot of you log

Re: Energy measurement with espeasy and pzem-004t

Posted: Sunday 10 December 2017 12:16
by ikke1
First of all, thanks for you patience and helping me out.

The log keep repeating the following lines:

2017-12-10 12:11:39.038 LUA: 239.80;8.12;984.00;89607.00

2017-12-10 12:11:39.039 Error: EventSystem: in pzem: [string "--[[Script to read infos from espeasy/pzem-00..."]:58: attempt to compare number with nil

2017-12-10 12:11:55.028 LUA: 239.80;3.49;773.00;89611.00

2017-12-10 12:11:55.028 Error: EventSystem: in pzem: [string "--[[Script to read infos from espeasy/pzem-00..."]:58: attempt to compare number with nil

etc...

Re: Energy measurement with espeasy and pzem-004t

Posted: Sunday 10 December 2017 13:51
by aleph0
Argh :( I forgot to mention one point !

pzem resolution for power is 1W, so I adapted the script for integer values for power and energy. You need to setup your espeasy decimals that way :
Image

Re: Energy measurement with espeasy and pzem-004t

Posted: Sunday 10 December 2017 13:58
by ikke1
Yes!

Thank you very much! That does the trick :D
Thank you for you patience!
And have a nice Sunday!

Best regards,

Frank.

Re: Energy measurement with espeasy and pzem-004t

Posted: Tuesday 12 December 2017 12:36
by gajotnt
Hello there, were can i get the bin file? The only one i saw was for 4mb rom, i only have 1mb rom :(

Re: Energy measurement with espeasy and pzem-004t

Posted: Wednesday 13 December 2017 14:50
by gajotnt

Re: Energy measurement with espeasy and pzem-004t

Posted: Monday 18 December 2017 12:19
by gajotnt
Officially supported in Tasmota 5.10.0b :D

Re: Energy measurement with espeasy and pzem-004t

Posted: Tuesday 26 December 2017 9:32
by Piacco
Thank you for this great tutorial :D

But i can't get it right, the Current value won't update.
I my log i have the following error:

2017-12-26 09:22:00.497 Error: EventSystem: in pzem: [string "--[[Script to read infos from espeasy/pzem-00..."]:73: attempt to index global 'devicechanged' (a nil value)

The value's of pzem seems to be right:
2017-12-26 09:25:42.784 LUA: 229.4;0.21;49;147
2017-12-26 09:25:42.784 LUA: 229.4
2017-12-26 09:25:42.784 LUA: 0.21
2017-12-26 09:25:42.785 LUA: 49
2017-12-26 09:25:42.785 LUA: 147

Best regards,

Albert.

Re: Energy measurement with espeasy and pzem-004t

Posted: Tuesday 26 December 2017 12:37
by gajotnt
Try tasmota fw

Re: Energy measurement with espeasy and pzem-004t

Posted: Monday 01 January 2018 18:42
by aleph0
Piacco wrote: Tuesday 26 December 2017 9:32 Thank you for this great tutorial :D

But i can't get it right, the Current value won't update.
I my log i have the following error:

2017-12-26 09:22:00.497 Error: EventSystem: in pzem: [string "--[[Script to read infos from espeasy/pzem-00..."]:73: attempt to index global 'devicechanged' (a nil value)

The value's of pzem seems to be right:
2017-12-26 09:25:42.784 LUA: 229.4;0.21;49;147
2017-12-26 09:25:42.784 LUA: 229.4
2017-12-26 09:25:42.784 LUA: 0.21
2017-12-26 09:25:42.785 LUA: 49
2017-12-26 09:25:42.785 LUA: 147

Best regards,

Albert.
Sorry for the late reply, I see your post only now :( Happy new year by the way !
It seems you're not using "device" type scripts : Can you please check this point and confirm ?

Re: Energy measurement with espeasy and pzem-004t

Posted: Friday 12 January 2018 12:39
by spawn
Thanks a lot for this tutorial!
I had the same problem with current update as @Piacco and found out that the current updates only when it is more than 1A. I checked the provided Lua Device script and found this line

Code: Select all

if tonumber(val_A)>1 then UpdateDev(dev_A,0,val_A) end
After changing 1 to 0 I have no problems with current :)

Re: Energy measurement with espeasy and pzem-004t

Posted: Tuesday 13 March 2018 21:28
by Alitoy
aleph0 wrote: Saturday 09 December 2017 22:28
Thank you for the informative post.
I would very much like to repeat it.

I ask you to tell me where you can download the current Firmware ESPeasy for PZEM-004T?
I downloaded the firmware from GITHUB : https://github.com/letscontrolit/ESPEasy/releases
Release mega-20180313 and Release v2.0-20180313
But there is no plugin for PZEM-004T .... :?

I think it will be convenient if you attach the correct firmware to this topic.

I will be grateful for help.

Re: Energy measurement with espeasy and pzem-004t

Posted: Tuesday 13 March 2018 21:44
by aleph0
Hi ! The PZEM-004T is in the "testing" mode, and I'm pretty sure we didn't port it to the 2018-03-13 version. I might try to compile it for this version on week-end, but won't have any hardware to test it anymore :/ Or you can use our development version here https://www.letscontrolit.com/forum/vie ... =30#p21383

Re: Energy measurement with espeasy and pzem-004t

Posted: Tuesday 13 March 2018 22:19
by Alitoy
[/quote]
aleph0 wrote: Tuesday 13 March 2018 21:44 Or you can use our development version here https://www.letscontrolit.com/forum/vie ... =30#p21383
Thank you ! )))
Thanks for quick response. I will try. )
aleph0 wrote: Tuesday 13 March 2018 21:44 The PZEM-004T is in the "testing" mode, and I'm pretty sure we didn't port it to the 2018-03-13 version. I might try to compile it for this version on week-end, but won't have any hardware to test it anymore :/
No problems ! I can test the new version.
And report the results. )))
I would be happy if you trust me to test the new 2018-03-13 version. )

Re: Energy measurement with espeasy and pzem-004t

Posted: Wednesday 14 March 2018 15:12
by tontze
Very interesting !

Ordered what needed to check it out. But, i would like to use split coil's here :

https://www.aliexpress.com/wholesale?ca ... ransformer

But i really dont know which one is right one, does any1 else have any glue how to measure/select correct one ?

-T

Re: Energy measurement with espeasy and pzem-004t

Posted: Wednesday 14 March 2018 17:23
by Alitoy
tontze wrote: Wednesday 14 March 2018 15:12 .... But, i would like to use split coil's here :
https://www.aliexpress.com/wholesale?ca ... ransformer
But i really dont know which one is right one, does any1 else have any glue how to measure/select correct one ?
No. I'm not sure what with the coil as you showed it will work correctly and right.
Together with PZEM-004T, in the kit, there is already a coil. And it is correct and designed to work with electronics PZEM-004T. Will other coils work correctly (accuracy of readings), is unknown.

Re: Energy measurement with espeasy and pzem-004t

Posted: Wednesday 14 March 2018 19:36
by tontze
Coil suplied, looks like its so small that you cannot fit any plugs through it .. Anyways, after some digging, amazon.com is selling same pzem-004t, but with this coil :

https://www.ebay.com/itm/PZCT-02-Split- ... 0290.m3507

Re: Energy measurement with espeasy and pzem-004t

Posted: Wednesday 14 March 2018 20:00
by Alitoy
tontze wrote: Wednesday 14 March 2018 19:36 Coil suplied, looks like its so small that you cannot fit any plugs through it ..
You're wrong. Not "fit any plugs".
See the wiring diagram. Only one wire passes through the coil.
Image

Here are the dimensions of the coil and the PZEM-004T board.
(I think that many of this information will be interesting)
Image