From a437d22b7abbe488b055d4062317ea51e640eb7d Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 09:49:31 +0100 Subject: [PATCH] Fix flex filter excluding valid low-price intervals in best price periods (#68) Fixed bug in best price flex filter that incorrectly excluded prices when checking for periods. The filter was requiring price >= daily_min, which is unnecessary and could theoretically exclude valid low prices. Changed from: in_flex = price >= criteria.ref_price and price <= flex_threshold To: in_flex = price <= flex_threshold This ensures all low prices up to the threshold are included in best price period consideration, matching the expected behavior described in the period calculation documentation. The fix addresses the user's observation that qualifying intervals appearing after the daily minimum in chronological order should be included if they meet the flex criteria. --- .../coordinator/period_handlers/level_filtering.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/custom_components/tibber_prices/coordinator/period_handlers/level_filtering.py b/custom_components/tibber_prices/coordinator/period_handlers/level_filtering.py index bcd783b..9714942 100644 --- a/custom_components/tibber_prices/coordinator/period_handlers/level_filtering.py +++ b/custom_components/tibber_prices/coordinator/period_handlers/level_filtering.py @@ -155,9 +155,12 @@ def check_interval_criteria( in_flex = price >= flex_threshold else: # Best price: accept prices <= (ref_price + flex_amount) - # Prices must be CLOSE TO or AT the minimum + # Accept ALL low prices up to the flex threshold, not just those >= minimum + # This ensures that if there are multiple low-price intervals, all that meet + # the threshold are included, regardless of whether they're before or after + # the daily minimum in the chronological sequence. flex_threshold = criteria.ref_price + flex_amount - in_flex = price >= criteria.ref_price and price <= flex_threshold + in_flex = price <= flex_threshold # ============================================================ # MIN_DISTANCE FILTER: Check if price is far enough from average