LIFX Developer Zone

Working with JavaScript (Solved - for now)

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);
}

Hello,

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.

Ok thanks, I’ve submitted a ticket.

BTW, I changed the second example to a ‘put’ method and now it’s returning the same ‘timed_out’ error.

Interestingly, I just tried sending code to a single bulb ID and noticed on the end of the code:

 "warning": "Unknown params",
      "unknown_params": {
        "{\"power\":\"on\",\"color\":\"blue\"}": [
          ""

So maybe my formatting isn’t right?

Edit: Ah, I think I’m getting somewhere now. I’ve taken out the “JSON.stringify” in color_2 code and it’s working! Seems that was unnecessary. :slight_smile:

Note to self: Read the error messages more carefully. They might be telling you something :slight_smile:

hehehhe. Yeah it needs the Content-Type of application/json and be a correctly formatted JSON string :slight_smile:

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.

Yes I guess it was JSONed enough already and I over-stringified it.

Haha, thanks for the https://http.cat, hadn’t seen that before :slight_smile:

BTW, I was looking at the ‘breathe’ effect at https://api.developer.lifx.com/docs/breathe-effect

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.

That’s a restriction in the windows terminal. In this case double quotes are a reasonable substitute :slight_smile:

You can also provide those as json by giving the application/json Content-Type and providing something like

{"period": 2, "cycles": 5, "color": "green"}