forked from shelly-tools/shelly-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue_button_multipush.js
133 lines (122 loc) · 4.98 KB
/
hue_button_multipush.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Credit goes to https://github.com/eurich for laying the groundwork.
Prerequisites:
- A wall-mounted button (not a switch) wired to the switch output of a Shelly PM with Firmware 0.10.0 or later
- The Shelly is in Button and Detached mode, nothing is wired to the consumer output (Script will override this)
- A Philips Hue Light where the lamp wire is wired directly to phase L ("always on"), which ensures that it will stay reachable for the Hue Bridge
- Set the Hue Light to restore the last state on power on in the Hue App
- Gather the informations needed to be inserted in the CONFIG variable
This Script handles the following scenarios:
- A "single push" on the wall button or "Toggle" from the app turns the light on or off
- Each quick "double push" on the wall button cycles through several color or temperature settings (see CONFIG)
- Each "long push" on the wall button cycles through several brightness settings (pseudo-dimmer)
Tested with:
- Shelly 1PM / Firmware 0.10.3
*/
/* TO BE SET BY USER */
let CONFIG = {
// Shelly Button ID
input0: 0,
// Hue Bridge IP
ip: '192.168.0.1',
// Hue Bridge API user
user: 'ABCDEFGH1234',
// Hue Light ID
light: '1',
// Do you want the long push to change color or the temperature
isDoublePushColorMode: false,
// Hue Color value
colorSequence: [0, 12750, 25500, 46920, 56100, 65280,],
// Hue Color Temperature in reciprocal megakelvin
colorTemperatureSequence: [500, 345, 233, 153,],
// Hue Brightness 0-254
brightnessSequence: [254, 174, 94, 14,],
};
/* No customization needed below this */
let STRINGS = {
shellyEventUndefined: 'undefined',
shellyEventToggle: 'toggle',
shellyEventSinglePush: 'single_push',
shellyEventDoublePush: 'double_push',
shellyEventLongPush: 'long_push',
shellyInputSetConfig: 'Input.SetConfig',
shellySwitchSetConfig: 'Switch.SetConfig',
shellyButton: 'button',
shellyDetached: 'detached',
shellyStateOn: 'on',
shellyStateOff: 'off',
httpRequest: 'http.request',
httpGet: 'GET',
httpPut: 'PUT',
};
let apiUrl = 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/lights/' + CONFIG.light;
let apiUrlState = apiUrl + '/state';
let indexSequence = 0;
let indexBrightness = 0;
Shelly.call(STRINGS.shellyInputSetConfig, {
id: CONFIG.input0,
config: {
type: STRINGS.shellyButton,
},
});
Shelly.call(STRINGS.shellySwitchSetConfig, {
id: CONFIG.input0,
config: {
in_mode: STRINGS.shellyDetached,
initial_state: STRINGS.shellyStateOn
},
});
Shelly.addEventHandler(
function (event, userData) {
// print(JSON.stringify(event));
if (typeof event.info.event !== STRINGS.shellyEventUndefined) {
if (event.info.id === CONFIG.input0 && (event.info.event === STRINGS.shellyEventToggle || event.info.event === STRINGS.shellyEventSinglePush)) {
Shelly.call(
STRINGS.httpRequest, {
method: STRINGS.httpGet,
url: apiUrl,
}, function (res, errorCode, errorMessage, ud) {
let state;
let resJson = JSON.parse(res.body);
if (resJson.state.on === true) {
state = false;
} else {
state = true;
}
CallStateApi(JSON.stringify({ on: state }));
}, null);
} else if (event.info.id === CONFIG.input0 && event.info.event === STRINGS.shellyEventDoublePush) {
if (CONFIG.isDoublePushColorMode === true) {
CallStateApi(JSON.stringify({ on: true, hue: CONFIG.colorSequence[indexSequence] }));
} else {
CallStateApi(JSON.stringify({ on: true, ct: CONFIG.colorTemperatureSequence[indexSequence] }));
}
indexSequence++;
ResetIndex();
} else if (event.info.id === CONFIG.input0 && event.info.event === STRINGS.shellyEventLongPush) {
if (CONFIG.brightnessSequence.length === indexBrightness) {
indexBrightness = 0;
}
CallStateApi(JSON.stringify({ on: true, bri: CONFIG.brightnessSequence[indexBrightness] }));
indexBrightness++;
ResetIndex();
} else {
return true;
}
} else {
return true;
}
});
function CallStateApi(state) {
Shelly.call(
STRINGS.httpRequest, {
method: STRINGS.httpPut,
url: apiUrlState,
body: state
}, function (res, errorCode, errorMessage) { }, null);
}
function ResetIndex() {
if (CONFIG.colorSequence.length === indexSequence || CONFIG.colorTemperatureSequence.length === indexSequence) {
indexSequence = 0;
}
}