logo elektroda
logo elektroda
X
logo elektroda

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

cdtdsilva 2328 55
ADVERTISEMENT
  • #31 21060466
    cdtdsilva
    Level 10  
    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
    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 10  
    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.
  • #34 21062517
    p.kaczmarek2
    Moderator Smart Home
    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 10  
    >>21060974

    And this code goes on autoexec.bat?
  • #36 21065687
    cdtdsilva
    Level 10  
    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 10  
    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.
  • ADVERTISEMENT
  • #38 21066771
    p.kaczmarek2
    Moderator Smart Home
    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.
  • #39 21066847
    cdtdsilva
    Level 10  
    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
    @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 10  
    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
    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 10  
    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
    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 10  
    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
    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 34  
    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
    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.
  • ADVERTISEMENT
  • #49 21069298
    divadiow
    Level 34  
    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 10  
    >>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 10  
    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 10  
    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
    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 10  
    @p.kaczmarek2,

    Any updates on the merge of my changes with the main repo?
  • #55 21081056
    p.kaczmarek2
    Moderator Smart Home
    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 10  
    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.
Summary generated by the language model.
ADVERTISEMENT