Hue api id identification

  api, javascript, json

I’m in the process of making my own philips hue controller via javascript. and I am trying to identify groups and light id’s. But i can’t figure out how I get the JSON id of a group or light.

I get an http answer in JSON format what looks like this:

{
    "1": {
        "name": "Woonkamer",
        "lights": [
            "2",
            "1",
            "4"
        ],
        "sensors": [],
        "type": "Room",
        "state": {
            "all_on": true,
            "any_on": true
        },
        "recycle": false,
        "class": "Living room",
        "action": {
            "on": true,
            "bri": 144,
            "hue": 12057,
            "sat": 143,
            "effect": "none",
            "xy": [
                0.502,
                0.4204
            ],
            "ct": 447,
            "alert": "select",
            "colormode": "xy"
        }
    },
    "2": {
        "name": "keuken",
        "lights": [
            "3"
        ],
        "sensors": [],
        "type": "Room",
        "state": {
            "all_on": true,
            "any_on": true
        },
        "recycle": false,
        "class": "Kitchen",
        "action": {
            "on": true,
            "bri": 254,
            "ct": 366,
            "alert": "select",
            "colormode": "ct"
        }
    },

I have no problem with accessing for example the type and name, but I have no idea how to access the id’s that are written at the start of each section. I cant just get the first index because then I just get the other objects like name etc.

My current function looks like this where I access the room name, but I also need the room id. Keep in mind this code looks awful because I’m quite new to JS

function getGroups()
    {
        var url = "https://"+ipadress+"/api/"+username+"/groups";
        request.open("GET", url);
        request.setRequestHeader("Content-Type", "application/json");
        request.send();

        request.onreadystatechange = () => {
            if (request.readyState === XMLHttpRequest.DONE) {
                const res = JSON.parse(request.responseText);
                for(var i = 1; i < Object.keys(res).length; i++)
               {
                    if(res[i] != null)
                    {
                        if(res[i].type == "Room")
                        {
                            var name = res[i].name;
                            document.getElementById("light_container").style.display = "block";
                            var onoff = document.createElement('input');
                            onoff.setAttribute('type', 'button');
                            onoff.setAttribute('id', name);
                            onoff.setAttribute('value', name); //need to change this to dynamic function that reads the hue
                            onoff.setAttribute('onclick', 'changeState("/groups/'+name+'/action")') //need to make change state function
                            document.body.appendChild(onoff);
                        }
                    }

                }
            };
        }
    }

Source: Ask Javascript Questions

LEAVE A COMMENT