diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index 27c835d38c..1cde692dd4 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -30,7 +30,11 @@ void BinarySensor::publish_initial_state(bool state) { } } void BinarySensor::send_state_internal(bool state, bool is_initial) { - ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), state ? "ON" : "OFF"); + if (is_initial) { + ESP_LOGD(TAG, "'%s': Sending initial state %s", this->get_name().c_str(), ONOFF(state)); + } else { + ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), ONOFF(state)); + } this->has_state_ = true; this->state = state; if (!is_initial) { diff --git a/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.cpp b/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.cpp index 61c73d272b..203f6d8a24 100644 --- a/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.cpp +++ b/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.cpp @@ -16,14 +16,16 @@ void HomeassistantBinarySensor::setup() { ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str()); break; case PARSE_ON: - ESP_LOGD(TAG, "'%s': Got state ON", this->entity_id_.c_str()); - this->publish_state(true); - break; case PARSE_OFF: - ESP_LOGD(TAG, "'%s': Got state OFF", this->entity_id_.c_str()); - this->publish_state(false); + bool new_state = val == PARSE_ON; + ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), ONOFF(new_state)); + if (this->initial_) + this->publish_initial_state(new_state); + else + this->publish_state(new_state); break; } + this->initial_ = false; }); } void HomeassistantBinarySensor::dump_config() { diff --git a/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.h b/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.h index c2c7ec4480..e468fd00eb 100644 --- a/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.h +++ b/esphome/components/homeassistant/binary_sensor/homeassistant_binary_sensor.h @@ -15,6 +15,7 @@ class HomeassistantBinarySensor : public binary_sensor::BinarySensor, public Com protected: std::string entity_id_; + bool initial_{true}; }; } // namespace homeassistant