logo elektroda
logo elektroda
X
logo elektroda

Bi-Directional meter for Solar with total Consumption and Export Energy counters (BL0942 based)

cdtdsilva 4785 55
Best answers

How can I add and enable extra energy counters such as export, today, yesterday, and hourly totals for BL0942-based devices in OpenBeken, and can this be done from the web interface?

Enable the extra energy statistics by updating `drv_bl_shared.c` (the statistics array) and its matching enumeration, then turn on NTP and run `SetupEnergyStats` so periodic counters like today, yesterday, and last hour are available [#21022822] There is no built-in toggle for this in the native web interface; the suggested approach is to add a console command and, if you want a button, wire it up in the JavaScript webapp instead of the device’s HTTP page [#21025418] For separate import/export handling, keep the energy value as a 32-bit float and split “taken” versus “given” later in the update path rather than adding a new variable unnecessarily [#21024952] If you need to store additional values, reuse reserved/retained storage rather than extending the flash structure, because adding fields breaks compatibility, while flashvars already writes in 64-byte blocks and only erases when a page fills [#21024952]
Generated by the language model.
ADVERTISEMENT
  • #31 21060466
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    p.kaczmarek2 wrote:
    cdtdsilva wrote:
    Ok. Would it be acceptable for me to use a few more on some future release?
    Enable cooling Fan (When power generation greater than setpoint)
    Remote Relay (The flag would enable or disable a storage heater or heat pump based on excess generation)
    Storage system: When there is no consumption, at the end of the netmetering period, it enables a battery storage, to ofset consumption.
    Disable net meteing: Shows Import and Export exactly as metered.

    This is a lot of flags. Maybe we should consider just giving you an extra byte for "power flags" in config struct?

    That would give you 8 flags.

    But I am not sure, I would need to think about it.

    Futhermore... are those strictly related to power metering? SHouldn't be the "enable cooling fan" be in external driver?
    The same goes for remote relay. Hmm


    These are related to solar, hence they only apply when negative energy is measured. I guess we could make a poll and see what others think. I have a strong preference in having the hardware running these automatically, instead of fiddling with home assistant or the likes.

    Passively cooled inverters get pretty hot pushing a few KW, so I run a cooling fan on a timer. In cloudy weather this is not needed, hence the drive to run it based on actual output. In systems with batteries it's not uncommon for the whole room to be hot and needing a exhaust fan to circulate the air.

    The remote relays are intended to either offset aditional generation or the lack of it. They are ideal to control charging and discharging of a storage battery. I also used them as a fool proof reminder than there is excess production and it's a good time to turn some loads on - we can't aumotate everything...

    I also use them to temporary turn off heavy loads such as a storage heater, when excess consumption is detected. This happens often in older instalations with a 16A supply, where several loads are attached to the same phase, or there is only one phase. I guess that could be added outside the scope of solar.

    In either case, if there was an easy way to select the IP address of those 'external relays' instead of having a hard coded one, that would be fantastic. Any ideas? Or at least used a hard coded IP address, based on our current subnet. For example: x.x.x.69.

    On the last version of my fork I have added two new flags: 'Net metering 15min' and 'Net metering 60min'. When none is selected it defaults to a standard meter, incrementing the values of consumption and generation as measured. I will use this to compare with the readings provided by the utility company before adjustments, for calibration purposes.
  • ADVERTISEMENT
  • #32 21060974
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    cdtdsilva wrote:

    In either case, if there was an easy way to select the IP address of those 'external relays' instead of having a hard coded one, that would be fantastic. Any ideas? Or at least used a hard coded IP address, based on our current subnet. For example: x.x.x.69.

    I think you really should look into the scripting engine:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    To set or toggle external relays, you can do in OBK:
    
    SendGet http://192.168.0.112/cm?cmnd=Power0%20ON
    

    
    SendGet http://192.168.0.112/cm?cmnd=Power0%20OFF
    

    
    SendGet http://192.168.0.112/cm?cmnd=Power0%20TOGGLE
    

    You can also script OBK to execute that on event, for example:
    
    // when channel 1 becomes 0, send OFF
    addChangeHandler Channel1 == 0 SendGet http://192.168.0.112/cm?cmnd=Power0%20OFF
    // when channel 1 becomes 1, send ON
    addChangeHandler Channel1 == 1 SendGet http://192.168.0.112/cm?cmnd=Power0%20ON
    

    The code above will mimic relay state on target device (it will follow relay on source device)

    Futhermore, the following should also work:
    
    addChangeHandler Voltage > 251 SetChannel 11 5555
    

    or, for example:
    
    addChangeHandler Power > 60 SetChannel 12 1234
    

    Any command can be triggered with change handler.

    I have just submited a passing self-test for this functionality:
    https://github.com/openshwprojects/OpenBK7231...mmit/b94351aa2bc00f516a081c909a5f05862fb647c7
    https://github.com/openshwprojects/OpenBK7231...mmit/4cfd4014eebebbb2813fd0065aafee216fbfd1b6
    There is, however, a small bug currently - I dont remember who was rewriting the BL shared code, but currently events are only fired when energy counters are on...

    Still, again, I would like to ask you @cdtdsilva to reconsider the design, and please, try to use existing scripting events for things like fan. Do not hardcode stuff.
    You can do:
    
    // external fan on
    addChangeHandler Power > 251 SendGet http://192.168.0.112/cm?cmnd=Power0%20ON
    // fan off
    addChangeHandler Power < 250 SendGet http://192.168.0.112/cm?cmnd=Power0%20OFF
    


    Added after 30 [seconds]:

    PS: We can also add change handler for consumption, of course!
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #33 21062417
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    Hi,

    Yes, I use those. Are you saying I can code them on the autoexec.bat? Would be great if you could give me an example :-)
    That's the bit I don't fully understand. For example, a while ago, I wanted to play with the relay indicator to give me some warnings, but I couldn't find a way to reliably way to link a channel into it.

    Using the main relay seems to trigger an MTQQ event once triggered, which the ESP doesn't always like.

    I was also thinking we should have an MTQQ event reporting net metering energy. Because this goes up and down, the final counter is only updated every hour, so I find myself having to log onto open becken to keep track of where I am.

    If I had an instant balance, I could run an automation on home assistant, for example, change the thermostat setting.
  • ADVERTISEMENT
  • #34 21062517
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    cdtdsilva wrote:

    Yes, I use those. Are you saying I can code them on the autoexec.bat? Would be great if you could give me an example :-)
    That's the bit I don't fully understand.

    Well so maybe let's start with the basics:



    And then in autoexec.bat you create:
    
    addChangeHandler Power > 60 Power OFF
    

    and this sample command will force power off once power is above 60...
    Helpful post? Buy me a coffee.
  • #35 21063183
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    >>21060974

    And this code goes on autoexec.bat?
  • #36 21065687
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    I made some modifications to the code, after trying it for a few days and looking into how the utility companies process net billing.

    It works as follows:

    AC meters still report everything that goes through them. They have no facility to average out the power. What they do is they report the meter readings to the supplier with a time stamp. The supplier them makes adjustments based on your production and consumption and the level of netmetering they give you, if any at all.

    So all of a sudden, the readings on your smart meter are useless as they do not correspond to the data used by the supplier for billing, altough presently this is to the consumer advantage. One should be carefull if changing supplier, though. At some point, I would expect utilities to just remove netbilling, as without a separate counter on the meter itself to calculate the average - this is all a big mess!

    So I decided to leave the consumption meter as is, and the export meter to measure export, instead of netmetering.
    These values will then match the supplier meter readings, so can be used to check the accuracy of both systems.

    That leaves us with the need for a third counter - which is the netmetering for period set by the supplier. What we are actually being billed upon - and what we can use to better make use of our produced energy, including running automations.

    For debugging purposes, I came up with this table which shows the total values for a period of 24h
    Screenshot of energy consumption and net metering data.

    Presentlly stored on RAM - They are not permanent - altough I would like them to be (for 24h), if there is such option.
    3 arrays each with 24 positions of 1 integer (144 bytes - numbers are stored as an int, as they never go too high)

    Meanwhile, values for Netmetering are stored every hour. We do loose some data if the device crashes, but then we limit a whole house system to less than 30 writes a day, which is fantastic. Consumption and export, because they are based on real values now, can be saved as they originally were, based on a threshold.

    I also realised open becken crashes when WiFi is lost. I have a metalic circuit breaker box and sometimes the door gets shut by wind or someone passing. I noticed both meters (each measuring it's own phase) lost RAM values after than and, later on, that they had the same uptime. I don't know if this is a bug that can be sorted? I would immagine they get stuck sending some data and reset by the watchdog.
  • #37 21066744
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    Here are the last changes to the code:

    Table with hourly stats for energy consumed, exported and averaged under netmetering, stored on RAM (Clears on restart).

    New Flags:
    'OBK_FLAG_POWER_ALLOW_NEGATIVE' -> Allow measurement of negative energy
    'OBK_FLAG_POWER_INVERT_AC' -> Invert the direction of AC Power flow, as some devices measure backwards: (Import + / Export -)
    'OBK_FLAG_NETMETERING_15MIN' -> Calculate 15min Netmetering
    'OBK_FLAG_NETMETERING_60MIN' -> Calculate 60min netmetering
    Periodic statistics are enabled with the above two flags and the correct time (15min or 60min) is selected. Changes require a reboot.
    Periodic statistics now measure positive and negative values (The value matches Netmetering, updated once a minute)
    Removed remote relay code. Instead variable 'dump_load_relay' was used for automation purposes with the following statuses:
    - 1: The load is on. There is sufficient solar energy exported.
    - 2: The load is on in Bypass mode. The time can be set by variables: 'bypass_off_time' and 'bypass_on_time' which should be loaded with a value between 0 and 23. Provided the total run time was less than the one set in variable 'min_daily_time_on' which represents minutes the load was on
    - 3: The load is off due to insuficient exported energy
    - 4: The load is off due to overload. The variable 'max_power_bypass_off' is used to indicate the overload value. It should be loaded with a positive value, representing Watts. It takes 3 minutes to reset.

    Notes: For suppliers without Netmetering arrangments, only the Consumption and Export fields apply (Ignore Netmetering and Consumption during this period fields).

    The Starting command should be set to Enable Powersaving, BL0942 and NTP with the correct time zone.
    Example: backlog Powersave 1; startDriver BL0942; startDriver NTP; ntp_timeZoneOfs 2;

    ToDo:
    - Currently I am not exporting the values of Netmetering to home assistant, or in fact saving them to memory as this requires an aditional variable to be added and more changes to the code.
    - Add a second bypass variable, to indicate when energy is negative. This can be used to inject power from a battery storage system ito the grid, when there is consumption but no solar production. For now, one can monitor the variable 'dump_load_relay' with status 3.
  • #38 21066771
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    Not bad, but why do you have separate flags for 15 and 60 minutes metering? What if I want 30 minutes interval?

    Maybe it would be better to have a console command that can set any arbitrary integer value?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #39 21066847
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    p.kaczmarek2 wrote:
    Not bad, but why do you have separate flags for 15 and 60 minutes metering? What if I want 30 minutes interval?

    Maybe it would be better to have a console command that can set any arbitrary integer value?


    Ultimatelly that's what I want to do. For an instant overview over a random period of time, we can now set the periodic statistics, as it measures both positive and negative values and spits out the average during that period. We then need to add it to the hourly counter.
    I just need to change how it works to clear the variables at the end of the period, instead of averaging the last period samples.

    It's all work in progress, but given how long the code has been on the works, I think it's good enough to merge now.

    I have made a series of merges to the main repository. Most of this was manual and needs testing once approved.
    The changes were mostly on drv_bl_shared.c and BL0942.c so shoudln't really impact anything else.

    I still didn't fix the MAX_RETAIN_CHANNELS 10. So maybe you can help me with that? I will aneed to add a new variable for netmetering, which will require two more channels, so it's a good point to review that.
  • #40 21066877
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    @cdtdsilva wait a moment, before I answer, can you fix your Git branch? You are opening many pull requests to main repository...
    List of open pull requests on GitHub, including details such as file name and time opened.
    Helpful post? Buy me a coffee.
  • #41 21066947
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    Screenshot of a repository showing 9 pull requests, with 8 open and 1 closed. I have cancelled the last one, but need those 8 to be aproved.
  • #42 21066960
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    I think you should create one single PR, creating multiple PRs makes users unable to test your code before merging it.

    Futhermore, what is the purpose of extra flags? Why not just let users run the command from autoexec.bat?
    Screenshot displaying a segment of source code with flag checks and the `SetupEnergyStats` command.
    You are hardcoding:
    
    SetupEnergyStats 1 60 60
    

    the same can be put in autoexec.bat by user...
    Helpful post? Buy me a coffee.
  • #43 21067030
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    Can you compromise a bit for now?

    Those changes will only apply to users that enable both reverse energy flag and one of the default set netmetering options.
    If they are not enabled, a random value can be selected for the time stats. It's just that at present there is no variable to save the netmetering values, so I carefully set them to save in the ram, so that they can be tracked in one hour intervals.

    For an upcoming release I am probably going to go the route of the statistics, where data is saved in one minute intervals. That way, any arbitary value can be selected for netmetering - the code will just pull the relevant samples from a 60min batch and the calculations for both the hour and the period are shown.

    Once the code is pushed I can then work on making further changes. I just think it's important I can debug the final release and others can also use the code and provide feedback on any bugs. The next step would be to use the command line, simplify the netmetering functions and fix the variables for the total generation and net metering.

    I have added all the changes into a mirror of the current release and sent a pull request for that. I believe that's what you asked for?
    I have also uploaded the binaries to my device and they seem to work as intended.
  • #44 21068748
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    cdtdsilva wrote:

    I have added all the changes into a mirror of the current release and sent a pull request for that. I believe that's what you asked for?
    I have also uploaded the binaries to my device and they seem to work as intended.

    This form is much better, good job:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1202
    You can also look into Github GUI (download github gui for desktop) to learn how to manage pull requests more easily.




    cdtdsilva wrote:
    Can you compromise a bit for now?

    Those changes will only apply to users that enable both reverse energy flag and one of the default set netmetering options.
    If they are not enabled, a random value can be selected for the time stats. It's just that at present there is no variable to save the netmetering values, so I carefully set them to save in the ram, so that they can be tracked in one hour intervals.

    But is it possible to just tell users to run:
    
    SetupEnergyStats 1 60 60
    

    in autoexec.bat instead of this:
    Screenshot of C code with crossed-out lines.
    ?
    Something like:
    Screenshot of the Filesystem interface on the OpenBK7231T application configuration page.
    would this work instead of two hardcoded flags? Can you try?

    I am requesting this change because of multiple reasons:
    1. we have a limited number of flags
    2. it's a bad practice to use two flags as a select between 3 states (60 interval, 15 interval, no metering)
    3. it's a bad practice to use a flag as a wrapper for a console command call....


    cdtdsilva wrote:

    Once the code is pushed I can then work on making further changes. I just think it's important I can debug the final release and others can also use the code and provide feedback on any bugs.

    Pushing code to main repository means it goes to releases for everybody, that's why I am asking for changes first. We must be sure not to release something causing issues for users.

    cdtdsilva wrote:

    The next step would be to use the command line, simplify the netmetering functions and fix the variables for the total generation and net metering.

    If you want to split ,then let's first finish the current version so it meets basic requirements so I can integrate it ,then we can make a better version.


    cdtdsilva wrote:

    I just think it's important I can debug the final release and others can also use the code and provide feedback on any bugs.

    Anyone can download PR binary from:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1202
    If you don't know how, see:
    https://www.elektroda.com/rtvforum/topic4033833.html#20946719
    Helpful post? Buy me a coffee.
  • Helpful post
    #45 21069092
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    That's not a problem.
    I'll have to re-write the software, which sould in principle not require the flags, if the user manualy sets time statistics.
    Since Netmetering at 60min is already implemented on the tables and that removing the flags will break compatibility with 15min calculations (as currently implemented), I have disable that section of the code. Later on, when I re-write the software thet can be added again.

    I've made changes to patch 3. Can you check it reflected them, or should I cancel the flow and send it again?
    Thanks
  • #46 21069118
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    This PR looks better now:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1202
    Now, the question is, how much is it tested? How much do we test it? @DeDaMrAz , @divadiow , would you be able to give it a try before we merge it? I can also try flashing it onto my BL0942 socket as well.

    @cdtdsilva , do you use it with Home Assistant? Is Home Assistant discovery still working?
    Helpful post? Buy me a coffee.
  • #47 21069187
    divadiow
    Level 38  
    Posts: 5044
    Help: 438
    Rate: 892
    Hi. I've not been following this change. I have maybe two BL0942 devices that I can flash. Not sure what's requested beyond that yet. I need to read through thread.

    Added after 45 [minutes]:

    two devices before and after

    Control panel of the OpenBK BK7231N Mini 16A device with BL0942 chip. Screenshot of the OpenBK software interface for the BK7231N Mini 16A device with BL0942 chip.

    OpenBK user interface showing device information and configuration options. Screenshot of the OpenBK user interface for the Atorch device showing electrical parameters and settings.

    lol. was wondering why the Atorch GUI didn't change. TuyaMCU of course.

    what am I testing with the Mini 16A? Totals over the next few days? I don't feed any power back into the grid...
  • #48 21069267
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    Well the first good step would be know if the current features are still working correctly. @cdtdsilva has made a lot of changes in the code so we need to be sure that nothing was broken in the process...
    Helpful post? Buy me a coffee.
  • #49 21069298
    divadiow
    Level 38  
    Posts: 5044
    Help: 438
    Rate: 892
    calibrated with a 60w incandescent

    Electric energy meter display showing voltage, current, power, and energy consumption.

    before

    Screenshot of the OpenBK_BK7231N device user interface displaying energy consumption data.

    after

    Energy statistics panel of OpenBK device displaying various energy consumption data.

    Added after 9 [minutes]:

    why does today's energy count reset after flashing but yesterday's stays untouched?

    Added after 5 [minutes]:

    the previous today count does not return if you flash back
  • #50 21070093
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    >>21069118

    Thanks. Is the consumed KW counter retained on the right position after flash?

    @divadiow

    Thanks for your help testing

    The startup commands text is required:
     backlog Powersave 1; startDriver BL0942; startDriver NTP; ntp_timeZoneOfs 2; SetupEnergyStats 1 60 60 


    I will check the case with the yesterday's counter.
    By default it should save too frequently. I changed the save threshold so I wouldn't toast the flash on my module. Need to check whenever that part of the code was put back as it was. I will also use a line on autoexec.bat to define the save threshold

    The following will help check the code:
    - Enable Flag 25. If the value of energy is positive, tick Flag 48. The values should now be negative. See whenever that incremets the export counter. If so, restart the module through the webinterface (that ensures is saved). Upon restart check the counter value is saved.
    - Flash back the stock firmware and see whenever any of the counters changed. For example - Are they still the same values? The export should disapear but the rest should remain unchanged.

    Home assistant discovery works fine. Actually need to restart mine to compare the readings with my energy supplier.
    Control panel displaying energy consumption data and other parameters in a home.
  • #51 21071044
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    I cloned today's version of openshwprojects and manually copied over the changes.

    Energy today wasn't assigned to either consumption or generation counters. That's now fixed and linked to consumption.

    @p.kaczmarek2 - I need to change the variable 'energyCounterSampleInterval' from the default 10W a higher value. Can you tell me the correct syntax to do so on the startup commands?

    Thanks

    Screenshot of an energy statistics table with data on frequency, voltage, and consumption.
  • #52 21071889
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    I found another bug,
    The KW hour settings are not being divided by 1000 for consumption, when they are sent via MQTT
    I will push an update and report back later

    Energy usage chart with textual data on the right side.

    Added after 8 [hours] 26 [minutes]:

    Done.
  • #53 21073036
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    cdtdsilva wrote:

    @p.kaczmarek2 - I need to change the variable 'energyCounterSampleInterval' from the default 10W a higher value. Can you tell me the correct syntax to do so on the startup commands?


    Are you asking how to add a command to modify a float variable in C code?

    Well, here is a sample:
    Code: C / C++
    Log in, to see the code

    Basically you first create a function and then call CMD_RegisterCommand to register it.
    Helpful post? Buy me a coffee.
  • #54 21081051
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    @p.kaczmarek2,

    Any updates on the merge of my changes with the main repo?
  • #55 21081056
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14604
    Help: 654
    Rate: 12619
    Which is the most recent version, that one?
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1211/files
    If so, there is still at least one change pending - the one where I said I would prefer to reuse two last retain channels instead of adding a new float field
    Screenshot of comparison changes in the file src/hal/hal_flashVars.h.
    Helpful post? Buy me a coffee.
  • #56 21084669
    cdtdsilva
    Level 11  
    Posts: 171
    Help: 4
    Rate: 15
    cdtdsilva wrote:
    @p.kaczmarek2,


    That will require some more testing, which was the reason I didn't want to make any changes immediately. I see they were merged now?

    I have a question. I've been monitoring some freezes of the module on a custom version I run. I suspect a possible reason could be the MQTT update rate as I see the values going out fairly frequently. I've seen 7 updates going out on a given second, at 4KW.

    Is there a way to change the threshold at which these are sent?

    Thanks

Topic summary

✨ The discussion revolves around the implementation of a bi-directional energy meter based on the BL0942 chip, which now allows for the measurement of both energy consumption and export to the grid. Key features include net metering capabilities that track self-consumption versus exported energy, and the addition of a remote relay to divert excess energy to appliances like water heaters. Participants discuss code modifications to enhance functionality, including the management of energy statistics, memory allocation, and the introduction of flags for various operational modes. Concerns about flash memory wear and the need for efficient data handling are also addressed, alongside suggestions for improving user interaction with the system through console commands and web interface enhancements.
Generated by the language model.

FAQ

TL;DR: For BL0942-based OpenBeken solar monitoring, you can add separate import and export counters, 15- or 60-minute net-metering logic, and automation hooks. "Use existing scripting events," one maintainer advised. The design also targets lower flash wear by saving in 10 Wh-class steps instead of every change. [#21060974]

Why it matters: This helps OpenBeken users measure what they import, what they export, and what actually counts inside a utility net-metering window.

Function Normal export counter Net metering counter
What it measures Energy sent to grid as metered Balance of production and consumption inside a billing window
Time basis Continuous total 15 min or 60 min periods discussed
Best use Match supplier meter readings Drive automations and self-consumption decisions
Sign meaning Export only Positive = excess production, negative = grid use

Key insight: The thread converges on a practical split: keep import and export as true meter-style totals, then add net metering as a separate period-based value. That preserves billing comparability and still enables solar diversion logic. [#21065687]

Quick Facts

  • OpenBeken flash writing was described as 64-byte blocks on a 4096-byte page, giving 4096/64 = 64 writes per erase cycle instead of erase-plus-write on every update. [#21024952]
  • The default save behavior discussed for power metering data was about 10 Wh increments, and one user suggested raising it to about 100 Wh for whole-house use to reduce 1000+ writes per day. [#21031005]
  • Periodic energy stats were configured with commands such as SetupEnergyStats 1 60 60 or SetupEnergyStats 1 60 15, meaning 60-second samples with 60-minute or 15-minute periods. [#21031005]
  • Flags were stored as two 32-bit values, so the thread states a practical maximum of 64 flags in the current layout. [#21054964]
  • A RAM-only hourly table was implemented as 3 arrays × 24 positions × 1 integer, using 144 bytes for consumed, exported, and net-metered values over 24 hours. [#21065687]

How can I add separate import and export energy counters for a BL0942-based OpenBeken device instead of using only one global energy total?

Add separate sensor entries and split signed energy flow into "taken" and "given" paths after the BL0942 update path. The thread suggests keeping the normal consumption total, adding an export or generation total, and exposing each as its own reading instead of relying on one global energy counter. A maintainer explicitly said that if energyWh is negative during energy gain, you do not need a new BL0942 input variable; you need conditional logic later to separate import from export. [#21024952]

What is net metering in the context of solar energy monitoring, and how is it different from a normal export counter?

Net metering is the period balance between produced and consumed energy, not the raw exported total. In the thread, a 1000 W production and 500 W load gives a positive 500 W balance. Later, the design was refined: export should remain a true exported-energy counter that matches the utility meter, while net metering should be a third counter calculated over the supplier period, such as 15 or 60 minutes. That makes export useful for billing comparison and net metering useful for automation. [#21065687]

How do I enable extra OpenBeken energy statistics like Energy Today, Energy Yesterday, and Energy Last Hour on the web interface or through Home Assistant?

Enable NTP, configure energy statistics, and expose the sensor entries in the shared driver tables. The thread points to drv_bl_shared.c, where Energy Today, Energy Yesterday, and Energy Last Hour already exist in the sensor array and enum. To make them work, the maintainer said you must enable NTP and run SetupEnergyStats, with an example of SetupEnergyStats 1 60 60. Those values can then appear in OpenBeken and Home Assistant discovery paths. [#21022822]

Which parts of OpenBK7231T_App need to be modified to expose additional BL0942 readings such as total generation, export energy, and net metering values?

Modify both the BL0942 driver path and the shared energy-reporting layer. The thread repeatedly names src/driver/drv_bl0942.c for raw BL0942 reading changes and src/driver/drv_bl_shared.c for sensor definitions, saving thresholds, MQTT exposure, and web-facing values. Later, one contributor said about 90% of the additions were inside drv_bl_shared.c, with earlier BL0942 acquisition changes in drv_bl0942.c. That is the main code path for export, generation, and period net-metering values. [#21032447]

Why does OpenBeken save energy metering data only at certain thresholds, and how can I change the save interval to reduce flash wear?

It saves at thresholds to reduce flash wear and avoid excessive writes. The thread explains that the flash layer appends 64-byte records and erases only when the page fills, which is far safer than erasing on every update. One user later observed about 10 Wh save steps and rewrote the logic so data saves when either the energy delta exceeds changeSavedThresholdEnergy or 6 hours pass. They also suggested using about 100 Wh for whole-house monitoring to avoid 1000+ writes per day. [#21031005]

What is Flag 25 in OpenBeken power metering, and why should reverse-energy logic only run when that flag is enabled?

Flag 25 is the gate used in this thread to enable negative or reverse-energy handling features. The maintainer asked that the new reverse-energy and net-metering logic run only when Flag 25 is set, because some power chips report negative values randomly. The user later confirmed the implementation: when Flag 25 is off, generation and net-metering statistics are disabled, and only ordinary readings remain. That protects users whose hardware occasionally emits false negative power values. [#21025789]

How can I store a new TotalGeneration value in OpenBeken flash without breaking compatibility with existing ENERGY_METERING_DATA layouts?

Do not add a new float field to ENERGY_METERING_DATA if you want compatibility. The maintainer warned that extending that structure would break stored stats for users and increase wear. Instead, the proposed safe approach was to store the new TotalGeneration value in spare retained-channel bytes and add a helper save function. A later commit proposal specifically aimed to write a 4-byte float into the last retained-channel space while keeping the structure layout intact. [#21031759]

When extending OpenBeken energy storage, is it better to reuse the last retained channels or add a new float field to the flash structure?

Reusing the last retained channels is the preferred approach in this thread. The maintainer rejected adding fields to the flash struct because it would reset existing stats on reboot and accelerate wear. He proposed keeping MAX_RETAIN_CHANNELS at 12 and reusing the last two retained values as one 4-byte float via a cast. That keeps old layouts working and avoids forcing all users through a migration of stored metering data. [#21025864]

How do I use SetupEnergyStats, NTP, and autoexec.bat together to calculate 15-minute or 60-minute solar net metering periods on BL0942 devices?

Use startup commands that enable BL0942, NTP, timezone, and the stats interval. A working example from the thread is: backlog Powersave 1; startDriver BL0942; startDriver NTP; ntp_timeZoneOfs 1; SetupEnergyStats 1 60 15. A similar 60-minute setup uses SetupEnergyStats 1 60 60. 1. Put the commands in autoexec.bat. 2. Reboot the device. 3. Use the chosen 15- or 60-minute period for net-metering calculations aligned to NTP time when available. [#21031005]

What’s the best way to trigger a relay, water heater, or heat pump from excess solar generation in OpenBeken: hardcoded firmware logic or addChangeHandler scripting?

Use addChangeHandler scripting, not hardcoded firmware logic. The maintainer clearly pushed back on embedding relay HTTP control in the core code and recommended the existing scripting engine instead. He argued that scripted events save flash space, avoid niche hardcoding, and keep the feature flexible for fans, heaters, or heat pumps. In his words, "please, try to use existing scripting events" for things like fan control and relay switching from power or consumption thresholds. [#21060974]

How can I use addChangeHandler and SendGet in OpenBeken to control an external smart plug based on power, export, or net metering thresholds?

Create change handlers that fire commands when a reading crosses a threshold. The thread gives direct examples such as addChangeHandler Channel1 == 1 SendGet http://192.168.0.112/cm?cmnd=Power0%20ON and addChangeHandler Power > 251 SendGet http://192.168.0.112/cm?cmnd=Power0%20ON. "addChangeHandler is an event rule that runs a command when a channel or reading changes state or crosses a condition, using built-in commands such as SendGet or SetChannel." Put these lines in autoexec.bat to make the automation persistent after reboot. [#21060974]

Why does Energy Today reset after flashing new firmware while Yesterday stays unchanged on some BL0942/OpenBeken devices?

Because the "today" value was not correctly assigned in one test branch, while older stored daily data remained intact. During testing, a user reported that after flashing, Energy Today reset but Yesterday stayed unchanged, and the previous today value did not return even after flashing back. Later, the contributor identified the bug: Energy Today had not been assigned to either the consumption or generation path and then linked it to consumption. That explains the asymmetry between today and yesterday. [#21071044]

What causes BL602 builds to fail when adding BL0942 power-metering changes, and why does including new_cfg.h fix implicit declaration errors?

The BL602 build is stricter about implicit declarations, so missing headers break compilation. In the thread, a suggested bool-definition fix did not solve the problem. The successful fix was to include new_cfg.h, because the BL602 toolchain enforced declarations that other builds tolerated. One participant stated that new_cfg.h must be included as in other drivers, and the original contributor confirmed, "Thanks, that did the trick!" after adding it. [#21060367]

How do I invert the direction of AC power flow in OpenBeken for devices that report import and export with opposite polarity?

Use the dedicated power-direction inversion flag added during the thread. The request was for a new option that flips polarity because some devices report consumption as positive and others as negative. The maintainer later added exactly that flag, named OBK_FLAG_POWER_INVERT_AC, described as inverting AC power-flow direction for hardware that measures backwards. In the final summary, it was listed alongside the negative-energy flag and net-metering options. [#21066744]

Where in OpenBeken can I read button presses and reliably control the status LED for custom solar automation logic?

The thread does not provide a finished button-press API path, but it does give the recommended control model: use console commands plus the external web app instead of hardcoding HTTP-page buttons. For UI actions, the maintainer said not to add special native HTTP controls because some devices have only 1 MB flash. He advised adding a console command first, then wiring it into the Vue/JS web app. For logic, use channels and addChangeHandler; the thread shows SetChannel and event-based control examples as the supported route. [#21025418]
Generated by the language model.
ADVERTISEMENT