mirror of
https://github.com/esphome/esphome.git
synced 2025-08-02 08:27:47 +00:00
[sensor] A little bit of filter clean-up (#9986)
This commit is contained in:
parent
0954a6185c
commit
291215909a
@ -599,7 +599,9 @@ async def throttle_filter_to_code(config, filter_id):
|
|||||||
TIMEOUT_WITH_PRIORITY_SCHEMA = cv.maybe_simple_value(
|
TIMEOUT_WITH_PRIORITY_SCHEMA = cv.maybe_simple_value(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_TIMEOUT): cv.positive_time_period_milliseconds,
|
cv.Required(CONF_TIMEOUT): cv.positive_time_period_milliseconds,
|
||||||
cv.Optional(CONF_VALUE, default="nan"): cv.ensure_list(cv.float_),
|
cv.Optional(CONF_VALUE, default="nan"): cv.Any(
|
||||||
|
cv.templatable(cv.float_), [cv.templatable(cv.float_)]
|
||||||
|
),
|
||||||
},
|
},
|
||||||
key=CONF_TIMEOUT,
|
key=CONF_TIMEOUT,
|
||||||
)
|
)
|
||||||
@ -611,6 +613,8 @@ TIMEOUT_WITH_PRIORITY_SCHEMA = cv.maybe_simple_value(
|
|||||||
TIMEOUT_WITH_PRIORITY_SCHEMA,
|
TIMEOUT_WITH_PRIORITY_SCHEMA,
|
||||||
)
|
)
|
||||||
async def throttle_with_priority_filter_to_code(config, filter_id):
|
async def throttle_with_priority_filter_to_code(config, filter_id):
|
||||||
|
if not isinstance(config[CONF_VALUE], list):
|
||||||
|
config[CONF_VALUE] = [config[CONF_VALUE]]
|
||||||
template_ = [await cg.templatable(x, [], float) for x in config[CONF_VALUE]]
|
template_ = [await cg.templatable(x, [], float) for x in config[CONF_VALUE]]
|
||||||
return cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], template_)
|
return cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], template_)
|
||||||
|
|
||||||
|
@ -225,7 +225,7 @@ optional<float> SlidingWindowMovingAverageFilter::new_value(float value) {
|
|||||||
|
|
||||||
// ExponentialMovingAverageFilter
|
// ExponentialMovingAverageFilter
|
||||||
ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at)
|
ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at)
|
||||||
: send_every_(send_every), send_at_(send_every - send_first_at), alpha_(alpha) {}
|
: alpha_(alpha), send_every_(send_every), send_at_(send_every - send_first_at) {}
|
||||||
optional<float> ExponentialMovingAverageFilter::new_value(float value) {
|
optional<float> ExponentialMovingAverageFilter::new_value(float value) {
|
||||||
if (!std::isnan(value)) {
|
if (!std::isnan(value)) {
|
||||||
if (this->first_value_) {
|
if (this->first_value_) {
|
||||||
@ -325,7 +325,7 @@ optional<float> FilterOutValueFilter::new_value(float value) {
|
|||||||
// ThrottleFilter
|
// ThrottleFilter
|
||||||
ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {}
|
ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {}
|
||||||
optional<float> ThrottleFilter::new_value(float value) {
|
optional<float> ThrottleFilter::new_value(float value) {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
if (this->last_input_ == 0 || now - this->last_input_ >= min_time_between_inputs_) {
|
if (this->last_input_ == 0 || now - this->last_input_ >= min_time_between_inputs_) {
|
||||||
this->last_input_ = now;
|
this->last_input_ = now;
|
||||||
return value;
|
return value;
|
||||||
@ -369,7 +369,7 @@ optional<float> ThrottleWithPriorityFilter::new_value(float value) {
|
|||||||
|
|
||||||
// DeltaFilter
|
// DeltaFilter
|
||||||
DeltaFilter::DeltaFilter(float delta, bool percentage_mode)
|
DeltaFilter::DeltaFilter(float delta, bool percentage_mode)
|
||||||
: delta_(delta), current_delta_(delta), percentage_mode_(percentage_mode), last_value_(NAN) {}
|
: delta_(delta), current_delta_(delta), last_value_(NAN), percentage_mode_(percentage_mode) {}
|
||||||
optional<float> DeltaFilter::new_value(float value) {
|
optional<float> DeltaFilter::new_value(float value) {
|
||||||
if (std::isnan(value)) {
|
if (std::isnan(value)) {
|
||||||
if (std::isnan(this->last_value_)) {
|
if (std::isnan(this->last_value_)) {
|
||||||
|
@ -221,11 +221,11 @@ class ExponentialMovingAverageFilter : public Filter {
|
|||||||
void set_alpha(float alpha);
|
void set_alpha(float alpha);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool first_value_{true};
|
|
||||||
float accumulator_{NAN};
|
float accumulator_{NAN};
|
||||||
|
float alpha_;
|
||||||
size_t send_every_;
|
size_t send_every_;
|
||||||
size_t send_at_;
|
size_t send_at_;
|
||||||
float alpha_;
|
bool first_value_{true};
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Simple throttle average filter.
|
/** Simple throttle average filter.
|
||||||
@ -243,9 +243,9 @@ class ThrottleAverageFilter : public Filter, public Component {
|
|||||||
float get_setup_priority() const override;
|
float get_setup_priority() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint32_t time_period_;
|
|
||||||
float sum_{0.0f};
|
float sum_{0.0f};
|
||||||
unsigned int n_{0};
|
unsigned int n_{0};
|
||||||
|
uint32_t time_period_;
|
||||||
bool have_nan_{false};
|
bool have_nan_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -378,8 +378,8 @@ class DeltaFilter : public Filter {
|
|||||||
protected:
|
protected:
|
||||||
float delta_;
|
float delta_;
|
||||||
float current_delta_;
|
float current_delta_;
|
||||||
bool percentage_mode_;
|
|
||||||
float last_value_{NAN};
|
float last_value_{NAN};
|
||||||
|
bool percentage_mode_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class OrFilter : public Filter {
|
class OrFilter : public Filter {
|
||||||
@ -401,8 +401,8 @@ class OrFilter : public Filter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector<Filter *> filters_;
|
std::vector<Filter *> filters_;
|
||||||
bool has_value_{false};
|
|
||||||
PhiNode phi_;
|
PhiNode phi_;
|
||||||
|
bool has_value_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
class CalibrateLinearFilter : public Filter {
|
class CalibrateLinearFilter : public Filter {
|
||||||
|
@ -135,10 +135,17 @@ sensor:
|
|||||||
- throttle: 1s
|
- throttle: 1s
|
||||||
- throttle_average: 2s
|
- throttle_average: 2s
|
||||||
- throttle_with_priority: 5s
|
- throttle_with_priority: 5s
|
||||||
|
- throttle_with_priority:
|
||||||
|
timeout: 3s
|
||||||
|
value: 42.0
|
||||||
|
- throttle_with_priority:
|
||||||
|
timeout: 3s
|
||||||
|
value: !lambda return 1.0f / 2.0f;
|
||||||
- throttle_with_priority:
|
- throttle_with_priority:
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
value:
|
value:
|
||||||
- 42.0
|
- 42.0
|
||||||
|
- !lambda return 2.0f / 2.0f;
|
||||||
- nan
|
- nan
|
||||||
- timeout:
|
- timeout:
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
|
Loading…
x
Reference in New Issue
Block a user