-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcontrol_rgbw.js
110 lines (98 loc) · 3.1 KB
/
control_rgbw.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @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 control a Shelly RGBW2 / ShellyDuo RGBW with a single button from
* a Shelly Plus device. Every time the button from the Plus device is pushed it jumps to the next step
* defined in the sequence array.
*/
// CONFIG START
// IP address or hostname from RGBW device
let REMOTE = {
ip: '192.168.178.71',
};
// button sequence
// The demo sequence is: on and blue with max brightness, then red, green, yellow, purple, off
let colorsequence = [
'?turn=on&gain=100&blue=0&red=255&green=120', // warm white
'?turn=on&gain=100&blue=255&red=0&green=0', // blue
'?turn=on&gain=100&blue=0&red=255&green=0', // red
'?turn=on&gain=100&blue=0&red=0&green=255', // green
'?turn=on&gain=100&blue=0&red=255&green=255', // yellow
'?turn=on&gain=100&blue=225&red=185&green=76', // purple
];
// Dimming level for the related color
let dimsequence = [
'?gain=50', // 50%
'?gain=33', // 33%
'?gain=20', // 20%
'?gain=5', // 5%
'?gain=100', // 100%
];
// CONFIG END
// no need to change anything below this line..
let colorposition = 0;
let dimpos = 0;
// add an evenHandler for button type input and various push events
Shelly.addEventHandler(
function (event, user_data) {
//print(JSON.stringify(event));
if (typeof event.info.event !== 'undefined') {
if (event.info.event === 'double_push') {
setRGBW(REMOTE.ip, colorposition);
colorposition++;
dimpos = 0;
if (colorsequence.length === colorposition) {
colorposition = 0;
}
} else if (event.info.event === 'single_push'){
toggleRGBW(REMOTE.ip);
} else if (event.info.event === 'long_push') {
dimRGBW(REMOTE.ip, dimpos);
dimpos++;
if (dimsequence.length === dimpos) {
dimpos = 0;
}
} else {
return true;
}
} else {
return true;
}
},
);
// send RBGW command
function setRGBW(ip, colorposition) {
Shelly.call(
"http.get", {
url: 'http://' + ip + '/light/0' + colorsequence[colorposition]
},
function (response, error_code, error_message, ud) {
print(colorsequence[colorposition]);
},
null
);
};
// send RBGW dimming command
function dimRGBW(ip, dimpos) {
Shelly.call(
"http.get", {
url: 'http://' + ip + '/light/0' + dimsequence[dimpos]
},
function (response, error_code, error_message, ud) {
print(dimsequence[dimpos]);
},
null
);
};
function toggleRGBW(ip) {
Shelly.call(
"http.get", {
url: 'http://' + ip + '/light/0?turn=toggle'
},
function (response, error_code, error_message, ud) {
},
null
);
}