diff --git a/esphome/components/binary_sensor_map/binary_sensor_map.cpp b/esphome/components/binary_sensor_map/binary_sensor_map.cpp index b2dffaf74d..3934e0a99c 100644 --- a/esphome/components/binary_sensor_map/binary_sensor_map.cpp +++ b/esphome/components/binary_sensor_map/binary_sensor_map.cpp @@ -30,7 +30,7 @@ void BinarySensorMap::process_group_() { if (bs.binary_sensor->state) { num_active_sensors++; total_current_value += bs.sensor_value; - mask |= 1 << i; + mask |= 1ULL << i; } } // check if the sensor map was touched @@ -38,12 +38,11 @@ void BinarySensorMap::process_group_() { // did the bit_mask change or is it a new sensor touch if (this->last_mask_ != mask) { float publish_value = total_current_value / num_active_sensors; - ESP_LOGD(TAG, "'%s' - Publishing %.2f", this->name_.c_str(), publish_value); this->publish_state(publish_value); } } else if (this->last_mask_ != 0ULL) { // is this a new sensor release - ESP_LOGD(TAG, "'%s' - No binary sensor active, publishing NAN", this->name_.c_str()); + ESP_LOGV(TAG, "'%s' - No binary sensor active, publishing NAN", this->name_.c_str()); this->publish_state(NAN); } this->last_mask_ = mask; @@ -52,28 +51,22 @@ void BinarySensorMap::process_group_() { void BinarySensorMap::process_sum_() { float total_current_value = 0.0; uint64_t mask = 0x00; - // check all binary_sensors for its state. when active add its value to total_current_value. - // create a bitmask for the binary_sensor status on all channels + // - check all binary_sensor states + // - if active, add its value to total_current_value + // - creates a bitmask for the binary_sensor status on all channels for (size_t i = 0; i < this->channels_.size(); i++) { auto bs = this->channels_[i]; if (bs.binary_sensor->state) { total_current_value += bs.sensor_value; - mask |= 1 << i; + mask |= 1ULL << i; } } - // check if the sensor map was touched - if (mask != 0ULL) { - // did the bit_mask change or is it a new sensor touch - if (this->last_mask_ != mask) { - float publish_value = total_current_value; - ESP_LOGD(TAG, "'%s' - Publishing %.2f", this->name_.c_str(), publish_value); - this->publish_state(publish_value); - } - } else if (this->last_mask_ != 0ULL) { - // is this a new sensor release - ESP_LOGD(TAG, "'%s' - No binary sensor active, publishing 0", this->name_.c_str()); - this->publish_state(0.0); + + // update state only if the binary sensor states have changed or if no state has ever been sent on boot + if ((this->last_mask_ != mask) || (!this->has_state())) { + this->publish_state(total_current_value); } + this->last_mask_ = mask; } diff --git a/esphome/components/binary_sensor_map/sensor.py b/esphome/components/binary_sensor_map/sensor.py index 025be490cd..573cce9223 100644 --- a/esphome/components/binary_sensor_map/sensor.py +++ b/esphome/components/binary_sensor_map/sensor.py @@ -39,7 +39,7 @@ CONFIG_SCHEMA = cv.typed_schema( ).extend( { cv.Required(CONF_CHANNELS): cv.All( - cv.ensure_list(entry), cv.Length(min=1) + cv.ensure_list(entry), cv.Length(min=1, max=64) ), } ), @@ -50,7 +50,7 @@ CONFIG_SCHEMA = cv.typed_schema( ).extend( { cv.Required(CONF_CHANNELS): cv.All( - cv.ensure_list(entry), cv.Length(min=1) + cv.ensure_list(entry), cv.Length(min=1, max=64) ), } ),