forked from shelly-tools/shelly-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue_switch_control_light.js
54 lines (48 loc) · 1.43 KB
/
hue_switch_control_light.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
/**
* @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 Light connected to a Phillips HuE Bridge.
*
*/
// CONFIG START
let CONFIG = {
ip: '192.168.178.168', //Hue Bridge IP
user: 'Y0NXhTgbfcZYk0dSrTB70Va0XSZKFJdUYJcAj4bp', //Hue Bridge API user
light: '4', // Hue Light ID
input1: 0, // Shelly switch ID
};
// CONFIG END
Shelly.addStatusHandler(function (e) {
if (e.delta.id === CONFIG.input1) {
Shelly.call(
"http.request", {
method: "GET",
url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/lights/' + CONFIG.light,
},
function (res, error_code, error_message, ud) {
let st = JSON.parse(res.body);
if (st.state.on === true) {
Toggle("false");
} else {
Toggle("true");
}
},
null
);
}
});
function Toggle(state) {
let b = '{"on": ' + state + '}';
Shelly.call(
"http.request", {
method: "PUT",
url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/lights/' + CONFIG.light + '/state',
body: b
},
function (r, e, m) {
},
null
);
}