mirror of
https://github.com/esphome/esphome.git
synced 2025-01-31 10:10:56 +00:00
Add a simple 'skip_initial' filter (#4582)
* Add a simple 'skip' filter This filter simply skips the first `send_first_at` values, then passes everything as-is. This is quite useful when you know the first few sensor readings should be ignored. Example YAML: ```yaml sensor: - platform: sgp30 id: mysensor_sgp30 eco2: id: mysensor_sgp30_co2 name: "eCO₂" accuracy_decimals: 0 filters: - skip: send_first_at: 41 ``` * Rename the filter to `skip_initial` and simplify the schema New usage: ```yaml filters: - skip_initial: 41 ``` * Apply clang-format
This commit is contained in:
parent
5207ca1d52
commit
48658d5a55
@ -194,6 +194,7 @@ SensorPublishAction = sensor_ns.class_("SensorPublishAction", automation.Action)
|
|||||||
Filter = sensor_ns.class_("Filter")
|
Filter = sensor_ns.class_("Filter")
|
||||||
QuantileFilter = sensor_ns.class_("QuantileFilter", Filter)
|
QuantileFilter = sensor_ns.class_("QuantileFilter", Filter)
|
||||||
MedianFilter = sensor_ns.class_("MedianFilter", Filter)
|
MedianFilter = sensor_ns.class_("MedianFilter", Filter)
|
||||||
|
SkipInitialFilter = sensor_ns.class_("SkipInitialFilter", Filter)
|
||||||
MinFilter = sensor_ns.class_("MinFilter", Filter)
|
MinFilter = sensor_ns.class_("MinFilter", Filter)
|
||||||
MaxFilter = sensor_ns.class_("MaxFilter", Filter)
|
MaxFilter = sensor_ns.class_("MaxFilter", Filter)
|
||||||
SlidingWindowMovingAverageFilter = sensor_ns.class_(
|
SlidingWindowMovingAverageFilter = sensor_ns.class_(
|
||||||
@ -365,6 +366,11 @@ MIN_SCHEMA = cv.All(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@FILTER_REGISTRY.register("skip_initial", SkipInitialFilter, cv.positive_not_null_int)
|
||||||
|
async def skip_initial_filter_to_code(config, filter_id):
|
||||||
|
return cg.new_Pvariable(filter_id, config)
|
||||||
|
|
||||||
|
|
||||||
@FILTER_REGISTRY.register("min", MinFilter, MIN_SCHEMA)
|
@FILTER_REGISTRY.register("min", MinFilter, MIN_SCHEMA)
|
||||||
async def min_filter_to_code(config, filter_id):
|
async def min_filter_to_code(config, filter_id):
|
||||||
return cg.new_Pvariable(
|
return cg.new_Pvariable(
|
||||||
|
@ -74,6 +74,19 @@ optional<float> MedianFilter::new_value(float value) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SkipInitialFilter
|
||||||
|
SkipInitialFilter::SkipInitialFilter(size_t num_to_ignore) : num_to_ignore_(num_to_ignore) {}
|
||||||
|
optional<float> SkipInitialFilter::new_value(float value) {
|
||||||
|
if (num_to_ignore_ > 0) {
|
||||||
|
num_to_ignore_--;
|
||||||
|
ESP_LOGV(TAG, "SkipInitialFilter(%p)::new_value(%f) SKIPPING, %u left", this, value, num_to_ignore_);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGV(TAG, "SkipInitialFilter(%p)::new_value(%f) SENDING", this, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
// QuantileFilter
|
// QuantileFilter
|
||||||
QuantileFilter::QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile)
|
QuantileFilter::QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile)
|
||||||
: send_every_(send_every), send_at_(send_every - send_first_at), window_size_(window_size), quantile_(quantile) {}
|
: send_every_(send_every), send_at_(send_every - send_first_at), window_size_(window_size), quantile_(quantile) {}
|
||||||
|
@ -102,6 +102,24 @@ class MedianFilter : public Filter {
|
|||||||
size_t window_size_;
|
size_t window_size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Simple skip filter.
|
||||||
|
*
|
||||||
|
* Skips the first N values, then passes everything else.
|
||||||
|
*/
|
||||||
|
class SkipInitialFilter : public Filter {
|
||||||
|
public:
|
||||||
|
/** Construct a SkipInitialFilter.
|
||||||
|
*
|
||||||
|
* @param num_to_ignore How many values to ignore before the filter becomes a no-op.
|
||||||
|
*/
|
||||||
|
explicit SkipInitialFilter(size_t num_to_ignore);
|
||||||
|
|
||||||
|
optional<float> new_value(float value) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
size_t num_to_ignore_;
|
||||||
|
};
|
||||||
|
|
||||||
/** Simple min filter.
|
/** Simple min filter.
|
||||||
*
|
*
|
||||||
* Takes the min of the last <send_every> values and pushes it out every <send_every>.
|
* Takes the min of the last <send_every> values and pushes it out every <send_every>.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user