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.

Getting data from Philips Hue Outdoor Motion Sensor
#1
Hi.

Just bought this outdoor sensor from Philips, but struggeling to get some data out where it can be used.
I don't want to do something directly inside the Hue system, but to extract data.

The device does not come up in http://<ipadress>/api/<userid>/lights along with my other lamps/led's.
Instead it is visible http://<ipadress>/api/<userid>/sensors

Therefore I tried to modify the user.hue script by adding a section for sensors to search and get data from them.
So far so good.
This is a snippet from the user.hue script where I have duplicated the code, but renamed the original code from i.e function searchHueLights() to function searchHueSensors() and so on.
So "function" 2 and 4 below is new regarding the sensors


Code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
-- function 1 existing huesearch function searchHueLights()   local response = {}   body_searchHueLights= ''   -- Set username for LogicMachine   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),   })   return response end -- function 2 new huesearch function searchHueSensors()   local response = {}   body_searchHueSensors= ''   -- Set username for LogicMachine   socket.http.request({   url = "http://"..ip_add.."/api/"..user.."/sensors",   method = 'POST',         sink = ltn12.sink.table(response),   headers = {           ['content-length'] = #body_searchHueSensors,           ['content-type'] = 'application/json',   },           source = ltn12.source.string(body_searchHueSensors),   })   return response end -- function 3 existing hue datacollection function getHueLights()   local response = {}   body_searchHueLights= ''   -- Set username for LogicMachine   socket.http.request({   url = "http://"..ip_add.."/api/"..user.."/lights",   method = 'GET',         sink = ltn12.sink.table(response),   headers = {           ['content-length'] = #body_searchHueLights,           ['content-type'] = 'application/json',   },           source = ltn12.source.string(body_searchHueLights),   })   return response end -- function 4 new hue datacollection function getHueSensors()   local response = {}   body_searchHueSensors= ''   -- Set username for LogicMachine   socket.http.request({   url = "http://"..ip_add.."/api/"..user.."/sensors",   method = 'GET',         sink = ltn12.sink.table(response),   headers = {           ['content-length'] = #body_searchHueSensors,           ['content-type'] = 'application/json',   },           source = ltn12.source.string(body_searchHueSensors),   })   return response end

By using this code I then can make a new script for searching
Code:
1234
require('user.hue') -- get bridge to search for new lights result = searchHueLights() log(result)
the log showing that the search is started.

And a new script for reading data
Code:
1234
require('user.hue') -- get all sensordata from bridge result = getHueSensors() log(result)
I get a looooong string, but how can i break it up into the pieces that I want?

 Output from Log window, (it all comes in one long line in the log window starting with * string: {...   ) 
Code:
1234
Read Hue devices 14.04.2022 17:50:36 * table: [1]   * string: {"1":{"state":{"daylight":null,"lastupdated":"none"},"config":{"on":true,"configured":false,"sunriseoffset":30,"sunsetoffset":-30},"name":"Daylight","type":"Daylight","modelid":"PHDL00","manufacturername":"Signify Netherlands B.V.","swversion":"1.0"},"2":{"state":{"presence":false,"lastupdated":"2022-04-14T15:29:10"},"swupdate":{"state":"noupdates","lastinstall":"2022-04-13T18:57:32"},"config":{"on":true,"battery":100,"reachable":true,"alert":"none","sensitivity":2,"sensitivitymax":4,"ledindication":false,"usertest":false,"pending":[]},"name":"OutdoorSensor","type":"ZLLPresence","modelid":"SML004","manufacturername":"Signify Netherlands B.V.","productname":"Hue outdoor motion sensor","swversion":"2.53.6","uniqueid":"00:17:88:01:0b:d1:3a:b5-02-0406","capabilities":{"certified":true,"primary":true}},"3":{"state":{"lightlevel":7186,"dark":true,"daylight":false,"lastupdated":"2022-04-14T15:50:23"},"swupdate":{"state":"noupdates","lastinstall":"2022-04-13T18:57:32"},"config":{"on":true,"battery":100,"reachable":true,"alert":"none","tholddark":16000,"tholdoffset":7000,"ledindication":false,"usertest":false,"pending":[]},"name":"Hue outdoor light sensor 1","type":"ZLLLightLevel","modelid":"SML004","manufacturername":"Signify Netherlands B.V.","productname":"Hue outdoor light sensor","swversion":"2.53.6","uniqueid":"00:17:88:01:0b:d1:3a:b5-02-0400","capabilities":{"certified":true,"primary":false}},"4":{"state":{"temperature":2410,"lastupdated":"2022-04-14T15:48:19"},"swupdate":{"state":"noupdates","lastinstall":"2022-04-13T18:57:32"},"config":{"on":true,"battery":100,"reachable":true,"alert":"none","ledindication":false,"usertest":false,"pending":[]},"name":"Hue outdoor temp. sensor 1","type":"ZLLTemperature","modelid":"SML004","manufacturername":"Signify Netherlands B.V.","productname":"Hue outdoor temp. sensor","swversion":"2.53.6","uniqueid":"00:17:88:01:0b:d1:3a:b5-02-0402","capabilities":{"certified":true,"primary":false}}}

Let's say that I'm interested in the lightlevel and temperature
I can't find the fomat to extract the data for now.
Any thoughts how to extract?

Attached Files Thumbnail(s)
   
Reply
#2
Hi,

funny thing I just started working on the same topic today. :-)

I got it working for the motion state:

1.) Add the following code to the hue user library script:

Code:
1234567891011121314151617
-- function get data from sensors function getHueSensors()   local response = {}   body_searchHueSensors= ''   -- Set username for LogicMachine   socket.http.request({   url = "http://"..ip_add.."/api/"..user.."/sensors",   method = 'GET',         sink = ltn12.sink.table(response),   headers = {           ['content-length'] = #body_searchHueSensors,           ['content-type'] = 'application/json',   },           source = ltn12.source.string(body_searchHueSensors),   })   return response end

2.) Implement a resident script (example for motion):

Code:
123456789101112131415161718192021222324252627282930313233343536373839
addressmapping = {   -- Name                  -- Presence FB          ['Hue motion 1'] = {state = '47/2/6', statevalue = ''}, } -- Use logging logging = true require('user.hue') require('json')   reply = getHueSensors()   mysensors = json.pdecode(reply[1])   for _, item in pairs(mysensors) do     -- Check if sensor is found by bridge otherwise it make no sense to send commands     if item.config.reachable == true then       -- On/Off       name = addressmapping[item.name]       if name then         addr = addressmapping[item.name]['state']         if addr and addr ~= '' then           currentvalue = addressmapping[item.name]['statevalue']           if currentvalue ~= item.state.presence then             grp.update(addr, item.state.presence)             addressmapping[item.name]['statevalue'] = item.state.presence             if logging == true then               log('sensor ' .. item.name .. ' state is: ' .. tostring(item.state.presence))             end           end         end       end      end   end

If you want to have the temperature just looking for the name inside the json e.g. = Hue outdoors temp. sensor 1" and change: "item.state.presence" to "item.state.temperature". This is working I tested it. Due to I just want to have the motion state I didn't implemented it.

To get the json "readable" I used: https://jsonformatter.org/json-parser

I hope this is helping you.

Best Regards
Steffen
Reply
#3
In getHueSensors you need to replace return response with return table.concat(response)
Then replace mysensors = json.pdecode(reply[1]) with mysensors = json.pdecode(reply)

Otherwise your script will start failing if the response becomes larger.
Reply


Forum Jump: