diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.cpp b/esphome/components/pulse_counter/pulse_counter_sensor.cpp index c71e51eb32..46b50a3021 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.cpp +++ b/esphome/components/pulse_counter/pulse_counter_sensor.cpp @@ -158,6 +158,12 @@ void PulseCounterSensor::update() { 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_); + } } #ifdef ARDUINO_ARCH_ESP32 diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.h b/esphome/components/pulse_counter/pulse_counter_sensor.h index 483036ac34..b3e3f42c01 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.h +++ b/esphome/components/pulse_counter/pulse_counter_sensor.h @@ -54,6 +54,7 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent { void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; } 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; } /// Unit of measurement is "pulses/min". void setup() override; @@ -64,6 +65,8 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent { protected: GPIOPin *pin_; PulseCounterStorage storage_; + uint32_t current_total_ = 0; + sensor::Sensor *total_sensor_; }; #ifdef ARDUINO_ARCH_ESP32 diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index 61d3f3d5b5..7550d5693a 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -3,8 +3,8 @@ import esphome.config_validation as cv from esphome import pins from esphome.components import sensor from esphome.const import CONF_COUNT_MODE, CONF_FALLING_EDGE, CONF_ID, CONF_INTERNAL_FILTER, \ - CONF_PIN, CONF_RISING_EDGE, CONF_NUMBER, \ - ICON_PULSE, UNIT_PULSES_PER_MINUTE + CONF_PIN, CONF_RISING_EDGE, CONF_NUMBER, CONF_TOTAL, \ + ICON_PULSE, UNIT_PULSES_PER_MINUTE, UNIT_PULSES from esphome.core import CORE pulse_counter_ns = cg.esphome_ns.namespace('pulse_counter') @@ -58,6 +58,8 @@ CONFIG_SCHEMA = sensor.sensor_schema(UNIT_PULSES_PER_MINUTE, ICON_PULSE, 2).exte cv.Required(CONF_FALLING_EDGE): COUNT_MODE_SCHEMA, }), validate_count_mode), cv.Optional(CONF_INTERNAL_FILTER, default='13us'): validate_internal_filter, + cv.Optional(CONF_TOTAL): sensor.sensor_schema(UNIT_PULSES, ICON_PULSE, 0), + }).extend(cv.polling_component_schema('60s')) @@ -72,3 +74,7 @@ def to_code(config): cg.add(var.set_rising_edge_mode(count[CONF_RISING_EDGE])) cg.add(var.set_falling_edge_mode(count[CONF_FALLING_EDGE])) cg.add(var.set_filter_us(config[CONF_INTERNAL_FILTER])) + + if CONF_TOTAL in config: + sens = yield sensor.new_sensor(config[CONF_TOTAL]) + cg.add(var.set_total_sensor(sens)) diff --git a/esphome/const.py b/esphome/const.py index d8a8ca9905..4d45d68839 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -525,6 +525,7 @@ CONF_TO = 'to' CONF_TOLERANCE = 'tolerance' CONF_TOPIC = 'topic' CONF_TOPIC_PREFIX = 'topic_prefix' +CONF_TOTAL = "total" CONF_TRANSITION_LENGTH = 'transition_length' CONF_TRIGGER_ID = 'trigger_id' CONF_TRIGGER_PIN = 'trigger_pin' @@ -641,6 +642,7 @@ UNIT_OHM = 'Ω' UNIT_PARTS_PER_BILLION = 'ppb' UNIT_PARTS_PER_MILLION = 'ppm' UNIT_PERCENT = '%' +UNIT_PULSES = "pulses" UNIT_PULSES_PER_MINUTE = 'pulses/min' UNIT_SECOND = 's' UNIT_STEPS = 'steps'