Presence detection using UniFi Controller and DzVents  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

User avatar
Hesitated
Posts: 42
Joined: Monday 04 June 2018 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.11665
Location: Ede
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Hesitated »

Sadly it doesn't work for me either:
2020-06-02 17:27:00.438 Status: dzVents: Info: Opvragen Unifi: ------ Start internal script: Unifi:, trigger: "every minute"
2020-06-02 17:27:00.439 Status: dzVents: Debug: OpenURL: url = https://unifi/api/login
2020-06-02 17:27:00.439 Status: dzVents: Debug: OpenURL: method = POST
2020-06-02 17:27:00.439 Status: dzVents: Debug: OpenURL: post data = {"password":"...","username":"..."}
2020-06-02 17:27:00.439 Status: dzVents: Debug: OpenURL: headers = {["Content-Type"]="application/json"}
2020-06-02 17:27:00.439 Status: dzVents: Debug: OpenURL: callback = unifi_loggedin
2020-06-02 17:27:00.439 Status: dzVents: Info: ------ Finished Unifi
2020-06-02 17:27:00.440 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-06-02 17:27:01.049 Status: dzVents: Info: Opvragen Unifi: ------ Start internal script: Unifi: HTTPResponse: "unifi_loggedin"
2020-06-02 17:27:01.049 Status: dzVents: Debug: OpenURL: url = https://unifi/api/s/default/stat/sta
2020-06-02 17:27:01.050 Status: dzVents: Debug: OpenURL: method = GET
2020-06-02 17:27:01.050 Status: dzVents: Debug: OpenURL: post data = nil
2020-06-02 17:27:01.050 Status: dzVents: Debug: OpenURL: headers = nil
2020-06-02 17:27:01.050 Status: dzVents: Debug: OpenURL: callback = unifi_data
2020-06-02 17:27:01.050 Status: dzVents: Info: ------ Finished Unifi
2020-06-02 17:27:01.050 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-06-02 17:27:01.456 Status: dzVents: Info: Opvragen Unifi: ------ Start internal script: Unifi: HTTPResponse: "unifi_data"
2020-06-02 17:27:01.456 Status: dzVents: Debug: No HTTPResponse or not ok
2020-06-02 17:27:01.456 Status: dzVents: Info: ------ Finished Unifi
In the error tab:
2020-06-02 17:27:01.377 Error: Error opening url: https://unifi/api/s/default/stat/sta
It looks like the login succeeds, but nothing gets returned.

Any ideas?

The script that I use:

Code: Select all

local const = require "consts"

local urlUnifi  = 'https://unifi/api'

local knownDevices = {
  ['80:58:f8:92:5f:6b'] = { idx=525, oui='Motorola'},
  ['dc:4f:22:76:73:57'] = { idx=526, oui='', hostname='OnePlus7T'},
}



return { 
  on = {
    timer = { 
      'every minute'
    },
    httpResponses = { 'unifi_loggedin' , 'unifi_data' }
  },
  logging = { 
    level = domoticz.LOG_DEBUG,
    marker = "Opvragen Unifi" 
  },
  execute = function(dz, item)
    _G.logMarker =  _G.moduleLabel
    local utils = dz.utils
    
    if (item.isTimer or item.isDevice) then
      dz.openURL({
        url = urlUnifi .. '/login',
        method = 'POST',
        postData = {
          ['password'] = const.unifiPassword, 
          ['username'] = const.unifiUser 
        },
        callback = 'unifi_loggedin'
      })
      return;
    end

    if (not (item.isHTTPResponse and item.ok)) then
        dz.log('No HTTPResponse or not ok', dz.LOG_DEBUG)    
      return;
    end    

    if (item.trigger == 'unifi_loggedin') then
      dz.openURL({
        url = urlUnifi .. '/s/default/stat/sta',
        method = 'GET',
        callback = 'unifi_data'
      })
      return
    end
    
    lodash = dz.utils._
    if (item.trigger == 'unifi_data') then
      local json = utils.fromJSON(item.data)
      if json == nil then
        dz.log('item.data is nil ', dz.LOG_DEBUG)
        return
      end
      
      print(json.data)
      for i, item in ipairs(json.data) do
        local device = knownDevices[item.mac]
        if device ~= nil then
          if device.idx ~= nil then
              print(device.idx)
            --dz.devices(device.idx). <----- TODO
          end
        else
          dz.log('mac: ' .. item.mac .. ' - oui: ' .. item.oui .. '- hostname: ' .. item.hostname, dz.LOG_DEBUG)
        end
      end
    end
  end 
}
Domoticz 2020.2 - RPi3B+ - RFXtrx433XL - P1 - CC2531 - Somfy - Xiaomi - Hue - Omron - MQTT
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

