Energy measurement with espeasy and pzem-004t

aleph0
Posts: 60
Joined: Thursday 12 May 2016 15:47
Target OS: Linux
Domoticz version: 3.8564
Location: South of France
Contact:

Energy measurement with espeasy and pzem-004t

Post by aleph0 » Saturday 09 December 2017 23:28

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

ikke1
Posts: 3
Joined: Sunday 10 December 2017 11:45
Target OS: Linux
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by ikke1 » Sunday 10 December 2017 12:05

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

aleph0
Posts: 60
Joined: Thursday 12 May 2016 15:47
Target OS: Linux
Domoticz version: 3.8564
Location: South of France
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by aleph0 » Sunday 10 December 2017 12:56

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

ikke1
Posts: 3
Joined: Sunday 10 December 2017 11:45
Target OS: Linux
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by ikke1 » Sunday 10 December 2017 13:16

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...

aleph0
Posts: 60
Joined: Thursday 12 May 2016 15:47
Target OS: Linux
Domoticz version: 3.8564
Location: South of France
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by aleph0 » Sunday 10 December 2017 14:51

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

ikke1
Posts: 3
Joined: Sunday 10 December 2017 11:45
Target OS: Linux
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by ikke1 » Sunday 10 December 2017 14:58

Yes!

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

Best regards,

Frank.

gajotnt
Posts: 50
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Azores
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by gajotnt » Tuesday 12 December 2017 13:36

Hello there, were can i get the bin file? The only one i saw was for 4mb rom, i only have 1mb rom :(

gajotnt
Posts: 50
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Azores
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by gajotnt » Wednesday 13 December 2017 15:50


gajotnt
Posts: 50
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Azores
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by gajotnt » Monday 18 December 2017 13:19

Officially supported in Tasmota 5.10.0b :D

Piacco
Posts: 15
Joined: Friday 14 November 2014 10:33
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by Piacco » Tuesday 26 December 2017 10: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.

gajotnt
Posts: 50
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Azores
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by gajotnt » Tuesday 26 December 2017 13:37

Try tasmota fw

aleph0
Posts: 60
Joined: Thursday 12 May 2016 15:47
Target OS: Linux
Domoticz version: 3.8564
Location: South of France
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by aleph0 » Monday 01 January 2018 19:42

Piacco wrote:
Tuesday 26 December 2017 10: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 ?

spawn
Posts: 1
Joined: Friday 12 January 2018 13:26
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by spawn » Friday 12 January 2018 13:39

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 :)

Alitoy
Posts: 10
Joined: Wednesday 21 February 2018 20:39
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by Alitoy » Tuesday 13 March 2018 22:28

aleph0 wrote:
Saturday 09 December 2017 23: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.

aleph0
Posts: 60
Joined: Thursday 12 May 2016 15:47
Target OS: Linux
Domoticz version: 3.8564
Location: South of France
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by aleph0 » Tuesday 13 March 2018 22:44

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

Alitoy
Posts: 10
Joined: Wednesday 21 February 2018 20:39
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by Alitoy » Tuesday 13 March 2018 23:19

[/quote]
aleph0 wrote:
Tuesday 13 March 2018 22: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 22: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. )

tontze
Posts: 329
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by tontze » Wednesday 14 March 2018 16:12

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
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

Alitoy
Posts: 10
Joined: Wednesday 21 February 2018 20:39
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by Alitoy » Wednesday 14 March 2018 18:23

tontze wrote:
Wednesday 14 March 2018 16: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.

tontze
Posts: 329
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by tontze » Wednesday 14 March 2018 20:36

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
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

Alitoy
Posts: 10
Joined: Wednesday 21 February 2018 20:39
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Energy measurement with espeasy and pzem-004t

Post by Alitoy » Wednesday 14 March 2018 21:00

tontze wrote:
Wednesday 14 March 2018 20: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

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests