forked from shelly-tools/shelly-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue_switch_reset.js
63 lines (57 loc) · 1.52 KB
/
hue_switch_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
let CONFIG = {
ip: '192.168.<ip.number>', //Hue Bridge IP
user: '<api-key>', //Hue Bridge API user
groups: '3', // Hue Groups ID
input1: 0, // Shelly Switch ID
recent: false, // has been recently turned on
recent_sec: 15 // number of seconds to consider recent
};
// Callback to clear recent
function ClearRecent() {
CONFIG.recent = false
}
// Set Switch detached
Shelly.call("Input.SetConfig", {
id: CONFIG.input1,
config: {
type: "switch",
},
});
Shelly.call("Switch.SetConfig", {
id: CONFIG.input1,
config: {
in_mode: "detached",
initial_state: "on"
},
});
// add an evenHandler
Shelly.addEventHandler(
function (event, user_data) {
if (typeof event.info.state !== 'undefined' && event.info.id === CONFIG.input1) {
if (event.info.state) {
if (CONFIG.recent) {
SetLight('{"ct": 500, "bri": 254, "on": true}');
} else {
CONFIG.recent = true
Timer.set(CONFIG.recent_sec * 1000, false, ClearRecent)
SetLight('{"on": true}');
}
} else {
SetLight('{"on": false}');
}
}
},
null,
);
function SetLight(b) {
Shelly.call(
"http.request", {
method: "PUT",
url: 'http://'+ CONFIG.ip +'/api/'+ CONFIG.user +'/groups/'+ CONFIG.groups +'/action',
body: b
},
function (r, e, m) {
},
null
);
}