Set States API error

Hello guys! I’m trying to integrate the SetStates API into my server (nodeJS) but I’m having some difficulties.


The JSON that I’m using for test purposes is the following:

But I’m always getting the same error:

Error Image

And my code is the following:

Controller Code


Can you understand why I’m getting this error? Sorry about the image links, I’m only allowed to post 1 image :slight_smile:

Thank you.

If you’re only going to set states on ‘all’ devices at once, maybe you should just use setState, not setStates. I assume setStates is expecting an array with more than 1 entry, otherwise the use of setStates is redundant.

Alternatively try putting in a dummy second array entry?

Thank you for your reply, but I’ve also tried to change the selector for a group but I’m getting the same error again.

The ‘all’ was just to test the JSON :slight_smile:

Your code looks fine, but I’m not sure what the lifx object refers to though? For the best results could you show the HTTP request being sent by the browser. It should be in the network tab of the built in debugger.

I am getting the same error as the OP, but I am using PHP.

Here’s my PHP:

$link = "https://api.lifx.com/v1/lights/states";
$authToken = $lifxkey;
$headers = array('Authorization: Bearer ' . $authToken);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$response = curl_exec($ch);

where json_encode($data) equals:

{
   "states":[
      {
         "id":"dxxxxxxxxx20",
         "brightness":0.1,
         "kelvin":"2700",
         "duration":null
      },
     {
         "id":"dxxxxxxxxxe2",
         "brightness":0.75,
         "kelvin":"2700",
         "duration":null
      },
      {
         "id":"dxxxxxxxxx32",
         "brightness":0.75,
         "kelvin":"2700",
         "duration":null
      }
   ],
    "defaults":{
      "duration":300
   }
}

I get the response:

{  
   "error":"states is missing, states must have between 1 and 50 entries",
   "errors":[  
      {  
         "field":"states",
         "message":[  
            "is missing",
            "must have between 1 and 50 entries"
         ]
      }
   ]
}

Thoughts? I feel like I have messed up the structure of the JSON somewhere, or nested something in the wrong spot.

You were really close! There are two mistakes, the first is that you should send the right Content-Type header so that the server knows how to read the data. So just change your headers variable to be:

$headers = array(
    'Authorization: Bearer ' . $authToken,
    'Content-Type: application/json'
);

The second problem is that you have included the selector wrong. The selector needs to be a string in the selector attribute. Like this:

{
  "selector":"id:dxxxxxxxxxe2",
  "brightness":0.75,
  "kelvin":"2700",
  "duration":null
},

So here is a working example, based off yours. Do you mind if I add this to the official documentation?

<?php

$authToken = "[YOUR TOKEN HERE]";

$data = array(
    "states" => array(
        array(
            "selector" => "id:d073d501b1e5",
            "brightness" => 0.1,
            "kelvin" => 2700,
        ),
        array(
            "selector" => "id:d073d503a4c1",
            "brightness" => 0.75,
            "kelvin" => 2700,
        )
    ),
    "defaults" => array(
        "duration" => 300,
    )
);

$link = "https://api.lifx.com/v1/lights/states";
$headers = array(
    'Authorization: Bearer ' . $authToken,
    'Content-Type: application/json'
);
$ch = curl_init($link);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

$response = curl_exec($ch);

?>

Thank youuuuuuuu I’ll try this out when I get home tonight. And of course add it to the docs, cheers!

Yep, works like a treat, thanks!