Repository navigation
Conversation
The On/Off cluster already accepted the standard `on_with_timed_off` command (0x42) at the stack level, but the callback ignored it — the relay stayed on forever. This adds a working countdown. Two independent bugs had to be fixed: 1. The Telink `clock_time()` register is 32 bits at 16 MHz, so `hal_millis()` wraps every ~268 s. A `uint32_t` deadline therefore stops matching after the first wrap: a timer set at t=270 s is compared against a `now` that has already wrapped back to 6.4 s and is then reported as ~263 s in the future. Added `hal_millis_64()`, which tracks wraparounds in a high-word counter, and store the deadline as `uint64_t`. Compare with unsigned `>=` — tc32-gcc miscompiles the signed `(int64_t)(now - deadline) >= 0` form and fires the timer on the first poll (~80 ms) instead of at on_time. 2. `relay_on()` fires the `on_change` callback, which cancels any pending auto-OFF. Arming the timer before driving the relay meant the same handler cancelled the timer it had just installed. Drive the relay first, install the deadline after, so the callback can only ever clear a *previous* timer. The timer is polled from `app_task()` via `relay_cluster_task()` rather than scheduled as an `ev_timer`: on end-device builds the core sleeps between wakeups and long delays do not fire reliably. As a consequence `on_with_timed_off` turns the relay on but arms no timer on end devices — the payload is still acknowledged, so this is a silent no-op rather than a malformed-command error. This also removes the unsolicited periodic `attributeReport` that heartbeat polling used to emit; the auto-OFF now shows up over the air as exactly one report, `OnOff=0`, at on_time. Covered by tests/test_relay_cluster_on_with_timed_off.py (19 cases): LE payload decoding, the arm/cancel race, multi-endpoint dispatch, and deadline evaluation across wraparound boundaries.
The file's own header asks for work-in-progress entries to go under '## Upcoming', and that section did not exist yet. Records the new on_with_timed_off behaviour and links PR #540 for the implementation detail rather than restating it here.
|
@andrei-lazarov please pay attention 🙏🏻 |
|
A pity that this is not implemented. Would like to see this function, to use together with the Ikea zigbee motion controllers properly. |
@hawaltie hi! |
|
@mrmaximas will you share the "second, tested" version with us, please? |
|
@falkenhawk I was editing some code for Telink, but I don't have any Silabs devices to test it on. What devices do you have? |
|
@mrmaximas: hi! I have a AVATTO ZWSM16-1-Zigbee switch, so almost the same switch you are using, but with a neutral. Already running the current custom switch firmware. |
|
@hawaltie I've modified two versions of the AVATTO LZWSM16-2 |
|
Thanks. And take it easy |
Summary
Implements the ZCL
on_with_timed_offcountdown (command0x42) for relay endpoints. The command was already accepted by the Telink stack but ignored by the cluster callback, so the relay stayed on indefinitely. Zigbee2MQTT, Home Assistant and any other controller can now schedule an auto-off without a wall-clock timer on the device side.Also removes the unsolicited periodic
attributeReportthat heartbeat polling emitted — the auto-off is now visible over the air as exactly one report,OnOff=0, aton_time.Two bugs fixed
1.
hal_millis()wraparound.clock_time()is a 32-bit register at 16 MHz, sohal_millis()wraps every ~268 s. Auint32_tdeadline silently stops matching after the first wrap: a timer armed at t=270 s is compared against anowthat has already wrapped back to 6.4 s, and the signed difference reads as ~263 s in the future. The timer then never fires for the rest of the uptime.Added
hal_millis_64(), which accumulates wraparounds in a high-word counter, and store the deadline asuint64_t. The comparison uses unsigned>=— tc32-gcc miscompiles the signed(int64_t)(now - deadline) >= 0form into asubcspattern that misfires the carry flag whena < b, firing the timer on the first poll (~80 ms) instead of aton_time.2. Arm/cancel race.
relay_on()fires theon_changecallback, which cancels any pending auto-off. Arming the timer before driving the relay meant the same handler cancelled the timer it had just installed — the relay turned on, reportedOnOff=1, and never turned off. The fix is to drive the relay first and install the deadline after, so the callback can only ever clear a previous timer.Polling rather than
ev_timerThe deadline is polled from
app_task()via a newrelay_cluster_task(). On end-device builds the core sleeps between wakeups and long delays do not fire reliably, so a scheduledev_timercannot carry a multi-second countdown; a polled deadline read fromhal_millis_64()survives that because the hardware counter ticks whether or not the CPU is awake.A consequence worth calling out: on end devices
on_with_timed_offturns the relay on but arms no timer. The payload is still acknowledged normally, so this is a silent no-op rather than a malformed-command error.Notes for review
p[1] | (p[2] << 8)), not via a struct cast.zcl_onoff_onWithTimeOffCmd_tembeds a union over a bitfield; the x86_64 host ABI gives that bitfield 4 bytes while tc32 gives it 1, so the struct is 8 bytes on the host and 5 on the device. The struct cast reads from the wrong offset in the stub tests and cannot be made correct for both with__attribute__((packed)).relay_cluster.cand is not board-specific.Tests
tests/test_relay_cluster_on_with_timed_off.py, 19 cases: LE payload decoding for a range ofon_time/off_wait_time, the arm/cancel ordering, multi-endpoint dispatch, deadline evaluation across wraparound boundaries, and cancellation by a subsequent ZCL command. Full suite: 250 passed.Verified on hardware
LZWSM16-2 (
_TZ3000_ljhbw1c9), router build. On a packet capture,onWithTimedOffwithontime=50producesdefaultRsp status=0, thenOnOff=1, then a singleattributeReport OnOff=0at exactly 5.0 s with nothing in between.ZWSM16-2 (
_TZ3000_mtnpt6ws) builds and installs, but has not been exercised on hardware yet.