-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript delayed power on and delayed off with reset.js
66 lines (61 loc) · 2.5 KB
/
script delayed power on and delayed off with reset.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Useful for controlling fan in the bathroom.
// Startup is delayed (2 sec), so you can cancel it if was activated accidentally.
// When turned off, it will continue running (for 5 minutes, adjust as needed).
// If input is active for less than 2 seconds, output is turned off.
// go to Shelly's IP address with web browser
// Select Channel settings -> IO settings -> DETACH mode (input and output are independent)
// Select Scripts -> Add script -> write name -> paste this code -> Save and Run
// go back to script list -> Enable this script (starts on Shelly startup)
let userdata = null;
let timer_handle = null;
let input_state = 0;
let short_timer_active = false;
function TimerCallback(userdata) {
print("timer activated");
if (short_timer_active) {
print("checking input...");
if (input_state === 0) { // switch was turned off under 2 seconds
// turn off output
print("... off -> turn off");
Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
} else { // switch is still on
// turn on output
print("... on -> turn on");
Shelly.call("Switch.Set","{ id:0, on:true }",null,null);
}
} else { // long timer
print("load active -> turn off");
Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
}
short_timer_active = false;
}
function eventcallback(userdata) {
// print("Event called: ", JSON.stringify(userdata));
// Event called: {"info":{"state":true,"id":0},"now":1643272255.228291,"id":0,"name":"input","component":"input:0"}
// Event called: {"info":{"state":false,"id":0},"now":1643272257.047522,"id":0,"name":"input","component":"input:0"}
// check if this a button press event
if (userdata.component==="input:0") {
if (userdata.info.state===true) {
print("button active event");
input_state = 1;
// reset timer, if running
Timer.clear(timer_handle);
// re-check button status after 2 seconds
timer_handle = Timer.set(2000,false,TimerCallback,null);
short_timer_active = true;
print("timer set for 2 sec");
}
else {
print("button off event");
input_state = 0;
if (short_timer_active === false) {
// reset timer, if running
Timer.clear(timer_handle);
// activate timer to turn off output after 5 minutes
timer_handle = Timer.set(5*60000,false,TimerCallback,null);
print("timer off set for 5 min");
}
}
}
}
Shelly.addEventHandler(eventcallback, userdata);