Check the url when you are using your controller in a browser:
//unifi/api/s/default/stat/sta

Probably you do not use the 'default' setting in your controller. Replace 'default' by your specific name you van see in the address bar in your browser.
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

I'm using the script that was posted in this discussion thread for some time now. What I notice when checking the error log is an error about the SSH remote key not OK. Is this a problem? How can I fix this? When using the https://192.168.0.25:8443/api/s/default/stat/sta straight into my browse it is working fine. Any suggestions?

Code: Select all

2020-10-04 08:00:04.874 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 08:46:01.738 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 08:46:01.812 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 08:53:02.411 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 08:53:02.509 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 12:02:01.489 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 12:02:01.565 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 12:03:01.408 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 12:03:01.487 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 12:04:01.592 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 12:04:01.666 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 13:38:02.364 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 13:38:02.440 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 13:39:01.608 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 13:39:01.680 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-04 13:40:01.574 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-04 13:40:01.654 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

I'm not sure but from the error log you shared, I can see you run the script every minute. What I do not understand is why the error is not showing every minute. Or did you share just parts of the error log?

If the error is not showing every minute (or not every time the script runs) I don't think the error is in the script itself.

Do you run the script every minute? And does presence detection work most of the time but you only get errors in your log?
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

Delfuego wrote: Tuesday 06 October 2020 11:40 I'm not sure but from the error log you shared, I can see you run the script every minute. What I do not understand is why the error is not showing every minute. Or did you share just parts of the error log?

If the error is not showing every minute (or not every time the script runs) I don't think the error is in the script itself.

Do you run the script every minute? And does presence detection work most of the time but you only get errors in your log?
The script runs every minute. I only see the error message now and then. At least not every minute. This is a bit strange to me. In many occasions it seems to be working fine.
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

Maybe you can add this line of code to get some more info on what is happening during script launch. (put it at the top of your code near 'data = ...')

Code: Select all

    logging = { level = domoticz.LOG_DEBUG, marker = 'Unifi', }, 
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

Ok, some more debug code and information on this error message.

The error message that i see on regular basis is (not always, not every minute).

Code: Select all

2020-10-07 10:28:02.146 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 401 ==>> No reason returned!
The matching debugging information:

Code: Select all

