This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Philips Hue Authentication
#1
Hello,

I try to get Hue to work with LM. The log mentions : 

* string: [{"error":{"type":1,"address":"/lights","description":"unauthorized user"}}]

How, when and where should the bridge be authenticated please ? 



Thanks,

Koen
Reply
#2
Hi Koen,

Philips have changed the API due homekit support.

Use this to control your HUE devices with latest HUE FW update :

First run bridgeSetup() once to receive the user (see log after running) and enter where now "xxxxxxxxxxxxxxxxxx' is filled in.

Code:
require('json')
require('socket.http')
require("ltn12")

--************************************************
ip_add = '192.168.1.25'
user = "XXXXXXXXXXXXXXXXXXXXX"    -- Response from bridgeSetup()
--************************************************

response = {}

function bridgeSetup()
 body_bridgeSetup= '{"devicetype":"homelynk#homelynk"}'
 -- Set username for homeLYnk
    socket.http.request({
     url = "http://"..ip_add.."/api",
  method = 'POST',
    sink = ltn12.sink.table(response),
 headers = {
     ['content-length'] = #body_bridgeSetup,
       ['content-type'] = 'application/json',
           },
   source = ltn12.source.string(body_bridgeSetup),
       })
    log(response)
end

function checkUser()
  body_bridgeSetup= '{}'
 -- Set username for homeLYnk
    socket.http.request({
     url = "http://"..ip_add.."/api/"..user,
  method = 'GET',
    sink = ltn12.sink.table(response),
 headers = {
     ['content-length'] = #body_bridgeSetup,
       ['content-type'] = 'application/json',
           },
   source = ltn12.source.string(body_bridgeSetup),
       })
    log(response)
 ret = table.concat(response)
    data = json.pdecode(ret)
 
 if (data[1].error.type)==1 then
   log("Hue bridge user is not set")
   return false
 else
   log("Hue bridge user is set")
   return true
 end
 
end

function searchHueLights()
 body_searchHueLights= ''
 -- Set username for homeLYnk
    socket.http.request({
     url = "http://"..ip_add.."/api/"..user.."/lights",
  method = 'POST',
    sink = ltn12.sink.table(response),
 headers = {
     ['content-length'] = #body_searchHueLights,
       ['content-type'] = 'application/json',
           },
   source = ltn12.source.string(body_searchHueLights),
       })
    log(response)
end

function sendToLight(Light_num,body_request)
 socket.http.request({
     url = "http://"..ip_add.."/api/"..user.."/lights/"..tostring(Light_num).."/state",
  method = 'PUT',
    sink = ltn12.sink.table(response),
 headers = {
       ['content-length'] = #body_request,
       ['content-type'] = 'application/json',
           },
     source = ltn12.source.string(body_request),
       })
    --log(response)
end

function sendToGroup(Group_num,body_request)
 socket.http.request({
     url = "http://"..ip_add.."/api/"..user.."/groups/"..tostring(Light_num).."/action",
  method = 'PUT',
    sink = ltn12.sink.table(response),
 headers = {
       ['content-length'] = #body_request,
       ['content-type'] = 'application/json',
           },
     source = ltn12.source.string(body_request),
       })
    --log(response)
end

function setRGB(Light_num,RGB_variable)
--RGB color set in homeLYnk
Red=bit.rshift(bit.band(RGB_variable,0xff0000),16)
Green=bit.rshift(bit.band(RGB_variable,0x00ff00),8)
Blue=bit.band(RGB_variable,0x0000ff)

-- Norm of RGB values
Red_f=    Red/255
Green_f=Green/255
Blue_f=    Blue/255

--Gamma correction
if Red_f>0.04045 then
 Red_f=((Red_f+0.055)/(1+0.055))^2.4
else
 Red_f=Red_f/12.92
end

if Green_f>0.04045 then
 Green_f=((Green_f+0.055)/(1+0.055))^2.4
else
 Green_f=Green_f/12.92
end

if Blue_f>0.04045 then
 Blue_f=((Blue_f+0.055)/(1+0.055))^2.4
else
 Blue_f=Red_f/12.92
end

--Coversion RGB->xy
X=Red_f*0.649926+Green_f*0.103455+Blue_f*0.197109
Y=Red_f*0.234327+Green_f*0.743075+Blue_f*0.022598
Z=Green_f*0.053077+Blue_f*1.035763

x=X/(X+Y+Z)
y=Y/(X+Y+Z)

--HTTP request send
body_msg = '{"on":true,"xy":['..x..','..y..']}'
sendToLight(Light_num,body_msg)  
end
 
Sample to set RGB value from color picker object event based script:
Code:
value = event.getvalue()
lamp_id = 1

if value == 0 then
 body_msg = '{"on":false}'
 sendToLight(lamp_id,body_msg)
else
 setRGB(lamp_id,value)
end

Good luck (:

BR,
Erwin
Reply
#3
Dear Sir,
Dear Erwin,

Thanks for the help.  

Could you please elaborate a bit more please ? I'm going to subscribe for the Homelynk training in the Netherlands, but I want to get this right.   I would add the code to 'common functions', and then create a resident script that calls the function to authenticate.  Is this the right way to do it please ? 

We use the Hue for only one purpose : night lights for the children. As such,  I would like to call a saved scene for a room. Is this possible please ? 

Thanks,

George
Reply
#4
Hi Koen,

No its not a good idea to put the functions into common functions.

Common functions are used when most of you scripts need the functions. Every script is connected to the common functions and loads that content. If you put all your functions into common functions it will rapidly reduce your sytem performance. 

That is why we have user liberies. When you put your functions in there you have to request in each script you make to include the content of that user libery by the command: require('youruserlibname')

So what you should do is create a user lib called hue (disable automatic load checkbox) , paste the functions from the first code block in my post above. Change only the IP to your HUE bridge IP and save.

Create a event based script connected to the RGB object that needs to trigger the action, start the script with requesting the just created user libary with: require('user.hue') and past my code from the second code block in my post above under the require command line.

To create the user where now xxxxxxx is filled in, you must temperary put the command bridgeSetup() under the require ('user.hue') line, save , enable the script and change the color once. Your HUE does not respond yet but now the user is created and you can find it in the log tab. 

Paste this user into the hue userlib, remove the bridgeSetup() command from your event based script, save all changed scripts.

Now you can control your HUE from the RGB object.

I will see you at the homeLYnk training in the Netherlands as i'm your trainer (:

BR,

Erwin
Reply


Forum Jump: