I want to turn my lights off with Java using the Philips HUE Api. For this, I have the following API: http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/
This returns:
{
"state": {
"on": true,
"bri": 178,
"hue": 41044,
"sat": 56,
"effect": "none",
"xy": [
0.3313,
0.3447
],
"ct": 181,
"alert": "select",
"colormode": "ct",
"mode": "homeautomation",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2020-07-03T12:17:24"
},
"type": "Extended color light",
"name": "Hue color spot 1",
"modelid": "LCG002",
"manufacturername": "Signify Netherlands B.V.",
"swconfigid": "9FD98F72"
}
Now I want to modify the "on: true" to "on: false" with Java. For that, I have this code:
URL url = new URL("http://192.168.0.53/api/HZ3bJqN3MIb35uNcI4FfsGcqstwbW6qgvuxd7Tmt/lights/1/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.setRequestProperty("Content-Type", "application/json");
JSONObject requestBody = new JSONObject();
requestBody.put("on", false);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(requestBody.toString());
writer.flush();
writer.close();
// Debug
int response = connection.getResponseCode();
System.out.println(requestBody.toString());
System.out.println(response);
This returns '200' and {"on":false} which would be perfect. But for some reason, it just does nothing. The light remains on even though the connection exists.
Any ideas why?
You are using the wrong URL for the PUT request.
A GET request to http://192.168.0.53/api/.../lights/1
will return the current state
But to modify you need a PUT request to http://192.168.0.53/api/.../lights/1/state