States returns 500 error

Hello Guys,

I can change settings of my lights using “state” method but I can’t make the “states” method work for me it returns error "returned code 500. Truncated server response: {“error”:“Error occured while processing request.”

Here is my Google appscript code:

  var headers={"Authorization": "Bearer "+TOKEN};
  
  var config =    
      {
        "states": [
          {
            "selector": "all",
            "power": "on",
            "color": "hue:45 saturation:1", 
            "brightness": 0.3
          }
        ],
        "defaults": {
          "duration": 5.0 // all states will be applied over 5 seconds
        }
      };

  var requestParams={"headers":headers, "method":"put","payload":config};

  var response = UrlFetchApp.fetch("https://api.lifx.com/v1/lights/states",requestParams);

Thanks for your help,

Léonard

I’ve had a quick look at our logs and it appears that your request body contains malformed JSON. Specifically, the states key-value pair is a string, and not an object.

You might want to double check how you’re encoding nested objects in your request body.

Alternatively, you could consider using the Set State endpoint. I don’t know much about Google appscript, but based on your code above it might look something like this:

var headers = {"Authorization": "Bearer "+TOKEN};

var config = {
  "power": "on",
  "color": "hue:45 saturation:1", 
  "brightness": 0.3,
  "duration": 5.0
};

var requestParams = {"headers":headers, "method":"put","payload":config};

var response = UrlFetchApp.fetch("https://api.lifx.com/v1/lights/all/state",requestParams);

Lastly, we realise returning a 500 response was the least useful thing in this situation. In future we may look at returning a 400 with a message like “Malformed JSON” to give you a better clue.

Thanks, will dig into it!