Unifi controller api get connected device mac address

Discussion in 'C-Bus Automation Controllers' started by Pie Boy, Mar 27, 2020 at 8:07 AM.

  1. Pie Boy

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    51
    Likes Received:
    7
    Location:
    New Zealand
    This code communicates from SHAC to the unif controller api and returns connected devices by mac address. im using this to set my away scene once i leave the house or disconnect from the wifi..... but im checking more than one device - so im hoping that should give me the desired effect

    im running this code in a resident script every 60 sec, change username, password and controller_ip and port to suit

    Code:
    htps = require("ssl.https")
     require('json')
    
    htps.TIMEOUT = 5
    
    usr_credenitals = json.encode({
      username = 'admin',
      password = 'admin123456'
    })
    
    controller_ip = '192.168.1.10'
    controller_port = '8443'
    
    login ='https://'.. controller_ip..':'..controller_port..'/api/login'
    sta = 'https://' .. controller_ip..':'..controller_port..'/api/s/default/stat/sta'
    
    Post_response = {}
    
    local res, err, response_headers, status = htps.request
      {
        url = login,
        method = "POST",
        protocol = 'tlsv12',
        headers =
        {
        ["Accept"] = "*/*",
        ["Content-Type"] = "application/x-www-form-urlencoded",
        ["Content-Length"] = usr_credenitals:len()
        },
    
      source = ltn12.source.string(usr_credenitals),    
      sink = ltn12.sink.table(Post_response)
      }
    
    if Post_response then
     -- log(Post_response)
      --log(res, code, response_headers
    
      mmm_cookies = response_headers['set-cookie']
      if mmm_cookies then
      session = mmm_cookies:match('(unifises=[^;]+)')
      token = mmm_cookies:match('(csrf_token=[^;]+)')
    
      cookie = session .. '; ' .. token
      end
    
      Get_response ={}
    
      res, err = htps.request({
         url = sta,
         method = "GET",
         protocol = 'tlsv12',
         headers = {
        cookie = cookie
        },
        sink = ltn12.sink.table(Get_response),
      })
    
    
      if Get_response then
        Get_response = table.concat(Get_response)
        dec = json.decode(Get_response)
    
        -- get all mac adresses currently connected to site
        for i = 1, #dec.data do
          log(dec.data[i].mac)
          -- return boolean if mac adress is connected
     -- replace string at end with known mac adress of device you want to moniter
         is_mac_1 = dec.data[i].mac =='90:e1:7b:6f:48:2e'
         is_mac_2 = dec.data[i].mac =='90:e1:79:6f:22:2e'
        end
    
      -- check if mac 1&2 are both false then set scene
    if not is_mac_1 and not is_mac_2  then
            --- set scene  here
    end
    
      else
        log('could not get stat ' .. tostring(err))
      end
      else
      log('login failed ' .. tostring(err))
    end
    
    
     
    Last edited: Mar 27, 2020 at 8:42 AM
    Pie Boy, Mar 27, 2020 at 8:07 AM
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.