Hi p.kaczmarek2!
The piece of code I used to save the generation variable is for development purposes. I will use your code, once you're finished. For now i just made sure the correct memory positions were used, so it's backwards compatible with the final code.
I may need some help to sync your repo to mine - last time it just over-wrote all my changes (good thing I had a backup). This time, too many files were changed to do it by hand. I'm not a daily github user, so not sure on all the perks there...
I noticed power data is saved in 10Wh intervals. That's probably usefull to document.
10Wh increments is probably fine for a smart plug, for whole house power consumption, it will likelly write the flash 1000+ times a day.
I sugest users change the default value to 100W and have added a small indicator showing the set value.
The variable that writes the flash is the one below, inside src/driver/drv_bl_shared.c. I have re-written it to save whenever either of the values goes above the threshold set by 'changeSavedThresholdEnergy'.
if (((sensors[OBK_CONSUMPTION_TOTAL].lastReading - lastSavedEnergyCounterValue) >= changeSavedThresholdEnergy) ||
((xTaskGetTickCount() - lastConsumptionSaveStamp) >= (6 * 3600 * 1000 / portTICK_PERIOD_MS))
I modified the Periodic statistics feature to fit the needs of Netmetering (the balance between consumption and generation), as currently implemented by power companies.
Because both the period and time zone will vary across the globe, the user will need to enable NTP and statistics manually.
My start command looks like this, for reference:
backlog Powersave 1; startDriver BL0942; startDriver NTP; ntp_timeZoneOfs 1; SetupEnergyStats 1 60 15
Energy stats sets the period offered by the utility company. 60 seconds and x minutes. The rest should be self-explanatory.
The statistics themselves, show the results for consumption, just as before. Netmetering balance is shown in a separate entry. A positive value indicates excess production and negative indicates consumption from the grid.
Where there is excess production, I made provision to turn on a remote smart plug. This can be used to turn on an immersion heater or command a heat pump. In Poland, the present regulations offer 60min netmetering period, which is very generous. The relay goes off if either the solar array is not producing above a minimun value or we drop below a certain ammount of excess production, otherwise it remains on. The turn on and off thresholds are different, to accomodate some hysteresis, plus the relay only changes states every 2 minutes.
Right now the default values are:
> 50W (instant) and >10Wh excess production to turn on the relay,
<50W (instant) and <5Wh excess production turns off the relay.
2 minutes for changing states
These values may need to be adjusted, based on the power of the instalation.
Presently the IP address of the remote plug is hard coded.
NTP was enabled and used to sync the readings. Where the device is connected to internet and able to sync, NTP is also used to sync the internal time at the start of every hour (HH:00min). This ensures the start of the metering period matches the one used by the utility company. The status of this relay is displayed on the webpage.
These features are all enabled using Flag 25.
I didn't check the MTTQ features as I don't have HA by hand. In principle everything should work, but I didn't test to see if the generation readings are reported correctly.
I leave it up to you what features you want to keep for the final version. It would be nice if the webpage was able to accomodate customization of these parameters by the end user. I can help document those.
For reference, here's a screenshot. For anyone wanting to try the binaries, they are added as an attachment to this post.