forked from shelly-tools/shelly-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue_button_control_group.js
63 lines (58 loc) · 1.92 KB
/
hue_button_control_group.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
/**
* @copyright shelly-tools contributors
* @license GNU Affero General Public License (https://www.gnu.org/licenses/agpl-3.0.de.html)
* @authors https://github.com/shelly-tools/shelly-script-examples/graphs/contributors
*
* This script is intended to toggle on/Off a group of lights connected to a Phillips HuE Bridge.
*
*/
let CONFIG = {
ip: '192.168.178.168', //Hue Bridge IP
user: 'Y0NXhTgbfcZYk0dSrTB70Va0XSZKFJdUYJcAj4bp', //Hue Bridge API user
group: '1', // Hue group ID
input1: 0, // Shelly Button ID
btnevent1: 'single_push' //Shelly Button Event
};
// add an evenHandler
Shelly.addEventHandler(
function (event, user_data) {
//print(JSON.stringify(event));
if (typeof event.info.event !== 'undefined') {
if (event.info.id === CONFIG.input1 && event.info.event === CONFIG.btnevent1) {
// Get the current light state
Shelly.call(
"http.request", {
method: "GET",
url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/groups/' + CONFIG.group,
},
function (res, error_code, error_message, ud) {
let st = JSON.parse(res.body);
if (st.state.any_on === true) {
Toggle("false");
} else {
Toggle("true");
}
},
null
);
} else {
return true;
}
} else {
return true;
}
},
);
function Toggle(state) {
let b = '{"on": ' + state + '}';
Shelly.call(
"http.request", {
method: "PUT",
url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/groups/' + CONFIG.group + '/action',
body: b
},
function (r, e, m) {
},
null
);
}