Get Device subtype?

Moderator: leecollings

Post Reply
Keesje
Posts: 6
Joined: Sunday 03 November 2019 13:53
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Contact:

Get Device subtype?

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get Device subtype?

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Keesje
Posts: 6
Joined: Sunday 03 November 2019 13:53
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Contact:

Re: Get Device subtype?

Post by Keesje »

That's lua script is more complicated than expected.
Thanks for your contribution.
Kees
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get Device subtype?

Post 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?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest