Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[P102] PZEM004Tv3 change Wh to kWh #4966

Merged
merged 2 commits into from
Feb 11, 2024

Conversation

chromoxdor
Copy link
Contributor

I found it misleading, that the value name is called Energy_Wh since the value is in kWh
Took me a while to figure out why the formula %value%/1000 didn´t give me the expected kWh value.

Can anybody confirm or do i have maybe a different revision of this device?

@TD-er
Copy link
Member

TD-er commented Feb 5, 2024

Some energy meters do only show "Wh" values upto some value and then switch to kWh.
If we always get kWh counts from this sensor, then it is OK to change it.
However if it only switches to kWh after some count, then it may need some reconsidering.

@chromoxdor
Copy link
Contributor Author

Some energy meters do only show "Wh" values upto some value and then switch to kWh.

I did a bit of research and at first glance it seemed to be like that....
But then i found this in PZEM004Tv30.cpp

/*! * PZEM004Tv30::energy *
 * Get Active energy in kWh since last reset *
 * @return active energy in kWh*/
float PZEM004Tv30::energy()
{
    if(!updateValues()) // Update vales if necessary
        return NAN; // Update did not work, return NAN

    return _currentValues.energy;
}

@chromoxdor
Copy link
Contributor Author

forgot to attach that:

_currentValues.energy =  ((uint32_t)response[13] << 8 | // Raw Energy in 1Wh
                              (uint32_t)response[14] |
                              (uint32_t)response[15] << 24 |
                              (uint32_t)response[16] << 16) / 1000.0;

@TD-er
Copy link
Member

TD-er commented Feb 5, 2024

Ah so the output is indeed kWh and not Wh.

Strange byte order by the way.

@chromoxdor
Copy link
Contributor Author

chromoxdor commented Feb 5, 2024

Strange byte order by the way.

I wish i could agree but I am working right now to understand what this bit shifting means anyway. :)

@TD-er
Copy link
Member

TD-er commented Feb 5, 2024

Bitshift is literally what the name suggests.

Thus 0010 << 1 = 0100 You literally shift the bits by N places.

So let's assume the response bytes have the hex values 0x13, 0x14, 0x15 and 0x16 on those positions in the response array, then the value becomes:
0x15161314

So there's a rather strange bit order.
It looks like it is little-endian and big-endian mixed.

It is probably OK, but still looks a bit 'odd'.

@chromoxdor
Copy link
Contributor Author

Minutes ago i asked chatgpt why it is strange :)
Bildschirmfoto 2024-02-05 um 22 24 36

@TD-er
Copy link
Member

TD-er commented Feb 5, 2024

Ah glad to see chatGPT also understands why things might look strange :)

@TD-er TD-er merged commit 4873c50 into letscontrolit:mega Feb 11, 2024
168 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants