From 312b62487c868bb9efd7b35a5a30d41de483e117 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sat, 8 Oct 2022 10:10:46 +0200 Subject: [PATCH] =?UTF-8?q?[linky]=20Avoid=20internal=20server=20error=20(?= =?UTF-8?q?getting=20power=20peak=20early=20in=20the=20=E2=80=A6=20(#13482?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [linky] Avoid internal server error (getting power peak early in the morning) Fix #13377 The binding now requests power peak data for 2 days (yesterday and the day before). Signed-off-by: Laurent Garnier --- .../linky/internal/handler/LinkyHandler.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java index 2b408e19986..7aa4df24467 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java @@ -105,13 +105,18 @@ public class LinkyHandler extends BaseThingHandler { }); this.cachedPowerData = new ExpiringDayCache<>("power cache", REFRESH_FIRST_HOUR_OF_DAY, () -> { - LocalDate to = LocalDate.now(); - LocalDate from = to.minusDays(1); - Consumption consumption = getPowerData(from, to); + // We request data for yesterday and the day before yesterday, even if the data for the day before yesterday + // is not needed by the binding. This is only a workaround to an API bug that will return + // INTERNAL_SERVER_ERROR rather than the expected data with a NaN value when the data for yesterday is not + // yet available. + // By requesting two days, the API is not failing and you get the expected NaN value for yesterday when the + // data is not yet available. + LocalDate today = LocalDate.now(); + Consumption consumption = getPowerData(today.minusDays(2), today); if (consumption != null) { logData(consumption.aggregats.days, "Day (peak)", true, DateTimeFormatter.ISO_LOCAL_DATE_TIME, Target.ALL); - consumption = getConsumptionAfterChecks(consumption, Target.FIRST); + consumption = getConsumptionAfterChecks(consumption, Target.LAST); } return consumption; }); @@ -199,8 +204,8 @@ public class LinkyHandler extends BaseThingHandler { if (isLinked(PEAK_POWER) || isLinked(PEAK_TIMESTAMP)) { cachedPowerData.getValue().ifPresentOrElse(values -> { Aggregate days = values.aggregats.days; - updateVAChannel(PEAK_POWER, days.datas.get(0)); - updateState(PEAK_TIMESTAMP, new DateTimeType(days.periodes.get(0).dateDebut)); + updateVAChannel(PEAK_POWER, days.datas.get(days.datas.size() - 1)); + updateState(PEAK_TIMESTAMP, new DateTimeType(days.periodes.get(days.datas.size() - 1).dateDebut)); }, () -> { updateKwhChannel(PEAK_POWER, Double.NaN); updateState(PEAK_TIMESTAMP, UnDefType.UNDEF);