ESPHome 2026.3.3
Loading...
Searching...
No Matches
bang_bang_climate.cpp
Go to the documentation of this file.
1#include "bang_bang_climate.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace bang_bang {
6
7static const char *const TAG = "bang_bang.climate";
8
10
12 this->sensor_->add_on_state_callback([this](float state) {
14 // control may have changed, recompute
15 this->compute_state_();
16 // current temperature changed, publish state
17 this->publish_state();
18 });
19 this->current_temperature = this->sensor_->state;
20
21 // register for humidity values and get initial state
22 if (this->humidity_sensor_ != nullptr) {
23 this->humidity_sensor_->add_on_state_callback([this](float state) {
24 this->current_humidity = state;
25 this->publish_state();
26 });
28 }
29
30 // restore set points
31 auto restore = this->restore_state_();
32 if (restore.has_value()) {
33 restore->to_call(this).perform();
34 } else {
35 // restore from defaults, change_away handles those for us
36 if (this->supports_cool_ && this->supports_heat_) {
38 } else if (this->supports_cool_) {
40 } else if (this->supports_heat_) {
42 }
43 this->change_away_(false);
44 }
45}
46
48 auto mode = call.get_mode();
49 if (mode.has_value()) {
50 this->mode = *mode;
51 }
53 if (target_temperature_low.has_value()) {
55 }
57 if (target_temperature_high.has_value()) {
59 }
60 auto preset = call.get_preset();
61 if (preset.has_value()) {
63 }
64
65 this->compute_state_();
66 this->publish_state();
67}
68
96
98 if (this->mode == climate::CLIMATE_MODE_OFF) {
100 return;
101 }
102 if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature_low) ||
103 std::isnan(this->target_temperature_high)) {
104 // if any control parameters are nan, go to OFF action (not IDLE!)
106 return;
107 }
108 const bool too_cold = this->current_temperature < this->target_temperature_low;
109 const bool too_hot = this->current_temperature > this->target_temperature_high;
110
111 climate::ClimateAction target_action;
112 if (too_cold) {
113 // too cold -> enable heating if possible and enabled, else idle
114 if (this->supports_heat_ &&
116 target_action = climate::CLIMATE_ACTION_HEATING;
117 } else {
118 target_action = climate::CLIMATE_ACTION_IDLE;
119 }
120 } else if (too_hot) {
121 // too hot -> enable cooling if possible and enabled, else idle
122 if (this->supports_cool_ &&
124 target_action = climate::CLIMATE_ACTION_COOLING;
125 } else {
126 target_action = climate::CLIMATE_ACTION_IDLE;
127 }
128 } else {
129 // neither too hot nor too cold -> in range
130 if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
131 // if supports both ends and both cooling and heating enabled, go to idle action
132 target_action = climate::CLIMATE_ACTION_IDLE;
133 } else {
134 // else use current mode and don't change (hysteresis)
135 target_action = this->action;
136 }
137 }
138
139 this->switch_to_action_(target_action);
140}
141
143 if (action == this->action) {
144 // already in target mode
145 return;
146 }
147
150 // switching from OFF to IDLE or vice-versa
151 // these only have visual difference. OFF means user manually disabled,
152 // IDLE means it's in auto mode but value is in target range.
153 this->action = action;
154 this->publish_state();
155 return;
156 }
157
158 if (this->prev_trigger_ != nullptr) {
159 this->prev_trigger_->stop_action();
160 this->prev_trigger_ = nullptr;
161 }
162 Trigger<> *trig;
163 switch (action) {
166 trig = &this->idle_trigger_;
167 break;
169 trig = &this->cool_trigger_;
170 break;
172 trig = &this->heat_trigger_;
173 break;
174 default:
175 trig = nullptr;
176 }
177 if (trig != nullptr) {
178 trig->trigger();
179 } else {
180 ESP_LOGW(TAG, "trig not set - unsupported action");
181 }
182 this->action = action;
183 this->prev_trigger_ = trig;
184 this->publish_state();
185}
186
197
199 this->normal_config_ = normal_config;
200}
201
203 this->supports_away_ = true;
204 this->away_config_ = away_config;
205}
206
207void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
208void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
209
213
214void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
215void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
216
218 LOG_CLIMATE("", "Bang Bang Climate", this);
219 ESP_LOGCONFIG(TAG,
220 " Supports HEAT: %s\n"
221 " Supports COOL: %s\n"
222 " Supports AWAY mode: %s\n"
223 " Default Target Temperature Low: %.2f°C\n"
224 " Default Target Temperature High: %.2f°C",
225 YESNO(this->supports_heat_), YESNO(this->supports_cool_), YESNO(this->supports_away_),
226 this->normal_config_.default_temperature_low, this->normal_config_.default_temperature_high);
227}
228
231 float default_temperature_high)
232 : default_temperature_low(default_temperature_low), default_temperature_high(default_temperature_high) {}
233
234} // namespace bang_bang
235} // namespace esphome
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:325
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:333
void compute_state_()
Re-compute the state of this climate controller.
BangBangClimateTargetTempConfig away_config_
void set_away_config(const BangBangClimateTargetTempConfig &away_config)
bool supports_cool_
Whether the controller supports cooling/heating.
void set_supports_heat(bool supports_heat)
climate::ClimateTraits traits() override
Return the traits of this controller.
Trigger idle_trigger_
The trigger to call when the controller should switch to idle mode.
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
void set_sensor(sensor::Sensor *sensor)
void set_supports_cool(bool supports_cool)
void change_away_(bool away)
Change the away setting, will reset target temperatures to defaults.
void set_normal_config(const BangBangClimateTargetTempConfig &normal_config)
Trigger * prev_trigger_
A reference to the trigger that was previously active.
sensor::Sensor * sensor_
The sensor used for getting the current temperature.
void switch_to_action_(climate::ClimateAction action)
Switch the climate device to the given climate mode.
Trigger cool_trigger_
The trigger to call when the controller should switch to cooling mode.
BangBangClimateTargetTempConfig normal_config_
sensor::Sensor * humidity_sensor_
The sensor used for getting the current humidity.
Trigger heat_trigger_
The trigger to call when the controller should switch to heating mode.
void set_humidity_sensor(sensor::Sensor *humidity_sensor)
This class is used to encode all control actions on a climate device.
Definition climate.h:33
const optional< float > & get_target_temperature_low() const
Definition climate.cpp:308
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:315
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:309
ClimateMode mode
The active mode of the climate device.
Definition climate.h:266
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:243
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:250
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:240
ClimateAction action
The active state of the climate device.
Definition climate.h:269
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:445
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:263
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:370
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:252
void add_feature_flags(uint32_t feature_flags)
void set_supported_presets(ClimatePresetMask presets)
void set_supported_modes(ClimateModeMask modes)
void add_supported_mode(ClimateMode mode)
Base-class for all sensors.
Definition sensor.h:47
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.cpp:82
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:125
bool state
Definition fan.h:2
@ CLIMATE_SUPPORTS_CURRENT_HUMIDITY
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE
@ CLIMATE_PRESET_AWAY
Device is in away preset.
@ CLIMATE_PRESET_HOME
Device is in home preset.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
@ CLIMATE_ACTION_OFF
The climate device is off (inactive or no power)
@ CLIMATE_ACTION_IDLE
The climate device is idle (monitoring climate but no action needed)
@ CLIMATE_ACTION_HEATING
The climate device is actively heating.
@ CLIMATE_ACTION_COOLING
The climate device is actively cooling.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7