UniFi Energy Report - How can I retrieve the value via http ?
-
Hi,
I have a piece of code that returns the Watts used by all the PoE devices on my home network. I’m trying to set it up on Vera so that can retrieve that value in a similar way I would retrieve a Vera variable via a http request.
The function works returns the total value when run via LuaTest, but when I try to retrieve it via luup.register_ handler, for some reason it returns the entire UniFi json file ??
Please could some help me with what’s wrong, my goal is to be able to retrieve the value , like an api so I can use it elsewhere ..
luup.register_handler("UnifiControllerPoE","UnifiWatts") function UnifiControllerPoE() local unifi_username = "unifi" local unifi_password = "unifi" local unifi_controller_ip = "192.168.1.207" local unifi_controller_port = "8443" local cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file local tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file local device = 'api/s/default/stat/device' local f = io.popen("stat -c %Y " .. tempfilename) local last_modified = f:read() --if (os.difftime (os.time(), last_modified) > 30) then -- URL for logging in, query and logging out url_login = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -H \'Content-Type: application/json\' -X POST -d \'{"password":"' .. unifi_password .. '","username":"' .. unifi_username .. '"}\' https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/login' url_open = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -s -o '..tempfilename..' --data "json={}" https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/' ..device url_logout = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/logout' -- Execute url -- Execute url read_login = os.execute(url_login) read_open = os.execute(url_open) read_logout = os.execute(url_logout) --end local line local file = io.open(tempfilename, "r") if file then line = file:read("*a") file:close() end return line end --print(UnifiController()) local readings = {} local total = 0 local json = require "dkjson" local u = json.decode( UnifiControllerPoE() ) --print('\n') for key,value in ipairs( u.data ) do for n, p in ipairs( value.port_table ) do --print('\t'.. tostring(p.port_idx), p.poe_enable, tonumber(p.poe_power)) -- print(p.port_idx) table.insert( readings, { value.name, tonumber(p.poe_power)}) -- populate readings table end end for _, reading in ipairs(readings) do local Variable = reading[1] local Value = reading[2] or 0 total = total + Value if Value ~= 0 then --print(Variable.. " - ".. Value, total) end end print("Total watts = " ..total) local html = "<html><head>" .. "</head>" .. "<body>" .. "<b>" .. tostring(total) .. '</b>' .. "</body></html>" return html, "text/html"
-
Hi,
I have a piece of code that returns the Watts used by all the PoE devices on my home network. I’m trying to set it up on Vera so that can retrieve that value in a similar way I would retrieve a Vera variable via a http request.
The function works returns the total value when run via LuaTest, but when I try to retrieve it via luup.register_ handler, for some reason it returns the entire UniFi json file ??
Please could some help me with what’s wrong, my goal is to be able to retrieve the value , like an api so I can use it elsewhere ..
luup.register_handler("UnifiControllerPoE","UnifiWatts") function UnifiControllerPoE() local unifi_username = "unifi" local unifi_password = "unifi" local unifi_controller_ip = "192.168.1.207" local unifi_controller_port = "8443" local cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file local tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file local device = 'api/s/default/stat/device' local f = io.popen("stat -c %Y " .. tempfilename) local last_modified = f:read() --if (os.difftime (os.time(), last_modified) > 30) then -- URL for logging in, query and logging out url_login = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -H \'Content-Type: application/json\' -X POST -d \'{"password":"' .. unifi_password .. '","username":"' .. unifi_username .. '"}\' https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/login' url_open = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -s -o '..tempfilename..' --data "json={}" https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/' ..device url_logout = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/logout' -- Execute url -- Execute url read_login = os.execute(url_login) read_open = os.execute(url_open) read_logout = os.execute(url_logout) --end local line local file = io.open(tempfilename, "r") if file then line = file:read("*a") file:close() end return line end --print(UnifiController()) local readings = {} local total = 0 local json = require "dkjson" local u = json.decode( UnifiControllerPoE() ) --print('\n') for key,value in ipairs( u.data ) do for n, p in ipairs( value.port_table ) do --print('\t'.. tostring(p.port_idx), p.poe_enable, tonumber(p.poe_power)) -- print(p.port_idx) table.insert( readings, { value.name, tonumber(p.poe_power)}) -- populate readings table end end for _, reading in ipairs(readings) do local Variable = reading[1] local Value = reading[2] or 0 total = total + Value if Value ~= 0 then --print(Variable.. " - ".. Value, total) end end print("Total watts = " ..total) local html = "<html><head>" .. "</head>" .. "<body>" .. "<b>" .. tostring(total) .. '</b>' .. "</body></html>" return html, "text/html"
-
Thanks so much @therealdb - Such a silly mistake, for some reason I was thinking it would run the whole piece of code, when i should have realised it will only run the function part, as that’s all that’s being called! Doh !
.
-
Sorted ! - thanks again @therealdb , I had to break it up over two functions in the end - but it works a treat !
luup.register_handler("rununfiwattagereport","unifi") function UnifiControllerPoE() local unifi_username = "vera" local unifi_password = "vera" local unifi_controller_ip = "192.168.102.207" local unifi_controller_port = "8443" local cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file local tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file local device = 'api/s/default/stat/device' local f = io.popen("stat -c %Y " .. tempfilename) local last_modified = f:read() --if (os.difftime (os.time(), last_modified) > 30) then -- URL for logging in, query and logging out url_login = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -H \'Content-Type: application/json\' -X POST -d \'{"password":"' .. unifi_password .. '","username":"' .. unifi_username .. '"}\' https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/login' url_open = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -s -o '..tempfilename..' --data "json={}" https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/' ..device url_logout = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/logout' -- Execute url -- Execute url read_login = os.execute(url_login) read_open = os.execute(url_open) read_logout = os.execute(url_logout) local line local file = io.open(tempfilename, "r") if file then line = file:read("*a") file:close() end return line end function rununfiwattagereport() local readings = {} local total = 0 local json = require "dkjson" local u = json.decode( UnifiControllerPoE() ) --print('\n') for key,value in ipairs( u.data ) do for n, p in ipairs( value.port_table ) do --print('\t'.. tostring(p.port_idx), p.poe_enable, tonumber(p.poe_power)) -- print(p.port_idx) table.insert( readings, { value.name, tonumber(p.poe_power)}) -- populate readings table end end for _, reading in ipairs(readings) do local Variable = reading[1] local Value = reading[2] or 0 total = total + Value if Value ~= 0 then --print(Variable.. " - ".. Value, total) end end print("Total watts = " ..total) local html = "<html><head>" .. "</head>" .. "<body>" .. "<b>" .. tostring(total) .. '</b>' .. "</body></html>" return html, "text/html" end