diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.cpp b/esphome/components/pulse_counter/pulse_counter_sensor.cpp index ef782531c9..fee91c8422 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.cpp +++ b/esphome/components/pulse_counter/pulse_counter_sensor.cpp @@ -269,9 +269,18 @@ void PulseCounterSensor::dump_config() { void PulseCounterSensor::update() { pulse_counter_t raw = this->storage_.read_raw_value(); - uint32_t now = millis(); + timestamp_t now; +#ifdef CONF_USE_TIME + if (this->time_id_ != nullptr) { + now = this->time_id_->timestamp_now() * 1000; + } else { + now = millis(); + } +#else + now = millis(); +#endif if (this->last_time_ != 0) { - uint32_t interval = now - this->last_time_; + timestamp_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); diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.h b/esphome/components/pulse_counter/pulse_counter_sensor.h index 8eeba12ac7..4ab832e5da 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.h +++ b/esphome/components/pulse_counter/pulse_counter_sensor.h @@ -4,6 +4,10 @@ #include "esphome/core/hal.h" #include "esphome/components/sensor/sensor.h" +#ifdef USE_TIME +#include "esphome/components/time/real_time_clock.h" +#endif + #include #if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3) @@ -26,6 +30,12 @@ using pulse_counter_t = int16_t; using pulse_counter_t = int32_t; #endif +#ifdef CONF_USE_TIME +using timestamp_t = time_t; +#else +using timestamp_t = int32_t; +#endif + struct PulseCounterStorageBase { virtual bool pulse_counter_setup(InternalGPIOPin *pin) = 0; virtual pulse_counter_t read_raw_value() = 0; @@ -72,6 +82,9 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent { void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; } void set_filter_us(uint32_t filter) { storage_.filter_us = filter; } void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; } +#ifdef USE_TIME + void set_time_id(time::RealTimeClock *time_id) { time_id_ = time_id; } +#endif void set_total_pulses(uint32_t pulses); @@ -84,9 +97,12 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent { protected: InternalGPIOPin *pin_; PulseCounterStorageBase &storage_; - uint32_t last_time_{0}; + timestamp_t last_time_{0}; uint32_t current_total_{0}; sensor::Sensor *total_sensor_{nullptr}; +#ifdef USE_TIME + time::RealTimeClock *time_id_{nullptr}; +#endif }; } // namespace pulse_counter diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index cf06b5296c..5bd92ddf3e 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -4,6 +4,7 @@ import esphome.config_validation as cv from esphome import automation, pins from esphome.components import sensor from esphome.components import esp32 +from esphome.components import time from esphome.const import ( CONF_COUNT_MODE, CONF_FALLING_EDGE, @@ -12,6 +13,7 @@ from esphome.const import ( CONF_PIN, CONF_RISING_EDGE, CONF_NUMBER, + CONF_TIME_ID, CONF_TOTAL, CONF_VALUE, ICON_PULSE, @@ -125,6 +127,7 @@ CONFIG_SCHEMA = cv.All( cv.GenerateID(CONF_STORAGE_ID): cv.declare_id( "pulse_counter::PulseCounterStorageBase" ), + cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock), }, ) .extend(cv.polling_component_schema("60s")), @@ -171,6 +174,11 @@ async def to_code(config): cg.add(var.set_falling_edge_mode(count[CONF_FALLING_EDGE])) cg.add(var.set_filter_us(config[CONF_INTERNAL_FILTER])) + if CONF_TIME_ID in config: + cg.add_define("CONF_USE_TIME", True) + time_ = await cg.get_variable(config[CONF_TIME_ID]) + cg.add(var.set_time_id(time_)) + if CONF_TOTAL in config: sens = await sensor.new_sensor(config[CONF_TOTAL]) cg.add(var.set_total_sensor(sens))