I’m trying to build some JavaScript to control my LIFX lights. I’ve got part of the way, in that I can get it to List Lights and see the status, and I can toggle all lights on and off, but I’m getting stuck when I need to send more data to make changes like a different color or power state. This is what I’m working with:
function colour_1() {
var token = "***********************************************************************"
var headers = { "Authorization":"Bearer " + token, };
//var data = {"color":"blue saturation:0.5" }
//var data = {"data":"color=blue"}
//var data = {"color":"blue"}
var data = {"power":"off"}
var options =
{ "method" : "put",
"headers": headers,
"data" : data,
"contentType": "application/json"
};
var result = UrlFetchApp.fetch('https://api.lifx.com/v1/lights/all/state', options).getContentText();
Logger.log(result);
}
You can see that I’ve tried various different options for the ‘data’ input part but I can’t find anything that works. It’s part way there in that some tries result in a ‘time out’ and not just a rejection of the code, so maybe that suggests it’s not totally wrong, just maybe badly formatted?
"results": [
{
"id": "************",
"status": "timed_out",
"label": "Front Left"
},
..........etc. it lists all lights as 'timed out'
I’ve also tried this, a slightly different way of setting it up, but just get a 405 error.
function color_2() {
var token = "***************************************************************************"
var payload = JSON.stringify({
"power" : "off"
});
var headers = {
"Authorization":"Bearer " + token, };
var options = {
'method': 'post',
'payload': payload,
'headers': headers
'Content-Type': 'application/json',
};
var result = UrlFetchApp.fetch('https://api.lifx.com/v1/lights/all/state', options).getContentText();
Logger.log(result);
}
Ok, so first the 405. Essentially in HTTP the endpoint is defined by the method (so PUT or POST in this case) and the url (so /v1/lights/all/state). We define that endpoint with the PUT method, so when you use the POST method it’s giving you a 405 error which says wrong method.
So the first request is a correct request. What it’s complaining about is your light isn’t talking back to us. I suggest you submit a ticket and the support team will help you figure out how to fix that.
hehehhe. Yeah it needs the Content-Type of application/json and be a correctly formatted JSON string
The 405 is the server complaining about your request, the timed_out is complaining about your bulb rather than the request (and you get back a “207” with that response)
There’s a lot of different status codes and some fun sites that list them all
In general, you have 2XX, 3XX, 4XX and 5XX status codes.
So for example, 200 is a 2XX status code, 404 is a 4XX status code, etc.
2XX - request itself was correct and was processed successfully (in your case, we were “successful” in that our code didn’t fail, even though your device didn’t respond to us)
3XX - the endpoint you used is actually somewhere else now. These are called redirections
4XX - your request was invalid. For example 404 means the endpoint you used doesn’t exist, or in above 405 means you used the wrong http method
5XX - your request was valid but something went wrong on the server and processing your request was unsuccessful.
Where it gives an example of the cURL commands, I could only make those work in a console by substituting the ‘single’ quotes for “doubles” for the three -d parameters.