From 1bec51d1ab2f964c3cfb4b15226b7de8e249a635 Mon Sep 17 00:00:00 2001 From: ErikDB87 <148375835+ErikDB87@users.noreply.github.com> Date: Wed, 24 Jun 2026 17:58:04 +0200 Subject: [PATCH] [shelly] Add BLU packet threshhold and fix typo (#21031) If a BLE device is restarted, its PID counter starts anew. Signed-off-by: Erik De Boeck --- .../binding/shelly/internal/api2/ShellyBluApi.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java index 7ed4d2f544..ae1f674b88 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java @@ -17,6 +17,7 @@ import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*; import static org.openhab.binding.shelly.internal.api2.ShellyBluJsonDTO.*; import static org.openhab.binding.shelly.internal.util.ShellyUtils.*; +import java.time.Instant; import java.util.concurrent.ScheduledExecutorService; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -73,7 +74,9 @@ public class ShellyBluApi extends Shelly2ApiRpc { private boolean connected; // true = BLU devices has connected private ShellySettingsStatus deviceStatus = new ShellySettingsStatus(); private int lastPid = -1; - private static final int PID_CYCLE_TRESHHOLD = 50; + private static final int PID_CYCLE_TRESHOLD = 50; + private long lastTimeStampPacket = 0; + private static final int PACKET_TIMESTAMP_TRESHOLD = 10; /** * Regular constructor - called by Thing handler @@ -192,16 +195,22 @@ public class ShellyBluApi extends Shelly2ApiRpc { logger.debug("{}: BLU event {} received from address {}, pid={} (JSON={})", thingName, event, getString(e.blu.addr), getInteger(e.blu.pid), eventJSON); if (e.blu.pid != null) { + long epochNow = Instant.now().getEpochSecond(); int pid = e.blu.pid; - if (lastPid != -1 && pid < (lastPid - PID_CYCLE_TRESHHOLD)) { + if (lastPid != -1 && pid < (lastPid - PID_CYCLE_TRESHOLD)) { logger.debug( "{}: Received pid {} is so low that a new cycle has probably begun since lastPID={}", thingName, pid, lastPid); + } else if (pid <= lastPid && epochNow - lastTimeStampPacket > PACKET_TIMESTAMP_TRESHOLD) { + logger.debug( + "{}: Received pid {} is too low, but received more than {} sec. after lastPID={}. A new cycle has thus probably begun", + thingName, pid, PACKET_TIMESTAMP_TRESHOLD, lastPid); } else if (pid <= lastPid) { logger.debug("{}: Duplicate packet for pid {} received, ignore", thingName, pid); break; } lastPid = pid; + lastTimeStampPacket = epochNow; } getThing().getProfile().gateway = message.src; }