2020-10-07 10:27:00.457 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Start internal script: CheckiPhoneAtHome2:, trigger: "every 1 minutes"
2020-10-07 10:27:00.469 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: url = https://192.168.0.25:8443/api/login
2020-10-07 10:27:00.469 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: method = POST
2020-10-07 10:27:00.469 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: post data = {"password":"XXXXXXXXX","username":"XXXXXXXXXX"}
2020-10-07 10:27:00.469 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: headers = {["Content-Type"]="application/json"}
2020-10-07 10:27:00.469 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: callback = unifi_loggedin
2020-10-07 10:27:00.473 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Finished CheckiPhoneAtHome2
2020-10-07 10:27:00.545 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-07 10:27:01.472 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Start internal script: CheckiPhoneAtHome2: HTTPResponse: "unifi_loggedin"
2020-10-07 10:27:01.483 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: url = https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-07 10:27:01.483 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: method = GET
2020-10-07 10:27:01.483 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: post data = nil
2020-10-07 10:27:01.483 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: headers = nil
2020-10-07 10:27:01.483 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: callback = unifi_data
2020-10-07 10:27:01.486 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Finished CheckiPhoneAtHome2
2020-10-07 10:27:01.486 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-07 10:27:01.673 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Start internal script: CheckiPhoneAtHome2: HTTPResponse: "unifi_data"
2020-10-07 10:27:01.713 Status: dzVents: Debug: CheckiPhoneAtHome2: Processing device-adapter for iPhoneS: Switch device adapter
2020-10-07 10:27:01.713 Status: dzVents: Debug: CheckiPhoneAtHome2: Constructed timed-command: Off
2020-10-07 10:27:01.714 Status: dzVents: Debug: CheckiPhoneAtHome2: Processing device-adapter for iPhoneL: Switch device adapter
2020-10-07 10:27:01.714 Status: dzVents: Debug: CheckiPhoneAtHome2: Constructed timed-command: Off
2020-10-07 10:27:01.715 Status: dzVents: Debug: CheckiPhoneAtHome2: Processing device-adapter for iPhoneI: Switch device adapter
2020-10-07 10:27:01.715 Status: dzVents: Debug: CheckiPhoneAtHome2: Constructed timed-command: Off
2020-10-07 10:27:01.716 Status: dzVents: Debug: CheckiPhoneAtHome2: Processing device-adapter for iPhoneD: Switch device adapter
2020-10-07 10:27:01.716 Status: dzVents: Debug: CheckiPhoneAtHome2: Constructed timed-command: On
2020-10-07 10:27:01.717 Status: dzVents: Debug: CheckiPhoneAtHome2: Processing device-adapter for iPhoneN: Switch device adapter
2020-10-07 10:27:01.717 Status: dzVents: Debug: CheckiPhoneAtHome2: Constructed timed-command: Off
2020-10-07 10:27:01.720 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Finished CheckiPhoneAtHome2
2020-10-07 10:27:56.267 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua

2020-10-07 10:28:00.505 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Start internal script: CheckiPhoneAtHome2:, trigger: "every 1 minutes"
2020-10-07 10:28:00.513 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: url = https://192.168.0.25:8443/api/login
2020-10-07 10:28:00.513 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: method = POST
2020-10-07 10:28:00.513 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: post data = {"password":"XXXXXXXXX","username":"XXXXXXXXX"}
2020-10-07 10:28:00.513 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: headers = {["Content-Type"]="application/json"}
2020-10-07 10:28:00.513 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: callback = unifi_loggedin
2020-10-07 10:28:00.516 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Finished CheckiPhoneAtHome2
2020-10-07 10:28:00.585 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-07 10:28:01.509 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Start internal script: CheckiPhoneAtHome2: HTTPResponse: "unifi_loggedin"
2020-10-07 10:28:01.516 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: url = https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-07 10:28:01.516 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: method = GET
2020-10-07 10:28:01.516 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: post data = nil
2020-10-07 10:28:01.516 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: headers = nil
2020-10-07 10:28:01.516 Status: dzVents: Debug: CheckiPhoneAtHome2: OpenURL: callback = unifi_data
2020-10-07 10:28:01.518 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Finished CheckiPhoneAtHome2
2020-10-07 10:28:01.518 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-07 10:28:02.139 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Start internal script: CheckiPhoneAtHome2: HTTPResponse: "unifi_data"
2020-10-07 10:28:02.148 Status: dzVents: Info: CheckiPhoneAtHome2: ------ Finished CheckiPhoneAtHome2
2020-10-07 10:28:26.881 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
The first time the script is executed successfully all the way up to processing the MAC addresses. The " Processing device-adapter" debug information for the vairous MAC addresses is clearly visible.

The second time, there is no processing of the MAC addresses, the " Processing device-adapter" is missing in the debug info. It seems that there is response from the "...../api/s/default/stat/sta" URL call but it is not a list of MAC addresses. I cannot see the error message or the URL response, that is not shown in the logging.

Any suggestions on why it sometimes fails?
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

I don't know what causes the error. I'm sorry.

I can see that you've adjusted the script to your needs. Do you get the same error when you are using my basic script?
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

Delfuego wrote: Wednesday 07 October 2020 14:54 I can see that you've adjusted the script to your needs. Do you get the same error when you are using my basic script?
Script is not really modified, apart from the MAC address list. Still the same as the original script in my view. What is different according to you?

Code: Select all

return {
 	logging = {
 		level = domoticz.LOG_ERROR, --DEBUG, --ERROR, 		-- Adjust to your needs
 		marker = 'CheckiPhoneAtHome2'
 	},
    on = {
        timer = { 
            'every 1 minutes' 
        },
        httpResponses = { 
            'unifi_loggedin',
            'unifi_data' 
        }
    },
    data = {
        presenceHistory = { 
            history = true, 
            maxMinutes = 6
        } 		-- Change maxMinutes value to appropriate for your devices
    },
    execute = function(domoticz, item)
        local controllerUrl = 'https://192.168.0.25:8443/' 	--Change IP and Port to your Unifi Controller settings
        local controllerLogin = 'XXXXXXX'						-- Your username for Unifi Controller
        local controllerPassword = 'XXXXXXXX' 				-- Your password for Unifi Controller

        local devices = {
            ['XXXXXXXXXXXXXX'] = 91,     -- iPhoneD
            ['XXXXXXXXXXXXXX'] = 110,    -- iPhoneS
            ['XXXXXXXXXXXXXX'] = 111,    -- iPhoneI
            ['XXXXXXXXXXXXXX'] = 108,    -- iPhoneL
            ['XXXXXXXXXXXXXX] = 109     -- iPhoneN
        }

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword, 
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
        
        if (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'unifi_loggedin') then
                domoticz.openURL({
                    url = controllerUrl .. 'api/s/default/stat/sta',
                    method = 'GET',
                    callback = 'unifi_data'
                })
            end
        
            if (item.trigger == 'unifi_data') then
                local presenceHistory = domoticz.data.presenceHistory

                for mac, device in pairs(devices) do
                    local isPresent = string.find(item.data, mac)
                    
                    presenceHistory.add({
                        ['mac'] = mac,
                        ['isPresent'] = not (isPresent == nil)
                    })
                
                    local wasPresent = presenceHistory.reduce(function(acc, item)
                        if (item.data.mac == mac and item.data.isPresent) then
                            acc = acc + 1
                        end
                        
                        return acc
                    end, 0)
                                       
                    if (wasPresent == 0) then
                        domoticz.devices(device).switchOff().checkFirst()
                    else
                        domoticz.devices(device).switchOn().checkFirst()
                    end
                end
            end
        end
    end
}
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

My fault! I Looked at the wrong post for the script you're using (in fact you didn't post your script at all before)!

And you are right; your script is/does the same. In fact it is more fancy because a phone has to be away for six minutes before it gets turned off (I myself build something similar in my own script). This way you prevent unnecessary actions because a phone loses WiFi contact for just a short period of time (and this often happens).

For your 'problem' I don't have a solution.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by waaren »

rgroothuis wrote: Wednesday 07 October 2020 10:46 I cannot see the error message or the URL response, that is not shown in the logging.
I added some log statements in below script. Maybe that give you enough extra information to identify the issue.

Code: Select all

return {
     logging = {
         level = domoticz.LOG_ERROR, --DEBUG, --ERROR,         -- Adjust to your needs
         marker = 'CheckiPhoneAtHome2'
     },
    on = {
        timer = { 
            'every 1 minutes' 
        },
        httpResponses = { 
            'unifi_loggedin',
            'unifi_data' 
        }
    },
    data = {
        presenceHistory = { 
            history = true, 
            maxMinutes = 6
        }         -- Change maxMinutes value to appropriate for your devices
    },
    execute = function(domoticz, item)
        local controllerUrl = 'https://192.168.0.25:8443/'     --Change IP and Port to your Unifi Controller settings
        local controllerLogin = 'XXXXXXX'                        -- Your username for Unifi Controller
        local controllerPassword = 'XXXXXXXX'                 -- Your password for Unifi Controller

        local devices = {
            ['XXXXXXXXXXXXXX'] = 91,     -- iPhoneD
            ['XXXXXXXXXXXXXX'] = 110,    -- iPhoneS
            ['XXXXXXXXXXXXXX'] = 111,    -- iPhoneI
            ['XXXXXXXXXXXXXX'] = 108,    -- iPhoneL
            ['XXXXXXXXXXXXXX] = 109     -- iPhoneN
        }

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword, 
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
        if item.isHTTPResponse and not(item.ok) then
            domoticz.log("problem with HTTPResponse", domoticz.LOG_ERROR)
            domoticz.log(item, domoticz.LOG_ERROR)
        elseif (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'unifi_loggedin') then
                domoticz.openURL({
                    url = controllerUrl .. 'api/s/default/stat/sta',
                    method = 'GET',
                    callback = 'unifi_data'
                })
            end
        
            if (item.trigger == 'unifi_data') then
                local presenceHistory = domoticz.data.presenceHistory

                for mac, device in pairs(devices) do
                    local isPresent = string.find(item.data, mac)
                    
                    presenceHistory.add({
                        ['mac'] = mac,
                        ['isPresent'] = not (isPresent == nil)
                    })
                
                    local wasPresent = presenceHistory.reduce(function(acc, item)
                        if (item.data.mac == mac and item.data.isPresent) then
                            acc = acc + 1
                        end
                        
                        return acc
                    end, 0)
                                       
                    if (wasPresent == 0) then
                        domoticz.devices(device).switchOff().checkFirst()
                    else
                        domoticz.devices(device).switchOn().checkFirst()
                    end
                end
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

Thanks, after comparing the scripts I see that the following three lines are added, is that correct?

if item.isHTTPResponse and not(item.ok) then
domoticz.log("problem with HTTPResponse", domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by waaren »

rgroothuis wrote: Thursday 08 October 2020 8:38 Thanks, after comparing the scripts I see that the following three lines are added, is that correct?

if item.isHTTPResponse and not(item.ok) then
domoticz.log("problem with HTTPResponse", domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
Yes and one if is changed to an elseif. These lines should provide you with some additional information when the response is not ok.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

Thanks again, I already got the following debug info:

Code: Select all

2020-10-08 09:00:04.566 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-10-08 09:00:04.648 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: HTTP/1.1 response: 60 ==>> SSL peer certificate or SSH remote key was not OK
2020-10-08 09:00:04.648 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: problem with HTTPResponse
2020-10-08 09:00:04.648 Error: dzVents: Error: (3.0.2) CheckiPhoneAtHome2: {["isGroup"]=false, ["_contentType"]="", ["protocol"]="HTTP/1.1", ["statusText"]="SSL peer certificate or SSH remote key was not OK", ["callback"]="unifi_data", ["baseType"]="httpResponse", ["isVariable"]=false, ["trigger"]="unifi_data", ["isDevice"]=false, ["isXML"]=false, ["isHardware"]=false, ["isHTTPResponse"]=true, ["isSystem"]=false, ["isSecurity"]=false, ["isTimer"]=false, ["isCustomEvent"]=false, ["statusCode"]=60, ["ok"]=false, ["headers"]={}, ["isJSON"]=false, ["isScene"]=false, ["data"]=""}
Seems there is an issue with the SSH remote key on the Unify side. But what is strange is that in many occasions it is working fine as expected.
andreasm80
Posts: 7
Joined: Thursday 24 November 2016 22:05
Target OS: Linux
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by andreasm80 »

Hi,

I also started to experience problems with this dzvent script. I think there has been a change from Unifi in one of the later versions. It has worked perfect for a couple of years, but recently stopped. Thanks to Waaren I am able to identify what it fails on, but I am not able to rectify it myself. I have tested all the api commands from Postman and verified that they work. I suspect that in the dzVents script it is not able to handle the json cookie received after the first authentication is done, or not able to use it at all to do the next api call, because I get "api.err.Login.Required". I am using the script Waaren posted a couple of post above.
Here is the complete debug:

Code: Select all

Error: dzVents: Error: (3.0.2) CheckPhoneAtHome2: problem with HTTPResponse
2020-11-29 13:22:00.990 Error: dzVents: Error: (3.0.2) CheckPhoneAtHome2: {["isScene"]=false, ["isJSON"]=true, ["isTimer"]=false, ["_contentType"]="application/json;charset=UTF-8", ["isHTTPResponse"]=true, ["json"]={["data"]={}, ["meta"]={["msg"]="api.err.LoginRequired", ["rc"]="error"}}, ["protocol"]="HTTP/1.1", ["trigger"]="unifi_data", ["isXML"]=false, ["isSecurity"]=false, ["isCustomEvent"]=false, ["isHardware"]=false, ["isDevice"]=false, ["isSystem"]=false, ["data"]="{"meta":{"rc":"error","msg":"api.err.LoginRequired"},"data":[]}", ["isVariable"]=false, ["headers"]={["Access-Control-Expose-Headers"]="Access-Control-Allow-Origin,Access-Control-Allow-Credentials", ["X-Frame-Options"]="DENY", ["Date"]="Sun, 29 Nov 2020 12:22:00 GMT", ["vary"]="Origin", ["Content-Length"]="63", ["Access-Control-Allow-Credentials"]="true", ["Content-Type"]="application/json;charset=UTF-8"}, ["statusText"]="No reason returned!", ["statusCode"]=401, ["isGroup"]=false, ["callback"]="unifi_data", ["ok"]=false, ["baseType"]="httpResponse"}


It would be great to get this going again.. So if anyone sees something obvious here please reach out.. :D

Thanks
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

I myself do not experience any problems with the script. I use one of the latest versions of Unifi controller (6.0.36) and I use Domoticz 2020.2.

Recently I have refreshed my script a bit (kind of watch routine built in because devices sometimes lose WiFi for a moment. I don't want to label these as absent. That's why a device has to be absent five times now before I turn it off) but structurally nothing has changed. Some real small change is that I now refer to ip 127.0.0.1. You can only do this if the controller and Domoticz are running on the same server. Otherwise I think you really need to refer to the ip-address of the unifi controller server. But this probably won't solve the problem either.

What I also did is create an admin account on the Unifi controller with read only rights. With this I now read Unifi from Domoticz.

For the sake of completeness I'll give you my current script below.

Code: Select all

return {
    on = {
        timer = {'every minute'},
            httpResponses = {'loggedin','data'} },
    data = { phoneHome = { initial = {} } },
   
    execute = function(domoticz, device)
        if (device.isTimer) then
            domoticz.openURL({
                url = 'https://127.0.0.1:8443/api/login',		-- Change 127.0.0.1 to your Unifi controllers IP if you run it on another device
                method = 'POST',
                postData = { ['password'] = 'XXXXXX' , ['username'] = 'XXXXXX' },  -- Provide username/password of your Unifi controller. I created a new Admin account with only Read Only rights
                callback = 'loggedin' })
        end
        if (device.isHTTPResponse and device.ok) then
            if (device.trigger == 'loggedin') then
                domoticz.openURL({
                url = 'https://127.0.0.1:8443/api/s/default/stat/sta',   -- Change 127.0.0.1 to your Unifi controllers IP if you run it on another device
                method = 'GET',
                callback = 'data' })
            else				
            						-- Replace YY with Domoticz virtual switch idx representing the phone (or other WiFi device)
             						-- Replace ZZ:ZZ:ZZ:ZZ:ZZ:ZZ with phones MAC adress. You can add as many idx-mac combinations as you like
                local UniFiDev = { [YY] = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ' , [YY] = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ', [YY] = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ', [YY] = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ' , [YY] = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ' , [YY] = 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ' }
                for DzIndx,Mac in pairs(UniFiDev)
                do
                    if (domoticz.data.phoneHome[DzIndx] == nil) then              --Initial value of the phone home if it does not exist
                        domoticz.data.phoneHome[DzIndx] = 5
                    end
                    if (string.find( device.data , Mac) == nil) then
                        domoticz.data.phoneHome[DzIndx] = domoticz.data.phoneHome[DzIndx] - 1
                        if (domoticz.data.phoneHome[DzIndx] <= 0) then      		--Extra delay because sometimes a phone is short disconnected from WiFi
                            domoticz.devices(DzIndx).switchOff().checkFirst()
                            domoticz.data.phoneHome[DzIndx] = 0 
                        end   
                    else
                        domoticz.devices(DzIndx).switchOn().checkFirst()
                        domoticz.data.phoneHome[DzIndx] = 5
                    end
                end    
            end
        end    
   end 
}
Last edited by Delfuego on Wednesday 02 December 2020 15:28, edited 1 time in total.
andreasm80
Posts: 7
Joined: Thursday 24 November 2016 22:05
Target OS: Linux
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by andreasm80 »

Hi Delfuego,

I appreciate your reply here. You verified that your setup worked without any issues, and are the exact same setup as I have. That got me thinking "outside" the script itself. I was kinda onto it myself in my previous post about json cookie.
I had to do a rebuild and restore a couple of days ago after some issues after an upgrade from Ubuntu 18.04 to 20.04. And when I installed Domoticz from scratch I noticed the install script did change some of the permissions on the files and folders in the domoticz folder (could'nt understand why it actually did that because the script was executed as user, not sudo.). But anyway after correcting the permissions after the install script messed it up I forgot one file.. domocookie.txt.. That was still left with root permissions making the user that runs the domoticz service unable to update the content of it. I figured that file must be where the json cookie is stored. After deleting the file restart of domoticz the file was recreated with the correct permission and...... drums... it worked as intended.
Must say that this could have been added as an event message in Domoticz for dzVents to inform of such an issue. Why couldnt Domoticz just have thrown an event indicating that it was unable to hold/update this file... Well, lost some hours troubleshooting this.
Thanks again Delfuego, I will actually use your script going forward as I also have noticed my previous script did have the tendency to update the phones status too often as disconnected from the network even though it was for a very short time. So atleast some positive outcome from this.. ;-)
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

Maybe this is useful info when trying to analsye why we are getting the "Error opening url" errors. I checked the time when I get these errors and there seems to be a pattern in the error reports, at 7.00, 8.00 and 23.00 I'm getting most of these errors. At this time I've other DZvents scripts running to check some Nuki door locks. Could this be the reason why I get these error messages?

Code: Select all

2020-11-27 23:00:04.517 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-28 07:00:08.013 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-28 23:00:07.647 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-29 07:00:07.767 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-29 08:00:04.898 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-29 20:25:01.380 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-29 23:00:06.225 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-30 07:00:07.412 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-11-30 23:00:07.605 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-12-01 07:00:05.249 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
2020-12-01 08:00:06.081 Error: Error opening url: https://192.168.0.25:8443/api/s/default/stat/sta
I will see if I can change the moment when this Unifi MAC checking script is executed. When checking at a slightly different time interval the problem good be gone.
labrag12
Posts: 14
Joined: Monday 15 March 2021 16:47
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by labrag12 »

Hi,

First thank you for sharing this script.
unfortunately i'm not able to get it working.
I changed the ip adress and checked the port.
i changed the name of the side, but the script is not able to login to the controller.

i keep getting this mesage, am i'm missing something ?


2021-03-22 13:55:10.517 Status: dzVents: Info: Handling httpResponse-events for: "loggedin"
2021-03-22 13:55:10.517 Status: dzVents: Info: ------ Start external script: gsm-monitor.lua: HTTPResponse: "loggedin"
2021-03-22 13:55:10.517 Status: dzVents: Info: ------ Finished gsm-monitor.lua
2021-03-22 13:55:10.497 Error: Error opening url: https://192.168.2.12:8443/api/login
2021-03-22 13:55:10.517 Error: dzVents: Error: (3.0.2) HTTP/1.1 response: 28 ==>> Timeout was reached
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego »

Hi,

I'm not sure but based on your short shared log I wonder if you are using the same URL twice?

After login you should use another URL:

Code: Select all

https://192.168.1.1:8443/api/s/default/stat/sta
Please also check if you changed from default in your Unifi setup to a custom name. You can check this by login into your Unifi controller dashboard and look into the URL of your browser.

Code: Select all

https://192.168.1.1:8443/manage/site/default/dashboard
In your case default is replaced by your custom name or number (not sure what exact it will be; I only use default).

Kind regards,

Luuk
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest