Replies: 7 comments 15 replies
-
|
interesting. I were researching with like a week ago and my tuya switches behave differently. When the switch is off, state: on, on_time: will turn it on after on_time timeout. It doesnt turn the light on when receives a command. weird |
Beta Was this translation helpful? Give feedback.
-
|
i can confirm that for tuya this /10 can be removed since it seems tuya is not following the spec |
Beta Was this translation helpful? Give feedback.
-
|
Indeed. The command is not supposed to toggle or turn on the switch immediately with the Tuya behavior. This is just a delayed toggle. The syntax Something like |
Beta Was this translation helpful? Give feedback.
-
|
I forgot to mention that an active timeout toggle can be cancelled by either setting Here is a converter that adds a command |
Beta Was this translation helpful? Give feedback.
-
|
Following @avbdr remark, I modified the converter to allow both CORRECTION: The state value was not properly converted to lower case in my initial post. Should work better now. //
// /set { "state":"on|off|toggle", "timeout_toggle": DELAY }
//
// Set the state and toggle it after the specified DELAY in seconds.
//
// Both new_state and timeout_toggle are optional.
//
// The DELAY can be an integer between 1 and 43200 (12 hours) or 0
// to cancel
//
// Setting a new state will also cancel any ongoing timeout.
//
// Examples:
//
// - turn on without timeout toggle
//
// /set { "state":"on" }
// or
// /set { "state":"on" , "timeout_toggle": 0 }
//
// - turn off and toggle after 10s
//
// /set { "state":"off", "timeout_toggle": 10 }
//
// - toggle now and again after 5s
//
// /set { "state":"toggle", "timeout_toggle": 5 }
//
// - keep current state and toggle after 5s
//
// /set { "timeout_toggle": 5 }
//
// - cancel an ongoing timeout toggle
//
// /set { "timeout_toggle": 0 }
//
const tuya_onoff_with_timeout_toggle = {
key: ['state', 'timeout_toggle'],
convertSet: async (entity, key, value, meta) => {
// If I understand correctly, this is called ONCE when any of the
// listed keys is present.
// Note: `key` and `value` seem to be the first ones given
// in the MQTT payload but that does not necessarily make sense
// since the members of a JSON object are supposed to be unordered.
// Simply speaking: Do not use key and value.
let new_state = meta.message.hasOwnProperty('state') ? meta.message.state : undefined ;
const timeout_toggle = meta.message.hasOwnProperty('timeout_toggle') ? meta.message.timeout_toggle : undefined ;
let result = {};
// Perform all consistancy checks first to insure that either all or none of the
// requested commands are sent.
if ( timeout_toggle !== undefined ) {
// Might work up to 0xFFFF seconds but the Tuya documentation says 43200 (so 12 hours).
// Tests needed
if ( !Number.isInteger(timeout_toggle) || timeout_toggle < 0 || timeout_toggle > 12*3600 ) {
throw Error('timeout_toggle must be an integer between 1 and 43200 (12 hours) or 0 to cancel' );
}
}
if ( new_state !== undefined ) {
new_state = utils.isString(new_state) ? new_state.toLowerCase() : null;
utils.validateValue(new_state, ['toggle', 'off', 'on']);
}
// Now, send the requested commands in the proper order: 'on/off/toggle' and then 'onWithTimedOff'.
if (new_state !== undefined) {
await entity.command('genOnOff', new_state, {}, utils.getOptions(meta.mapped, entity));
if (new_state === 'toggle') {
const currentState = meta.state[`state${meta.endpoint_name ? `_${meta.endpoint_name}` : ''}`];
result = currentState ? {state: {state: currentState === 'OFF' ? 'ON' : 'OFF'}} : {};
} else {
result = {state: {state: new_state.toUpperCase()}};
}
}
if ( timeout_toggle !== undefined ) {
const payload = {ctrlbits: 0, ontime:timeout_toggle , offwaittime:timeout_toggle };
await entity.command('genOnOff', 'onWithTimedOff', payload, utils.getOptions(meta.mapped, entity));
}
return result ;
}
}; |
Beta Was this translation helpful? Give feedback.
-
|
Hi! I tried to use the countdown feature in the HA automation to turn off the light activated by a presence sensor and it works but the problem is when the light is toggled by countdown, its status is not updated in HA |
Beta Was this translation helpful? Give feedback.
-
|
I am successfully using countdown on multiple plugs and dry switches. First you need to figure out if the on/off status is properly reported in Z2M for example using the Z2M web interface? A custom reporting rule could solve the problem. If its works fine in Z2M then the problem is likely in HA. Are you you sure that your widget is properly reacting to changes it did not initiate? |
Beta Was this translation helpful? Give feedback.



Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
NOTE: I already filled #22182 about that.
Devices with the on-off cluster are documented in Z2M with the On with timed off feature :
When setting the state to ON, it might be possible to specify an automatic shutoff after a certain amount of time. To do this add an additional property on_time to the payload which is the time in seconds the state should remain on. Additionnaly an off_wait_time property can be added to the payload to specify the cooldown time in seconds when the switch will not answer to other on with timed off commands. Support depend on the switch firmware. Some devices might require both on_time and off_wait_time to work Examples : {"state" : "ON", "on_time": 300}, {"state" : "ON", "on_time": 300, "off_wait_time": 120}.
This is a standard behavior of the on-off cluster described in the Zigbee standard. See sections 3.8.2.3.6 On With Timed Off Command and **3.8.2.4 State Description ** in the ZCL.
However, Tuya devices that support the on-off cluster have a slightly different implementation so the behavior described above does not work as expected. The cluster attributes are the same but with the following differences:
OnTimeandOff Wait Timefields are in 1/10 seconds in the Zigbee standard but in seconds in Tuya.OffWaitTimedoes not really have any use in Tuya (I think) but the documentation says that it should be set to the same value thanOnTime.OnTimeseconds. According to the zigbee standard, this is transition from ON to OFF afterOnTime1/10th seconds.Simply speaking, Tuya on-off cluster implements a delayed toggle (i.e. a countdown). In Z2M, a toggle after 14 seconds can already be obtained with
Notice how the delay of 14 seconds must be divided by 10. This is because Z2M is incorrectly assuming that the
OnTimeandOffWaitTimefields are in 1/10 seconds.This is documented for all Tuya standards that support the On/Off cluster in https://developer.tuya.com/en/docs/connect-subdevices-to-gateways/Zigbee_2?id=Kcww7qppbe87m . The links in the page body are all in Chinese but can be changed to English using the 'World' icon at the top of the page (or replace
/cn/by/en/in the URL.For example, the Standard for Smart Metering Socket documents the Timer feature as follow:
================================
Format of payload: On/off Control: 0x00. On time and Off Wait Time must be set to the same value. Example: To send a command of a local countdown for 60s, the payload contains the following settings:
================================
A few remarks:
OffWaitTimeshould be equal toOnTime. I did not follow that rule in my external converter but it seems to work fine at least for now.0 to 43200? because43200s = 12 * 3600s = 12 hours. The full 16bits range 0-0xFFFF may not be allowed. Some tests are needed.OnTimeandOffWaitTimethat were obviously borrowed from the non-tuya standard behavior.
The Standard for Smart Switch is slightly different but the idea remains the same:
Beta Was this translation helpful? Give feedback.
All reactions