Philips Hue api v2 - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Philips Hue api v2 (/showthread.php?tid=3846) |
Philips Hue api v2 - gjniewenhuijse - 02.02.2022 Has anyone already made any code for the Philips Hue api V2? Why the v2 api? because i want to trigger dynamic scenes and thats not possible in the v1 api. Please correct my code is you see improvements. Code: --[[ RE: Philips Hue api v2 - gjniewenhuijse - 02.02.2022 Can anyone help me to setup an evenstream connection? Migration Guide to the new Hue API - Philips Hue Developer Program (meethue.com) The V2 API supports proactive notifications on changes through Server-Sent Events (SSE) under the /eventstream endpoint: curl --insecure -N -H 'hue-application-key: <appkey>' -H 'Accept: text/event-stream' https://<ipaddress>/eventstream/clip/v2 Events have an id, timestamp, type (‘update’, ‘add’, ‘delete’, ‘error’), and data field which contains the changed properties of the resource in the same format as a GET response on the same resource type. The following is an example event stream that would result from turning a light on and off: id: 1617322504:0 data: [{"creationtime":"2021-04-02T00:15:04Z","data":[{"id":"4413c8fd-6643-48b5-ad02-59453edf8a61","id_v1":"/lights/1","on":{"on":true},"type":"light"}],"id":"19845c30-2e4c-4205-a7b4-8bd496f3407d","type":"update"}] id: 1617322505:0 data: [{"creationtime":"2021-04-02T00:15:05Z","data":[{"id":"4413c8fd-6643-48b5-ad02-59453edf8a61","id_v1":"/lights/1","on":{"on":false},"type":"light"}],"id":"bea68344-a36c-4bfd-a658-97830e4e2b1a","type":"update"}] On HTTP1.1, you will need a separate connection for the SSE request and regular requests, but we recommend using HTTP2 to multiplex them over a single connection which is more resource efficient. Currently there is a 1 second rate limit on the amount of event containers the Bridge will send. If the same property has changed twice within that timeframe, you only get the last state. If multiple resources have changed within that timeframe, you will get multiple events grouped in a single container. RE: Philips Hue api v2 - admin - 02.02.2022 You can adapt this code for your use case: https://forum.logicmachine.net/showthread.php?tid=2508&pid=15941#pid15941 Add headers to initial request lines and change APPKEY to the actual application key: Code: sock:send( RE: Philips Hue api v2 - gjniewenhuijse - 02.02.2022 (02.02.2022, 15:53)admin Wrote: You can adapt this code for your use case: https://forum.logicmachine.net/showthread.php?tid=2508&pid=15941#pid15941 i think i do something wrong because the output of the lines is the same as the bridge html source what i see in the browser Code: if not sock then RE: Philips Hue api v2 - admin - 03.02.2022 Maybe some extra headers are needed. What do you get in the output? RE: Philips Hue api v2 - gjniewenhuijse - 03.02.2022 (03.02.2022, 07:21)admin Wrote: Maybe some extra headers are needed. What do you get in the output? same url in the browser RE: Philips Hue api v2 - admin - 03.02.2022 It can return 404 if the appkey is incorrect. RE: Philips Hue api v2 - gjniewenhuijse - 03.02.2022 (03.02.2022, 09:22)admin Wrote: It can return 404 if the appkey is incorrect. but its the same appkey as i use for the normal connection from the other example. And there it works. with curl the code is: curl --insecure -N -H 'hue-application-key: <appkey>' -H 'Accept: text/event-stream' https://<ipaddress>/eventstream/clip/v2 I see https.. Do the example also setup a https connection? and is this to port 80 or 443? RE: Philips Hue api v2 - admin - 03.02.2022 Does it work for you with curl? HTTPS is 443 but some extra code is needed for the raw socket to use encryption: https://forum.logicmachine.net/showthread.php?tid=206&pid=903#pid903 RE: Philips Hue api v2 - gjniewenhuijse - 03.02.2022 (03.02.2022, 09:50)admin Wrote: Does it work for you with curl? HTTPS is 443 but some extra code is needed for the raw socket to use encryption: https://forum.logicmachine.net/showthread.php?tid=206&pid=903#pid903 yep, curl for windows code works: curl --insecure -N -H "hue-application-key: sameapiuserasbefore" -H "Accept: text/event-stream" https://myipaddress/eventstream/clip/v2 and thx, the raw socket example works also. working code for evenstream Code: if not sock then RE: Philips Hue api v2 - gjniewenhuijse - 03.02.2022 parsing the output is another thing: line output: data: [{"creationtime":"2022-02-03T13:35:32Z","data":[{"id":"f2ef58b3-818e-45ea-a291-325e8eeb97bc","id_v1":"/lights/10","on":{"on":false},"owner":{"rid":"0120cec8-b58e-4206-a409-2bd99055af55","rtype":"device"},"type":"light"}],"id":"49142b93-3da7-49dc-9ad6-10809df8fe9d","type":"update"}] Code: line = string.sub(line,8,-2) -- remove "data: [" and the last ] character But the second output combines more lines, how to decode this line? line output: data: [{"creationtime":"2022-02-03T13:35:32Z","data":[{"id":"728be63c-dc61-42b1-9a84-54daeff76046","id_v1":"/groups/0","on":{"on":false},"type":"grouped_light"}],"id":"b201d09a-0ab2-4a60-b72f-3a099841ebbc","type":"update"},{"creationtime":"2022-02-03T13:35:32Z","data":[{"id":"33b4f136-c59e-4270-83af-814c4f5ba856","id_v1":"/groups/18","on":{"on":false},"type":"grouped_light"}],"id":"e2860e1b-ebd0-40b0-81ed-f1c0b27163a3","type":"update"},{"creationtime":"2022-02-03T13:35:32Z","data":[{"id":"7a8f900c-c312-403a-bf73-9d2fcb005489","id_v1":"/groups/19","on":{"on":false},"type":"grouped_light"}],"id":"1cc814e9-9210-4e75-92c0-29a543c32dcb","type":"update"}] RE: Philips Hue api v2 - Joep - 10.01.2023 I would like to contribute on this and started with the Hue discovey by changing the script i found from https://forum.logicmachine.net/showthread.php?tid=3751&pid=24214#pid24214 Below the modified version that gives all the Hue Bridge information with most important the IP address. Code: hue = io.readproc('avahi-browse _hue._tcp') RE: Philips Hue api v2 - Fcs - 04.11.2024 Hi @gjniewenhuijse, Have you been able to make any progress on using api V2? like you, I need to use dynamic scenes and motion sensors. thanks you RE: Philips Hue api v2 - gjniewenhuijse - 05.11.2024 (04.11.2024, 14:17)Fcs Wrote: Hi @gjniewenhuijse, I made some code to use the api v2 Code: -- hue v2 api begin -------------------------------------------------------------------------------------------------- and tested with the event handler Code: --[[ |