-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Energy Statistics - Update Week/Month for Partial Time Period #65
Comments
Worked it out, in .CPP change the Process code to:
And in the .h change the definitions in energy_data_t to add the full week/month, whilst the existing are used for partial time periods:
@dentra any interest in this? If so, will create a PULL request for you...? |
please have a look update at master branch |
You seems to have done it a bit different, looking at energy_statistics.cpp though, are you missing:
around line 87 - Also in energy_statistics.h would you need:
|
Deleted, replaced with comment below |
Just testing your v2 energy_statistics and using your current 'master' code, it appears NOT to be updating all the sensors. Edit: Took a while to update the week / month sensors, but they are now updating. However I note that the Year sensor is NOT updating. I would assume this as the code for year is missing in places within the .cpp and .h Please refer to PULL 67 for patches: #67 As a side issue, in the past I found that if/when updating the firmware due to a change or new release of ESPHome, all the sensors would lose their data. Given this, in the yaml I've found it best to use the sensors from the custom_components to be 'internal: true' and have a lambda that updates a global. From this I then have corresponding template sensors, that read the globals and publish the values. Using this approach means that the today / yesterday / week / month / year and total sensors are all stored by the globals and this data persists between reflashing/updating firmware. If of interest, here is the templated yaml that I use for all my Athom Smart Plugs (no relay) models, there's also versions for Smart Plug v2 and v3 (that have relays), see: https://github.com/Roving-Ronin/myHomeAssistant/blob/main/esphome/sensors/athom-power-plug-no-relay.yaml PS. Probably possible to have this functionality to save to global then publish via sensor built into the custom_component, buy just did it in yaml as it was faster and easier. |
I do not use year stats, so I might have missed something. Thanks. I will look your PR. My code uses absolutely the same approach to store data as globals do. Are you using "restore_from_flash: true" for your esp8266? Also "flash_write_interval" may matters when you reflash faster than it was set. |
Yes on ESP8285 I have:
Whilst ESP8285 and ESP32 both have:
Still erases at times with reflashing / updating ESPHome versions on them, whereas the Energy Total sensor (that is feed into the component) that is stored as a global remains safe, only wiping with a factory reset of the device. |
@dentra |
No idea why, but after 1.5 - 2 days the plugs have now started updating and showing the Week / Month kWh readings. I've submitted a PULL to correct this omission: #70 |
@dentra, This code in the 'EnergyStatistics::loop' would be the cause of why the week/month/year sensors are taking 36-48 hours to first update. This appears to be looking to the value of energy_.start_yesterday and expecting this to be a number, but when its first started its a NAN (showing as NA) value, that is not a valid number that is required. Given this these sensors then fail to initialise for the first day. Its only when the today sensor rolls over and updates the yesterday sensor that the week / month / year sensors are then able to initialise.
|
As it appears the Yesterday value initially being NAN (NA in web interface) does cause the week / month / year sensors to not update, till the yesterday sensor received a numerical value. Thinking we could simply default Yesterday value to 0 (IF there is no prior value in globals), by using this for the Process component:
|
Yes, the year, month and week will update on next day, it is by design to optimize calculation time. NaN is showing that this info is unavailable yet. |
This PULL still needs to be merged anyhow, as your PROCESS component is missing the code for Year. |
Hi, @dentra
Currently the energy_statistics sensor only starts counting for the week and month, if the device is running as it changes from Sunday to Monday / from the last day of the month to the first day of the month. So for example if you plug in a power sensor on day 2 of the week/month (or even part way through the first day) it won't update the weekly or monthly sensors, instead you have to wait for another week/month to pass before they start update.
Would you know how to change this, so these sensors all initalize and start couning immediately, like the today sensor?
Thanks.
The text was updated successfully, but these errors were encountered: