Philips Hue Status Updates

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Post Reply
limar
Posts: 6
Joined: Thursday 31 January 2019 21:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Philips Hue Status Updates

Post by limar »

Wasn't sure, but it seemed that the script behind the below URL had unneeded dependencies (required to install libraries) as it can be done with everything that's available as part of dzVents already (dzVents was upgraded in the meantime I guess). Pasted my code below for others to use. One for the groups (that actually are in Domoticz as devices as part of the default integration) and one for lights. Note that for groups I implemented an if statement to only check for groups with type=room. This can easily be removed if unwanted.
https://www.domoticz.com/wiki/Simple_sc ... Hue_status

Lights

Code: Select all

return {
	on = {
	    timer = {'every minute'},
        httpResponses = { 'huetrigger' }
	},
    data = {
    previousData = { 
                    history = true, 
                    maxMinutes  = 5
                    }
    },
    execute = function(domoticz,item)
        if (item.isTimer) then
            domoticz.log('timerworks')
            local hueBridgeIP = '192.168.2.XXX'
            local hueBridgeAPI = 'XXXXXXXXXXXXXXXXX'
            
            local url = string.format("http://%s/api/%s/lights", hueBridgeIP, hueBridgeAPI)
            domoticz.log(url)
            
            domoticz.openURL({
                url = url,
                method = 'GET',
                callback = 'huetrigger'
            })
        end
        if (item.isHTTPResponse) then
            if (item.isJSON) then
                local lights = domoticz.utils.fromJSON(item.data)
                local previousData = domoticz.data.previousData
        
               for i, light in pairs(lights) do
                    if (domoticz.devices( light.name ) ~= nil) then
                        local device = domoticz.devices( light.name )
                        if( light.state.on == true and device.state == 'Off') then
                            device.switchOn().checkFirst()
                        elseif( light.state.on == false and device.state == 'On') then
                            device.switchOff().checkFirst()
                        elseif( light.state.reachable == false and device.state == 'On') then
                            previousData.add( light.name )
                             
                            local olderItems = previousData.subsetSince('00:05:00')
                            local count = olderItems.reduce(function(acc, item)
                                if (item.data == light.name) then
                                    acc = acc + 1
                                end
                                return acc
                            end, 0)
        
                            if( count > 5 ) then -- last 5 polls were unreachable, assuming light is off
                               device.switchOff().checkFirst()
                            end
                        end
                    end
                end
            else
                domoticz.log('noJSON',item.data)
            end
        end
    end
}
Groups

Code: Select all

return {
	on = {
	    timer = {'every minute'},
        httpResponses = { 'huetrigger' }
	},
    data = {
    previousData = { 
                    history = true, 
                    maxMinutes  = 5
                    }
    },
    execute = function(domoticz,item)
        if (item.isTimer) then
            domoticz.log('timerworks')
            local hueBridgeIP = '192.168.2.XXX'
            local hueBridgeAPI = 'XXXXXXXXXX'
             
            local url = string.format("http://%s/api/%s/groups/", hueBridgeIP, hueBridgeAPI)
            
            domoticz.openURL({
                url = url,
                method = 'GET',
                callback = 'huetrigger'
            })
        end
        if (item.isHTTPResponse) then
            if (item.isJSON) then
                local huegroups = domoticz.utils.fromJSON(item.data)
                local previousData = domoticz.data.previousData
        
                for i, huegroup in pairs(huegroups) do
                    if ( huegroup.type == 'Room' ) then
                        if (domoticz.devices( "Group "..huegroup.name ) ~= nil) then
                            local device = domoticz.devices( "Group "..huegroup.name )
                            if( huegroup.state.any_on == true and device.state == 'Off') then
                                device.switchOn().checkFirst()
                            elseif( huegroup.state.any_on == false and device.state == 'On') then
                                device.switchOff().checkFirst()
                            end
                        end
                    end
                end
            else
                domoticz.log('noJSON',item.data)
            end
        end
    end
}
[2019-02-09: added .checkFirst() to all switch commands]
Last edited by limar on Saturday 09 February 2019 0:28, edited 1 time in total.
rotero
Posts: 10
Joined: Saturday 13 June 2015 13:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.2284
Contact:

Re: Philips Hue Status Updates

Post by rotero »

Hi,

Thanks for this script.

Still have a problem with it........
I changed the names of the Hue bulbs so they are the same in Hue app and Domoticz.
I entered the correct IP address of the Hue Brigde after: local hueBridgeIP =
I am not sure what to enter after: local hueBridgeAPI =
Should I enter my just created Hue Developer account? I created the account, but I didn't add something to my account.

This is the error I get now:
2019-02-06 14:07:01.268 Status: dzVents: Info: Handling httpResponse-events for: "huetrigger
2019-02-06 14:07:01.269 Status: dzVents: Info: ------ Start internal script: HUE STATUS UPDATE: HTTPResponse: "huetrigger"
2019-02-06 14:07:01.283 Status: dzVents: Error (2.4.6): An error occured when calling event handler HUE STATUS UPDATE
2019-02-06 14:07:01.283 Status: dzVents: Error (2.4.6): .../scripts/dzVents/generated_scripts/HUE STATUS UPDATE.lua:35: attempt to index field 'state' (a nil value)
2019-02-06 14:07:01.284 Status: dzVents: Info: ------ Finished HUE STATUS UPDATE


Please help.

Regards,
Rob
limar
Posts: 6
Joined: Thursday 31 January 2019 21:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Philips Hue Status Updates

Post by limar »

Hi Rotero,
You need to whitelist yourself on the bridge, by creating an access key/username. This is all described on https://developers.meethue.com/ (click the Develop button and register). Then visit the get started section.

In short:
* Open your browser and enter the ip address (when you're at home) of your bridge and add /debug/clip.html so http://192.168.2.X/debug/clip.html (ip can be found using the app: settings > Hue Bridges > i-icon > IP address)
* You should see a page where you can send commands to the bridge. Enter the following details:
URL /api
Body {"devicetype":"domoticzupdates"}
Method POST

After pressing the POST button you should be returned with an username/access key. That should be the value you need to get it up and running.
rotero
Posts: 10
Joined: Saturday 13 June 2015 13:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.2284
Contact:

Re: Philips Hue Status Updates

Post by rotero »

Thanks limar!

It's working now. I missed the moment for pressing the link switch on the Hue Bridge.

In short (for other newbies):
- Open your browser and go to: http://your_ip_address/debug/clip.html (example http://192.168.178.222/debug/clip.html)
(ip can be found using the app: settings > Hue Bridges > i-icon > IP address)
- Enter the following in the 'CLIP API Debugger' screen:
- in the URL box: /api
- in the Message Body box: {"devicetype":"domoticzupdates"}
- Now press the hardware 'link' button on your Philips Hue Bridge
- Click on the POST button on the 'CLIP API Debugger' screen

Now you receive the username between quotes. You should use this username in the command in limar's script:

local hueBridgeAPI = 'the_received_username'
(example local hueBridgeAPI = 'W35p2QKSorYG2eQSnhd2f5LsoghQCEb7u726NV9z')

Good luck and thanks again limar!

Regards,
Rob
limar
Posts: 6
Joined: Thursday 31 January 2019 21:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Philips Hue Status Updates

Post by limar »

Ah yes. pressing the button :-) . Forgot to add.
nachonam
Posts: 1
Joined: Sunday 14 November 2021 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Philips Hue Status Updates

Post by nachonam »

Hello,

A little modification if you don't want to have the name of the Hue bulbs at the same in Hue app and Domoticz.
WARNING: You need to modify local hueBridgeID var with the id of your HueBridge

Regards

Code: Select all

return {
	on = {
	    timer = {'every minute'},
        httpResponses = { 'huetrigger' }
	},
    data = {
         previousData = { 
                    history = true, 
                    maxMinutes  = 5
                    }
    },
    logging = {
		level = domoticz.LOG_INFO,
		marker = 'huetrigger v2',
	},
    execute = function(domoticz,item)
            local hueBridgeID = 6  -- WARNING Hardcoded
            
            if (item.isTimer) then
                domoticz.log('timerworks')
				local hueBridgeIP = '192.168.2.XXX'
				local hueBridgeAPI = 'XXXXXXXXXXXXXXXXX'

                
                local url = string.format("http://%s/api/%s/lights", hueBridgeIP, hueBridgeAPI)
                domoticz.log(url)
                
                domoticz.openURL({
                    url = url,
                    method = 'GET',
                    callback = 'huetrigger'
                })
            end
            if (item.isHTTPResponse) then
                if (item.isJSON) then
                    local lights = domoticz.utils.fromJSON(item.data)
                    local previousData = domoticz.data.previousData
                    local configData = domoticz.data.configData
                    
                   for i, light in pairs(lights) do
                          local myDevice = domoticz.devices().find(function(device)
                                return ( (device.deviceId == string.format("%08d", i)) and   (device.hardwareID == hueBridgeID) )  --local hueBridgeID = 6 Hardcoded
                            end)
                        if (( myDevice ~= nil) and (domoticz.devices( myDevice.name ) ~= nil) ) then
                            local device = domoticz.devices( myDevice.name )
                            if( light.state.on == true and device.state == 'Off') then
                                device.switchOn().checkFirst()
                            elseif( light.state.on == false and device.state ~= 'Off') then
                                device.switchOff().checkFirst()
                            elseif( light.state.reachable == false and device.state ~= 'Off') then
                                previousData.add( light.name )
                                 
                                local olderItems = previousData.subsetSince('00:05:00')
                                local count = olderItems.reduce(function(acc, item)
                                    if (item.data == light.name) then
                                        acc = acc + 1
                                    end
                                    return acc
                                end, 0)
            
                                if( count > 5 ) then -- last 5 polls were unreachable, assuming light is off
                                   device.switchOff().checkFirst()
                                end
                            end
                        end
                    end
                else
                    domoticz.log('noJSON',item.data)
                end
            end
    end
}

Sarcas
Posts: 86
Joined: Wednesday 11 October 2017 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1ß
Location: Friesland
Contact:

Re: Philips Hue Status Updates

Post by Sarcas »

nachonam wrote: Sunday 14 November 2021 8:58 A little modification if you don't want to have the name of the Hue bulbs at the same in Hue app and Domoticz.
WARNING: You need to modify local hueBridgeID var with the id of your HueBridge
Tried it, but some of my bulbs (Ikea on Hue bridge) toggle on and off every minute. No idea why, I disabled it for now.
--

Domoticz on rPi4 - RFXCOM RFXtrx433 USB - ZW090 Z-Stick Gen5 EU - IKEA Tradfri - Philips HUE - YouLess meter - SolarEdge
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests