Repository navigation
Conversation
This was referenced Nov 16, 2026
|
I want to try if this works, but how can i compile an image with this change? |
Author
|
@hawaltie I can compile it out—what is the exact Zigbee model of your relay? |
|
From Zigbee2MQTT: Zigbee model Is this the information you require? |
The example converter listed only the two 2-gang TS0012 variants. The 1-gang TS0001-AVB (ZWSM16-1) and the 2-gang TS0002-AVB (ZWSM16-2) build from the same firmware and expose the timer the same way, so add them to the example. The endpoint layout follows the config_str in device_db.yaml: the switch clusters come first, then the relay clusters. Also drop a duplicated block of configuration attributes, which registered the same three exposes twice per definition.
Author
|
Works! Excellent. I binded an Ikea motion detector to this device. It didn't work before, and now it just works. Nice! Thanx. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Countdown timers (ZCL On With Timed Off) for relay endpoints
What
The ZCL
On With Timed Offcommand (0x42) was already accepted by the Zigbee stack, but the OnOff cluster callback ignored it, so the relay stayed on indefinitely. This PR implements it.on_timeandoff_wait_timeare honoured, in units of 100 ms as the spec defines them.on/off/toggle/ level command. The relay then stays where the user put it.on_with_timed_offre-arms the countdown instead of cancelling it.app_task(), so no extra traffic is sent: the firmware only reports an attribute change when the relay actually switches.zigbee2mqtt/countdown_timer_example.jsis an example external converter that exposes theon_timefield. The generated converters do not expose it, so it cannot be set from the UI without it. It covers all four AVATTO variants:TS0012-avatto,TS0012-AVB1,TS0002-AVBandTS0001-AVB.The millisecond counter bug
Two bugs had to be fixed before a countdown could work at all.
clock_time()on the Telink TLSR8258 is a 32-bit counter at 16 MHz, so it wraps every 268.435456 s. The oldhal_millis()divided that raw tick by 16000 and tracked the wrap of the derived value. The derived value only spans 0..268435 ms, so the wrap counter was shifted left by 32 bits: every hardware wrap added ~49.7 days instead of the ~268 s that really elapsed. The result was not a millisecond timeline.The consequence was that a countdown was compared against a counter that jumped forward by 49.7 days at every wrap, so any timer longer than ~268 s fired at the wrap boundary instead of at its deadline. Observed on real hardware:
Both timestamps land exactly on a hardware-wrap boundary.
hal/tick64.hreplaces that with a counter that advances by the elapsed tick delta. The delta is a plainuint32subtraction, so it is the true elapsed tick count across the wrap. The sub-millisecond remainder is carried over, because a tick period is 268435.456 ms rather than a whole number of milliseconds.hal_millis()now returns the low 32 bits of the true millisecond timeline, so it wraps every ~49.7 days instead of every ~268 s. The countdown itself is compared againsthal_millis_64(), which is monotonic.Only 32-bit division is used: the tc32 toolchain has no
__udivdi3.Platform coverage
This is implemented for Telink router builds only.
HAL_HAS_TIMER_64gates the whole feature. It is defined for Telink router builds and for the host stub, and not for end device builds and not for Silabs.End device builds sleep between polls, and the system clock stops while they do, so a deadline could never be reached on time there. The feature is therefore excluded rather than shipped with a broken timer.
Silabs is not covered by this PR. We have no Silabs hardware to test on, so we did not want to ship an untested time source there. A Silabs build is unaffected and still compiles.
The Silabs side is not a hard problem, though: it needs the same monotonic 64-bit millisecond counter, and the rest of the cluster logic is shared code and applies as is.
src/silabs/hal/timer.cwould need its own counter implementation, the same waysrc/telink/hal/timer.chas one now.If you have Silabs hardware and are willing to test it, please say so. The implementation for Silabs can be provided on request - the approach is already worked out and the shared part of the code is already in this PR. It just needs a Silabs timer implementation and someone to flash it and check that the countdown behaves.
Tested on
Live hardware, with the countdown exercised over Zigbee2MQTT. Both are running router firmware:
_TZ3000_ljhbw1c9TS0012-avatto_TZ3000_kpatq5pqTS0012-AVB1The firmware for the other two AVATTO variants is built from the same source, but was not tested on live hardware:
_TZ3000_4rbqgcuvTS0001-AVB_TZ3000_mtnpt6wsTS0002-AVBTests
126 new test cases in the host stub (73 test functions, several of them parameterised):
tests/test_relay_cluster_on_with_timed_off.py- the command itself: arming, firing,off_wait_timeprecedence, the(0,0)case, byte order, per-relay independence, and a countdown that spans a hardware tick wrap.tests/test_relay_cluster_timer_reset.py- every path that cancels or replaces a pending countdown.tests/test_relay_cluster_timer_matrix.py- the cross-product of timer length, device shape, endpoint and tick wrap.tests/test_relay_cluster_button_pairing.py- which button cancels which relay's countdown, including a crossed wiring.tests/test_tick64_timer.py- the counter itself, driven with a simulated tick so the code under test is the code that ships. The test also carries the old formula, so it fails if the fix is reverted.The full suite passes: 357 tests.
Notes for the reviewer
relay_cluster_task()is called fromapp_task()and reads the clock once per loop iteration. It does not send anything on its own.on_time/off_wait_timevalues are in 100 ms units on the wire, as the spec requires. Zigbee2MQTT takes seconds and converts.device_db.yamlis not touched. Note that the upstream entries for these AVATTO boards currently saydevice_type: end_device, so a countdown needs a router build (BOARD=MODULE_AVATTO_TS0012 DEVICE_TYPE=router make board/build). The two devices listed above were tested on router firmware.Related upstream work
PR #555 (opened 2026-11-15) fixes the same
hal_millis()wrap on Telink, in the same files this PR touches. The two changes overlap insrc/app.candsrc/telink/hal/timer.c; the conflict is two lines of context and is resolved by keeping both.If #555 lands first, this PR can be rebased on top of it and drop the
hal_millis()change, keeping onlyhal_millis_64(), since the counter would then already be monotonic.PR #170 also carries an "On with timed Off" implementation, but it is from October 2025 and was written against the old polling HAL; the review there asked for a rewrite in terms of tasks. This PR takes the task-based route already:
relay_cluster_task()polls a deadline fromapp_task().Issue #182 tracks the countdown timer request.