Skip to content
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

Open
Roving-Ronin opened this issue Jan 2, 2025 · 1 comment
Open

Comments

@Roving-Ronin
Copy link

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.

@Roving-Ronin
Copy link
Author

Worked it out, in .CPP change the Process code to:

void EnergyStatistics::process_(float total) {
  // Publish today's energy
  if (this->energy_today_ && !std::isnan(this->energy_.start_today)) {
    this->energy_today_->publish_state(total - this->energy_.start_today);
  } else if (std::isnan(this->energy_.start_today)) {
    // Initialize today's start value
    this->energy_.start_today = total;
    if (this->energy_today_) {
      this->energy_today_->publish_state(0);  // Publish initial value as 0
    }
  }

  // Publish yesterday's energy
  if (this->energy_yesterday_ && !std::isnan(this->energy_.start_yesterday)) {
    this->energy_yesterday_->publish_state(this->energy_.start_today - this->energy_.start_yesterday);
  }

  // Publish weekly energy (partial or full)
  if (this->energy_week_ && !std::isnan(this->energy_.start_week)) {
    if (this->energy_.full_week_started) {
      // Publish full calendar week value
      this->energy_week_->publish_state(total - this->energy_.start_week);
    } else {
      // Publish partial week value
      this->energy_week_->publish_state(total - this->energy_.start_week);
    }
  } else if (std::isnan(this->energy_.start_week)) {
    // Initialize start_week for the first time
    this->energy_.start_week = total;
    if (this->energy_week_) {
      this->energy_week_->publish_state(0);  // Publish initial value as 0
    }
  }

  // Publish monthly energy (partial or full)
  if (this->energy_month_ && !std::isnan(this->energy_.start_month)) {
    if (this->energy_.full_month_started) {
      // Publish full calendar month value
      this->energy_month_->publish_state(total - this->energy_.start_month);
    } else {
      // Publish partial month value
      this->energy_month_->publish_state(total - this->energy_.start_month);
    }
  } else if (std::isnan(this->energy_.start_month)) {
    // Initialize start_month for the first time
    this->energy_.start_month = total;
    if (this->energy_month_) {
      this->energy_month_->publish_state(0);  // Publish initial value as 0
    }
  }
 
  // Save the current state
  this->save_();
}

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:

  struct energy_data_t {
    uint16_t current_day_of_year{0};
    float start_today{NAN};
    float start_yesterday{NAN};
    float start_week{NAN};
    float start_month{NAN};
    bool full_week_started{false};    // Added
    bool full_month_started{false};  // Added
    bool full_year_started{false};   // Added
  } energy_;  // Instance of the struct

@dentra any interest in this? If so, will create a PULL request for you...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant