ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
nextion_sensor.cpp
Go to the documentation of this file.
1#include "nextion_sensor.h"
2#include "esphome/core/util.h"
3#include "esphome/core/log.h"
4
5namespace esphome::nextion {
6
7static const char *const TAG = "nextion_sensor";
8
9void NextionSensor::process_sensor(const std::string &variable_name, int state) {
10 if (!this->nextion_->is_setup())
11 return;
12
13#ifdef USE_NEXTION_WAVEFORM
14 if (this->wave_chan_id_ == UINT8_MAX && this->variable_name_ == variable_name)
15#else // USE_NEXTION_WAVEFORM
16 if (this->variable_name_ == variable_name)
17#endif // USE_NEXTION_WAVEFORM
18 {
19 this->publish_state(state);
20 ESP_LOGD(TAG, "Sensor: %s=%d", variable_name.c_str(), state);
21 }
22}
23
24#ifdef USE_NEXTION_WAVEFORM
26 this->needs_to_send_update_ = true;
27 int wave_state = (int) ((state / (float) this->wave_maxvalue_) * 100);
28 this->wave_buffer_.push_back(wave_state);
29 if (this->wave_buffer_.size() > (size_t) this->wave_max_length_) {
30 this->wave_buffer_.erase(this->wave_buffer_.begin());
31 }
32}
33#endif // USE_NEXTION_WAVEFORM
34
36 if (!this->nextion_->is_setup() || this->nextion_->is_updating())
37 return;
38
39#ifdef USE_NEXTION_WAVEFORM
40 if (this->wave_chan_id_ == UINT8_MAX) {
41 this->nextion_->add_to_get_queue(this);
42 } else {
43 if (this->send_last_value_) {
45 }
46 this->wave_update_();
47 }
48#else // USE_NEXTION_WAVEFORM
49 this->nextion_->add_to_get_queue(this);
50#endif // USE_NEXTION_WAVEFORM
51}
52
53void NextionSensor::set_state(float state, bool publish, bool send_to_nextion) {
54 if (!this->nextion_->is_setup() || this->nextion_->is_updating())
55 return;
56
57 if (std::isnan(state))
58 return;
59
60#ifdef USE_NEXTION_WAVEFORM
61 if (this->wave_chan_id_ != UINT8_MAX) {
62 // Waveform sensor — buffer the value, don't send directly.
63 if (this->send_last_value_) {
64 this->last_value_ = state; // Update will handle setting the buffer
65 } else {
66 this->add_to_wave_buffer(state);
67 }
69 return;
70 }
71#endif // USE_NEXTION_WAVEFORM
72
73 if (send_to_nextion) {
74 if (this->nextion_->is_sleeping() || !this->component_flags_.visible) {
75 this->needs_to_send_update_ = true;
76 } else {
77 this->needs_to_send_update_ = false;
78 if (this->precision_ > 0) {
79 double to_multiply = pow(10, this->precision_);
80 int state_value = (int) (state * to_multiply);
81 this->nextion_->add_no_result_to_queue_with_set(this, (int) state_value);
82 } else {
83 this->nextion_->add_no_result_to_queue_with_set(this, (int) state);
84 }
85 }
86 }
87
88 float published_state = state;
89 if (publish) {
90 if (this->precision_ > 0) {
91 double to_multiply = pow(10, -this->precision_);
92 published_state = (float) (state * to_multiply);
93 }
94 this->publish_state(published_state);
95 }
97
98 ESP_LOGN(TAG, "Write: %s=%lf", this->variable_name_.c_str(), published_state);
99}
100
101#ifdef USE_NEXTION_WAVEFORM
103 if (this->nextion_->is_sleeping() || this->wave_buffer_.empty()) {
104 return;
105 }
106#ifdef NEXTION_PROTOCOL_LOG
107 size_t buffer_to_send =
108 this->wave_buffer_.size() < 255 ? this->wave_buffer_.size() : 255; // ADDT command can only send 255
109 ESP_LOGN(TAG, "Wave update: %zu/%zu vals to comp %d ch %d", buffer_to_send, this->wave_buffer_.size(),
110 this->component_id_, this->wave_chan_id_);
111#endif // NEXTION_PROTOCOL_LOG
113}
114#endif // USE_NEXTION_WAVEFORM
115
116} // namespace esphome::nextion
virtual void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value)=0
virtual void add_addt_command_to_queue(NextionComponentBase *component)=0
virtual void add_to_get_queue(NextionComponentBase *component)=0
void set_state(float state) override
void process_sensor(const std::string &variable_name, int state) override
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
bool state
Definition fan.h:2