Page 1 of 1

Updated dzVent HUE monitoring script.

Posted: Monday 14 October 2019 17:00
by BOverdevest
Hi.

I updated the HUE script in the Domoticz Wiki.
https://www.domoticz.com/wiki/Simple_sc ... Hue_status
(2nd to last script)

Reason is that lamps with colors (like Ikea's white spectrum bulbs) do not show "ON" as device state but show as "Set Color" in Domoticz. Because of this, lights that are physically switched off in the house, remain on in both HUE app and Domoticz.

I solved this by changing the logic from
device.state == "On"
to
device.state ~= 'Off'
Below the updated code as is now in Wiki.

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 ~= 'Off') then --changed from device.state == "On"
                            device.switchOff().checkFirst()
                        elseif( light.state.reachable == false and device.state ~= 'Off') then --changed from device.state == "On"
                            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
}