Page 1 of 1

Get Device subtype?

Posted: Saturday 19 December 2020 15:53
by Keesje
Hi all,

I have searched already but cannot find a way to get the Type or SubType (e.g. like Switch, Temp, General, etc) of a known Device Idx using LUA-scripting.
How can I retrieve this information?

Thanks in advance,
Kees

Re: Get Device subtype?

Posted: Saturday 19 December 2020 17:13
by waaren
Keesje wrote: Saturday 19 December 2020 15:53 Hi all,

I have searched already but cannot find a way to get the Type or SubType (e.g. like Switch, Temp, General, etc) of a known Device Idx using LUA-scripting.
How can I retrieve this information?

Thanks in advance,
Kees
in dzVents

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every minute',
        },
    },

    execute = function(dz)
        item = dz.devices(3)
        
        dz.log(item.name .. ':  Type    => ' .. item.deviceType)
        dz.log(item.name .. ':  Type    => ' .. item.deviceSubType)
    end
}
In classic Lua

Code: Select all

commandArray = {}

package.path = 'scripts/lua/?.lua;' .. package.path -- prepend to package path to allow use of mudules in scripts/lua

local idx = 3
local port = 8084  -- change to your domoticz port number
local ip = '127.0.0.1' -- or localhost or real domoticz IP (make sure source IP does not require password

local json = '/json.htm?type=devices\\&rid=' .. idx
local url = 'http://' .. ip .. ':' .. port .. json


local  function tprint (t, indent, done)
  local done = done or {}
  indent = indent or 0
  for key, value in pairs (t) do
    pre = (string.rep (" ", indent)) -- indent it
    if type (value) == "table" and not done[value] then
      done [value] = true
      print (pre .. tostring (key) .. ":");
      tprint (value, indent + 2, done)
    elseif type(value) == 'function' and not done[value] then
      print( pre .. (tostring (key) .. "()"))
    else
      pre = pre .. (tostring (key) .. ": ")
      print (pre .. tostring(value))
    end
  end
end

local function osCommand(cmd)
    local file = assert ( io.popen(cmd) )
    local output = assert ( file:read('*all') )
    local rc = { file:close() }
    return output, rc[3]
end

result = osCommand('curl ' .. url  )

local JSON = require "JSON"     -- use generic JSON.lua
local jsonParse = JSON:decode(result)
-- tprint (jsonParse) -- debug only

local attributes = jsonParse.result[1]


print (attributes.Name .. ':  Type    => ' .. attributes.Type)
print (attributes.Name .. ':  SubType => ' .. attributes.SubType)

return commandArray

Re: Get Device subtype?

Posted: Saturday 19 December 2020 22:25
by Keesje
That's lua script is more complicated than expected.
Thanks for your contribution.
Kees

Re: Get Device subtype?

Posted: Saturday 19 December 2020 22:41
by waaren
Keesje wrote: Saturday 19 December 2020 22:25 That's lua script is more complicated than expected.
Thanks for your contribution.
Kees
Maybe a bit less if you leave out the tprint function which is only needed when debugging the script and identify all return values from the json?