pulse_counter_ulp: avoid zero

This commit is contained in:
brisk 2024-07-27 14:39:33 +09:30
parent c882b58e53
commit bd9029c2b2

View file

@ -136,8 +136,8 @@ void PulseCounterUlpSensor::dump_config() {
void PulseCounterUlpSensor::update() { void PulseCounterUlpSensor::update() {
UlpProgram::state raw = this->storage_.pop_state(); UlpProgram::state raw = this->storage_.pop_state();
clock::time_point now = clock::now(); clock::time_point now = clock::now();
auto interval = now - this->last_time_; clock::duration interval = now - this->last_time_;
if (this->last_time_ != clock::time_point{}) { if (interval != clock::duration::zero()) {
float value = std::chrono::minutes{1} * static_cast<float>(raw.edge_count) / interval; // pulses per minute float value = std::chrono::minutes{1} * static_cast<float>(raw.edge_count) / interval; // pulses per minute
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value); ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
this->publish_state(value); this->publish_state(value);