logo elektroda
logo elektroda
X
logo elektroda

Meters capable of measuring negative AC power flow (For Solar) - and how to integrate with HA.

cdtdsilva 2010 11
ADVERTISEMENT
  • #1 21012850
    cdtdsilva
    Level 10  
    So this point are my observations on negative current measurements using tuya devices.

    What's the deal about it? Well AC is constantly changing polarity, so in order to measure the flow of current, one needs to use the phase angle information, in addition to current and voltage. Many AC power meter's can't measure the direction of an AC current, they only report an absolute value, because... most devices only consume power!

    Now, with PV Solar modules becoming common, it's interesting to be able to measure the current in both directions, to understand if one is consuming from the grid or exporting to the grid. If one is exporting, the rates are often so low that it is desirable to first try and consume this energy by activating certain loads such heating, or hot water heating which can be timed to come on when there is an excess of production, avoiding the consumption of excessive energy from the grid and promoting self consumption.

    On the market there are many devices that feature a energy consumption feature. Most are based on the BL0937 and some on the BL0942. The BL0937 is an 8 pin device that uses a series of pulses to indicate the current flow. These can be reconstructed in a microcontroller to provide a representation of the actual current flow. These devices are the most common ones, but they are not able to indicate the direction of power, only it's absolute value. Another reason why you should stay away from them is because if deep sleep or power saving is enabled, pulses might be skipped and power readings will not be accurate.

    Another device, which is not so common is the BL0942 - a fully serial programmable power monitor which provides details on power and direction and can therefore be used for measurements of energy in either direction. Chinese firmware programs the device in order to only obtain the absolute values of power, so to be able to measure it's direction, it is necessary to flash it with Open Becken.
    The devices can be distinguished easily - The more common BL0937 is an 8 pin device, while the BL0942 is a 10 pin device.

    One challenge with OpenBecken is that even though instantaneous power is measured correctly as either a positive or negative value, cumulative power is not. The total KWh reading is always positive, so it simply adds consumption and export together as a single number, which is not very useful to monitor your solar installation or consumption over time.

    I guess one solution would be to poll the instantaneous values and reconstruct the total consumption, but I'm pretty sure the BL0942 chip can be initialized to do this. If someone has an idea how to get to the source code on openbecken, please let me know. In the meanwhile, if time allows, I might just buy myself a pair of CC1101 dongles and use my choice of microcontroller to communicate directly with the BL0942. This would also solve the problem of the WiFi module, as most of my fuse boxes are metal and shielded / grounded, which kills the 2.4Ghz signal.

    Here are some of the devices I found, which are based on the BL0942:

    The Aubess is by far my favourite. The relay cuts the Neutral (So you can wire it bypassing the relay), Power save works fine, reducing self heating, it's cheap, easy to open and easy to program. But It's limited to 16A as the current must go through PCB traces, so it's a stretch to use it in an incoming house supply. Particularly, in case of short circuit, I don't know if the surface mounted sense resistor would survive.

    Aubess power monitor switch on a keyboard Electronic measurement devices on a laptop keyboard.

    The Tongou TO-Q-SY1-JWT is possibly the most promising. The Relay is bystable (it keeps states without power), so that means a failure won't result in loss of power to the house. It is rated up to 63A and it's relatively easy to program once open. But I don't like that it is riveted and so, it's hard to close it back again. It also seems to draw 1W regardless of what, so I wonder about the durability of the caps inside, which run pretty hot. The power supply regulates at 12V, for the relay, and an LM1113 steps down the power to the WiFi module. So 3x it's consumption are just wasted as heat.

    Tongou TO-Q-SY1-JWT relay held in hand Close-up of the bottom part of a device with mounting holes and markings. View of the interior of an electronic device with a 60A 250VAC relay.

    Another interesting device is The SMTONFF. It appears to be the same device covered (https://www.elektroda.com/rtvforum/ttopic4022907.html
    The WB3S module has to be removed to program and it's obviously a larger device. There are others that can be programmed as the WiFi module can be de-attached from tuya MCU, releasing the Serial port. (https://www.elektroda.com/rtvforum/topic3995777.html and https://www.elektroda.com/rtvforum/topic3933839.html)
    I am not fully sure how to read from the Tuya MCU - Who manages the communication with the BL0942 - guess I will figure that out.
    This device can easily be opened (only two screws on the back), however, it relies on a relay that must be constantly powered, so a failure of the device will result in loss of power to the circuits downstream. The current measurement is inline with the relay, so no option to bypass without modifying it. It also heats up a bit inside, drawing the same 0.8W whenever the relay is on or off - I suspect some kind of current limited shunt power supply.
    My version doesn't measure negative values, and since communication with the BL0942 is managed by the tuya MCU, I guess there is not much that can be done other than waiting for tuya to update it's firmware. I also found it annoying that the display shows KWh and instantaneous voltage and current, but not power.

    Electronic module lying on a table with components and pins. Close-up of internal electronic components of a power measurement device. Close-up of a PCB with a BL0942 chip and electronic components. SMTONFF measuring device with a digital display showing current and voltage values.

    If you know of any additional devices based on the BL0942 or how to get under the hood on the programing of the BL0942 under Openbecken, please write here. I think we can all benefit. Meanwhile I will slowly update as I make modifications to the devices.
  • ADVERTISEMENT
  • #2 21014044
    cdtdsilva
    Level 10  

    Here's an overview using the AUBESS Power monitor switch, showing both positive and negative energy flow:
    Screenshot of the AUBESS power monitor switch interface showing parameters. Power monitor AUBESS screen displaying energy data.

    You can see in one direction there's about 0.7W more. That's due to the device measuring its own power consumption :-)

    For this to work, the following flag must be enabled under 'CONFIG' -> 'CONFIGURE GENERAL FLAGS'
    Screenshot of a flag setting in a software.

    Don't be fooled by the description, this doesn't work with the BL0937.
  • #3 21014383
    p.kaczmarek2
    Moderator Smart Home
    Yes, you've found correctly - there is a flag to allow that. This is because in the past we had devices that reported negative current/power in idle.
    If you want to see how it's implemented in code, first see the BL0942 driver itself:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_bl0942.c
    Then you can check the shared code between BL0942, BL0937, and CSE, etc:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_bl_shared.c
    Does the flag suit your needs or should I add anything else soon?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #4 21014898
    cdtdsilva
    Level 10  

    p.kaczmarek2 wrote:
    Yes, you've found correctly - there is a flag to allow that. This is because in the past we had devices that reported negative current/power in idle.
    If you want to see how it's implemented in code, first see the BL0942 driver itself:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_bl0942.c
    Then you can check the shared code between BL0942, BL0937, and CSE, etc:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_bl_shared.c
    Does the flag suit your needs or should I add anything else soon?


    Hi p.kaczmarek2.

    Thanks for your help.
    That flag only solves half of the problem. It does give me instantaneous power in both positive and negative directions, but it doesn't solve the fact that the total counter only goes up, regardless of the power direction. There should be two counters and a check to increment the correct one based on the instantaneous power being positive or negative.
    Screenshot of an interface displaying electrical network parameters showing the device is off.
    I'll have a look into the code. Is there a way I can send binary data straight to the serial port on the Open becken firmware? That would help me debug the registers on the BL0942.
  • ADVERTISEMENT
  • #5 21014932
    p.kaczmarek2
    Moderator Smart Home
    There is no need for that. You can just search for "abs" in the code, here:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_bl0942.c
    I think this fabs here may be the culprit, it was submitted by contributor some time ago:
    Code snippet showing a function calculating energy based on power scale.
    Do you want to get a build without this fabs?

    See: OpenBeken online building system - compiling firmware for all platforms (BK7231, BL602, W800, etc)
    Helpful post? Buy me a coffee.
  • #6 21015581
    cdtdsilva
    Level 10  

    I was able to replicate this behavior on another system initialized with the defaults (only the command 0x58 and 0xAA given to the chip to perform a reading), so I don't think this is a coding issue, rather an initialization.

    If my understanding is right, the total counter is derived from reads to the CF_CNT register
    Diagram and table describing the CF_CNT register in the BL0942 system.

    Assuming that is true, this register is initialized by default as '0b1' which means the absolute value (positive) is accumulated. I think if we were to initialize it as 0, it would count negative. We could initialize it as: 0xA8(Write command);0x19(Address) and then the 3 bytes of data 0x00; 0x00 and 0x04, which keeps everything on its defaults, except bit 7 which is toggled from 1 to 0.
    Operating Mode Register table with descriptions and default values.

    Unfortunately I can't send them directly through the serial terminal, but I think the following line of code issued at the startup of the BL0942 driver would do the trick:

    UART_WriteReg(24, 4)


    Added after 50 [minutes]:

    Having a closer look at the code, it seems we could do something a bit different:

    It seems like the user mode register is initialized as 0x87, as suggested on the datasheet
    #define BL0942_MODE_DEFAULT 0x87


    I suggest, instead initializing it as 0x07. This effectively clears bit 7. It's then possible to write a define as below, in case we want to re-enable it.
    #define BL0942_MODE_CF_CNT_ADD_SEL (1 << 7)

  • #7 21016027
    p.kaczmarek2
    Moderator Smart Home
    I have approved your PR, so we should get binaries soon:
    Screenshot showing a webpage with a request to merge changes in the file drv_bl0942.c on the GitHub platform.
    What about that abs call? Did you remove it? The only change I see is:
    The image shows a proposed change in the drv_bl0942.c code file, where the value defined as BL0942_MODE_DEFAULT was changed from 0x87 to 0x07.
    Helpful post? Buy me a coffee.
  • #8 21016125
    cdtdsilva
    Level 10  
    p.kaczmarek2 wrote:
    I have approved your PR, so we should get binaries soon:
    Screenshot showing a webpage with a request to merge changes in the file drv_bl0942.c on the GitHub platform.
    What about that abs call? Did you remove it? The only change I see is:
    The image shows a proposed change in the drv_bl0942.c code file, where the value defined as BL0942_MODE_DEFAULT was changed from 0x87 to 0x07.


    Thanks,
    I didn't remove the abs call, wanted to do one at a time.
    Do you know where I can download the binaries? Will they show here? Link
  • #9 21016416
    p.kaczmarek2
    Moderator Smart Home
    PR is not released yet. You need to download binaries from PR. See guide:
    p.kaczmarek2 wrote:
    Helpful post? Buy me a coffee.
  • #10 21016484
    cdtdsilva
    Level 10  
    I checked this, but the images were 'skipped' according to this page

    Computer screen showing results of an application build process on GitHub.

    So I created my own fork. Removing the function results in the counter not working. I'm investigating why.
  • #11 21016717
    p.kaczmarek2
    Moderator Smart Home
    This skipped is not a problem. Only main release is skipped, binaries are still created.
    I am speaking about this PR:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1136
    https://github.com/openshwprojects/OpenBK7231T_App/actions/runs/8398182427?pr=1136
    Binaries are here:
    CI/CD actions summary on GitHub for the OpenBK7231T project.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #12 21017599
    cdtdsilva
    Level 10  
    I made a pull request to the OpenBK7231T_App repository with the changes to allow the BL0942 to measure forward and reverse energy flow.

    2 file edits: drv_bl0942.c & drv_bl_shared.c

    Summary of the changes:

    CF_CNT - the 24bit variable that collected the data from active energy accumulation could overflow, causing false readings periodically. The way this varianble reports data was changed, to be cleared after every reading (by setting CF_CNT_CLR_SEL (bit [6], reg 0x19) to one). This also simplifies the calculations, without a need to deduct previous readings (Values are simpply summed to those exixting).

    CF_CNT_ADD_SEL was set to it's default (bit [6], reg 0x19) - counting positive, so the variable doesn't roll over. In order to identify the direction of power flow, CF_REVP_F (bit [0], reg 0x09) is monitored. This bit indicates the direction of the last energy Pulse CF
    0: forward; 1: reverse. Therefore when performing the reading, the value of CF_CNT (Accumulated energy) can be assigned to either the forward or reverse register. At the moment, being there is only one counter register, it increases it or decreases it. In order to get this behaviour, a line on the drv_bl_shared.c file had to be commented out, to allow negative values.

    One other modification: register 0x14 (Active power anti-passage register) was programmed to zero out the instantaneous power at idle (for example, when device measures noise or it's own power consumption). A Value of 64 works well and still allows a 6W light bulb power consumption to be measured. It would be good to have an entry on webpage to adjust this value.

    Energy monitoring panel OpenBK7231N with negative power reading. Screenshot of the OpenBK7231N user interface with active power measurement mode.

    The 'Periodic Statistics' doesn't work well under negative flow. As it can be seen on the pictures above, only the first value can be read negative. Following ones default to zero, which caps the field 'Consumption (during this period)'.

    Where I now need help:
    Fix the inability of the periodic statistics to deal with negative readings
    Add 3 counters, instead of 1, for total power.

    - Forward Energy (Consumption)
    - Reverse Energy (Production)
    - Absolute Energy (Consumption - Production)

    I would like statistics to apply to the "absolute energy" - with an option to set a timer, with an adjustable duration, to reset it. This is to assist with net metering, where utilities allow 15 or 60minutes net metering. After that period, any left over unused energy is 'given' to the network, so the counter should be reset.

Topic summary

The discussion centers on the challenges and solutions related to measuring negative AC power flow using Tuya devices, particularly in the context of solar energy systems. Users highlight the importance of measuring both consumption and export of energy to optimize self-consumption of solar power. The AUBESS Power monitor switch is mentioned as capable of showing both positive and negative energy flow, but issues arise with total energy counters only incrementing positively. Solutions involve modifying firmware, specifically the BL0942 driver, to implement dual counters for positive and negative energy flow. Key modifications include adjusting the CF_CNT register initialization and monitoring the CF_REVP_F bit to determine energy direction. Users share code snippets and links to repositories for further development and troubleshooting.
Summary generated by the language model.
ADVERTISEMENT