ESPHome 2025.5.0
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 this->sensor_->add_on_state_callback([this](float state) {
12 // control may have changed, recompute
13 this->compute_state_();
14 // current temperature changed, publish state
15 this->publish_state();
16 });
17 this->current_temperature = this->sensor_->state;
18
19 // register for humidity values and get initial state
20 if (this->humidity_sensor_ != nullptr) {
21 this->humidity_sensor_->add_on_state_callback([this](float state) {
22 this->current_humidity = state;
23 this->publish_state();
24 });
26 }
27
28 // restore set points
29 auto restore = this->restore_state_();
30 if (restore.has_value()) {
31 restore->to_call(this).perform();
32 } else {
33 // restore from defaults, change_away handles those for us
36 } else if (supports_cool_) {
38 } else if (supports_heat_) {
40 }
41 this->change_away_(false);
42 }
43}
45 if (call.get_mode().has_value())
46 this->mode = *call.get_mode();
47 if (call.get_target_temperature_low().has_value())
49 if (call.get_target_temperature_high().has_value())
51 if (call.get_preset().has_value())
53
54 this->compute_state_();
55 this->publish_state();
56}
82 if (this->mode == climate::CLIMATE_MODE_OFF) {
84 return;
85 }
86 if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature_low) ||
87 std::isnan(this->target_temperature_high)) {
88 // if any control parameters are nan, go to OFF action (not IDLE!)
90 return;
91 }
92 const bool too_cold = this->current_temperature < this->target_temperature_low;
93 const bool too_hot = this->current_temperature > this->target_temperature_high;
94
95 climate::ClimateAction target_action;
96 if (too_cold) {
97 // too cold -> enable heating if possible and enabled, else idle
98 if (this->supports_heat_ &&
100 target_action = climate::CLIMATE_ACTION_HEATING;
101 } else {
102 target_action = climate::CLIMATE_ACTION_IDLE;
103 }
104 } else if (too_hot) {
105 // too hot -> enable cooling if possible and enabled, else idle
106 if (this->supports_cool_ &&
108 target_action = climate::CLIMATE_ACTION_COOLING;
109 } else {
110 target_action = climate::CLIMATE_ACTION_IDLE;
111 }
112 } else {
113 // neither too hot nor too cold -> in range
114 if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
115 // if supports both ends and both cooling and heating enabled, go to idle action
116 target_action = climate::CLIMATE_ACTION_IDLE;
117 } else {
118 // else use current mode and don't change (hysteresis)
119 target_action = this->action;
120 }
121 }
122
123 this->switch_to_action_(target_action);
124}
126 if (action == this->action) {
127 // already in target mode
128 return;
129 }
130
133 // switching from OFF to IDLE or vice-versa
134 // these only have visual difference. OFF means user manually disabled,
135 // IDLE means it's in auto mode but value is in target range.
136 this->action = action;
137 this->publish_state();
138 return;
139 }
140
141 if (this->prev_trigger_ != nullptr) {
142 this->prev_trigger_->stop_action();
143 this->prev_trigger_ = nullptr;
144 }
145 Trigger<> *trig;
146 switch (action) {
149 trig = this->idle_trigger_;
150 break;
152 trig = this->cool_trigger_;
153 break;
155 trig = this->heat_trigger_;
156 break;
157 default:
158 trig = nullptr;
159 }
160 if (trig != nullptr) {
161 trig->trigger();
162 } else {
163 ESP_LOGW(TAG, "trig not set - unsupported action");
164 }
165 this->action = action;
166 this->prev_trigger_ = trig;
167 this->publish_state();
168}
180 this->normal_config_ = normal_config;
181}
183 this->supports_away_ = true;
184 this->away_config_ = away_config;
185}
187 : idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {}
188void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
189void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
192void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
194void BangBangClimate::set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
196 LOG_CLIMATE("", "Bang Bang Climate", this);
197 ESP_LOGCONFIG(TAG, " Supports HEAT: %s", YESNO(this->supports_heat_));
198 ESP_LOGCONFIG(TAG, " Supports COOL: %s", YESNO(this->supports_cool_));
199 ESP_LOGCONFIG(TAG, " Supports AWAY mode: %s", YESNO(this->supports_away_));
200 ESP_LOGCONFIG(TAG, " Default Target Temperature Low: %.2f°C", this->normal_config_.default_temperature_low);
201 ESP_LOGCONFIG(TAG, " Default Target Temperature High: %.2f°C", this->normal_config_.default_temperature_high);
202}
203
206 float default_temperature_high)
207 : default_temperature_low(default_temperature_low), default_temperature_high(default_temperature_high) {}
208
209} // namespace bang_bang
210} // namespace esphome
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:104
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:96
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.
void set_supports_heat(bool supports_heat)
climate::ClimateTraits traits() override
Return the traits of this controller.
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.
Trigger * idle_trigger_
The trigger to call when the controller should switch to idle mode.
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.
Trigger * cool_trigger_
The trigger to call when the controller should switch to cooling mode.
void switch_to_action_(climate::ClimateAction action)
Switch the climate device to the given climate mode.
Trigger * heat_trigger_
The trigger to call when the controller should switch to heating mode.
BangBangClimateTargetTempConfig normal_config_
sensor::Sensor * humidity_sensor_
The sensor used for getting the current humidity.
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:275
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:280
const optional< float > & get_target_temperature_high() const
Definition climate.cpp:276
const optional< ClimateMode > & get_mode() const
Definition climate.cpp:273
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition climate.h:182
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:189
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:179
ClimateAction action
The active state of the climate device.
Definition climate.h:176
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:395
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:208
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:329
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition climate.h:191
This class contains all static data for climate devices.
void set_supported_modes(std::set< ClimateMode > modes)
void set_supports_action(bool supports_action)
void set_supports_current_humidity(bool supports_current_humidity)
void set_supports_two_point_target_temperature(bool supports_two_point_target_temperature)
void set_supported_presets(std::set< ClimatePreset > presets)
void add_supported_mode(ClimateMode mode)
void set_supports_current_temperature(bool supports_current_temperature)
Base-class for all sensors.
Definition sensor.h:57
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:52
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
bool state
Definition fan.h:0
@ 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