forked from ALLTERCO/shelly-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdouble-press-switch.js
More file actions
40 lines (27 loc) · 1.14 KB
/
double-press-switch.js
File metadata and controls
40 lines (27 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// the script show how perform mutliple action with a double classic switch used to toggle light.
// They keep their state once pressed, it usefull when the switch directly control the light.
// However it complicate double press actions when paired with a shelly. This script aim to solve this issue
let CONFIG = {
simpleClickAction: 'http://shelly-ip/rpc/Switch.Toggle?id=0',
doubleClickAction: 'http://shelly-ip/rpc/Switch.Toggle?id=1',
doubleClickDelay: 400,
buttonId: 0
};
let timer = undefined;
function toggleLight(action) {
timer = undefined;
return Shelly.call("http.get", {url: action});
}
Shelly.addEventHandler(
function (event, user_data) {
if (typeof event.info.event !== 'undefined' && event.info.event === 'toggle' && event.info.id === CONFIG.buttonId) {
if (timer === undefined) {
timer = Timer.set(CONFIG.doubleClickDelay, 0, toggleLight, CONFIG.simpleClickAction);
} else {
Timer.clear(timer);
timer = undefined;
toggleLight(CONFIG.doubleClickAction);
}
}
}
)