Hi all,
I've been struggling to get a piece of script from HE over to a Shelly relay as per their documentation but I can't manage to translate the Python example to Groovy, any idea what I'm doing wrong ?
def uploadScript(){
code = "Shelly.addStatusHandler(function(event) {if (event.name === \"switch\"){let url = \"http://HUBITAT_IP:39501/switch/\" + JSON.stringify(event.id) + \"/\" + (event.delta.output?\"on\":\"off\") + \"/\";Shelly.call(\"HTTP.GET\", {\"url\": url});}});";
uri = "http://${shelly_ip}/rpc/Script.PutCode"
def postParams = [
uri: uri,
requestContentType: 'text/html',
contentType: 'application/octet-stream',
body: ['code' : java.net.URLEncoder.encode(code), 'id': 1, 'append': false]
]
/*
The original request as per https://github.com/ALLTERCO/shelly-script-examples/blob/main/tools/put_script.py:
req = {"id": id_, "code": data, "append": append}
req_data = json.dumps(req, ensure_ascii=False)
res = requests.post(url, data=req_data.encode("utf-8"), timeout=2)
log.info res.json()
*/
log.info postParams
try{
asynchttpPost('myCallbackMethod', postParams)
}catch (e){
log.debug e.message
}
}
def myCallbackMethod(response, data) {
log.debug "status of post call is: ${response.status}"
}
Message is always 'status of post call is: 500' aka Internal Server Error
The shelly examples are available in bash/curl and python (shelly-script-examples/tools at main · ALLTERCO/shelly-script-examples · GitHub), but neither got me any closer to the finish line.