Last active
November 16, 2022 20:55
-
-
Save goncalomb/60d55dadb127ed2952d39342fa25584e to your computer and use it in GitHub Desktop.
A Shelly Script to control Hue lights without the cloud.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// control a hue lights group from a shelly device, without the cloud | |
// for shelly gen2 devices that have script support | |
// author: goncalomb <[email protected]> | |
// year: 2022 | |
// 1. use the hue debugger (clip.html) to create a new username (POST to /api), see below | |
// 2. change HUE_URL and HUE_USERNAME | |
// 3. optionally change HUE_GROUP, SHELLY_INPUT and EDGE_SWITCH | |
// 4. add the script to your shelly (web ui) | |
// 5. done | |
// ?. adapt the script to your needs | |
// create hue username: | |
// https://developers.meethue.com/develop/get-started-2/ | |
// https://<bridge ip address>/debug/clip.html | |
// POST /api {"devicetype":"shelly_btn"} | |
// shelly script documentation and examples: | |
// https://shelly-api-docs.shelly.cloud/gen2/Scripts/Tutorial | |
// https://shelly-api-docs.shelly.cloud/gen2/Scripts/ShellyScriptLanguageFeatures | |
let HUE_URL = "http://192.168.1.1/"; | |
let HUE_USERNAME = "XXXXXXXXXX"; | |
let HUE_GROUP = "1"; | |
let SHELLY_INPUT = "input:0"; | |
let EDGE_SWITCH = false; // change state with every hit | |
function request(params, callback, userdata) { | |
Shelly.call("HTTP.Request", params, function (result, error_code, error_message, userdata) { | |
if (error_code) { | |
print("ERR", error_code, error_message); | |
} else { | |
print("RES", result.code, result.message, result.body); | |
if (userdata.callback) { | |
userdata.callback(result, userdata.userdata); | |
} | |
} | |
}, { | |
callback: callback, | |
userdata: userdata, | |
}); | |
} | |
function get_state(callback) { | |
request({ | |
method: "GET", | |
url: HUE_URL + "api/" + HUE_USERNAME + "/groups/" + HUE_GROUP, | |
}, function (result, callback) { | |
let data = JSON.parse(result.body); | |
callback(data.state.any_on); | |
}, callback); | |
} | |
function put_state(state) { | |
request({ | |
method: "PUT", | |
url: HUE_URL + "api/" + HUE_USERNAME + "/groups/" + HUE_GROUP + "/action", | |
body: state ? '{"on":true}' : '{"on":false}', | |
}); | |
} | |
Shelly.addEventHandler(function (event, userdata) { | |
if (event.name === "input" && event.component === SHELLY_INPUT) { | |
if (event.info.component === SHELLY_INPUT && event.info.event === "toggle") { | |
if (EDGE_SWITCH) { | |
get_state(function (state) { | |
put_state(!state); | |
}); | |
} else { | |
put_state(event.info.state); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment