1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-13 00:32:20 +01:00

Fix for noise in pulse_counter and duty_cycle components (#2646)

This commit is contained in:
Carlos Garcia Saura
2021-11-01 20:27:57 +01:00
committed by GitHub
parent d8b3af3815
commit d54b4e7c44
4 changed files with 22 additions and 17 deletions

View File

@@ -155,16 +155,20 @@ void PulseCounterSensor::dump_config() {
void PulseCounterSensor::update() {
pulse_counter_t raw = this->storage_.read_raw_value();
float value = (60000.0f * raw) / float(this->get_update_interval()); // per minute
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
this->publish_state(value);
uint32_t now = millis();
if (this->last_time_ != 0) {
uint32_t interval = now - this->last_time_;
float value = (60000.0f * raw) / float(interval); // per minute
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
this->publish_state(value);
}
if (this->total_sensor_ != nullptr) {
current_total_ += raw;
ESP_LOGD(TAG, "'%s': Total : %i pulses", this->get_name().c_str(), current_total_);
this->total_sensor_->publish_state(current_total_);
}
this->last_time_ = now;
}
} // namespace pulse_counter

View File

@@ -65,7 +65,8 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
protected:
InternalGPIOPin *pin_;
PulseCounterStorage storage_;
uint32_t current_total_ = 0;
uint32_t last_time_{0};
uint32_t current_total_{0};
sensor::Sensor *total_sensor_;
};