Page 1 of 1

How can I use a string as an attribute.

Posted: Saturday 11 August 2018 16:50
by BertB
Hi,
I started working with dcVents a couple of days ago. I love it, but I want to do something with reading a devices attributes and don't know how to.
In a function, I parse through a variable with the names of devices I want to read. That works well, but from some devices I want to read the state and from others I like to read a value like current or color or whatever.
So, I read the devices description and in the first line, I wrote the name of the attribute I need to read.

Lets assume I have a switch named Switch1, of which I want to read the nValue.
I can turn the string 'Switch1' into a device with MyDevice = domoticz.devices('Switch1')
When I read the descriptiption I can put the desired attribute into another string like MyAttrib = MyDevice.description. That works fine, but MyAttrib is not a real attribute, so MyValue=MyDevice.MyAttrib does not work.

Is this possible at all?

Thanks,
Bert

Re: How can I use a string as an attribute.

Posted: Sunday 12 August 2018 17:27
by waaren
BertB wrote: Saturday 11 August 2018 16:50 Hi,
I started working with dcVents a couple of days ago. I love it, but I want to do something with reading a devices attributes and don't know how to.
In a function, I parse through a variable with the names of devices I want to read. That works well, but from some devices I want to read the state and from others I like to read a value like current or color or whatever.
So, I read the devices description and in the first line, I wrote the name of the attribute I need to read.

Lets assume I have a switch named Switch1, of which I want to read the nValue.
I can turn the string 'Switch1' into a device with MyDevice = domoticz.devices('Switch1')
When I read the descriptiption I can put the desired attribute into another string like

Code: Select all

MyAttrib = MyDevice.description
. That works fine, but MyAttrib is not a real attribute, so MyValue=MyDevice.MyAttrib does not work.

Is this possible at all?

Thanks,
Bert
@Bertb,

Your MyDevice is a lua table with all attribute names as keys.
So the correct syntax for your requirement is

Code: Select all

MyDevice = domoticz.devices('Switch1')
MyAttrib = MyDevice.description
MyValue = MyDevice[MyAttrib]
tested and working (version V4.9796) with

Code: Select all

return {
   on = {
      timer = { 'every minute'
   }},
   execute = function(dz)
      local sensor = dz.devices('temperatuur Rotterdam')
      local attribute = "temperature"
      dz.log(sensor[attribute])
   end
}

Re: How can I use a string as an attribute.

Posted: Sunday 12 August 2018 19:16
by BertB
Thank you so mutch for pointing me in the right direction