How can I use a string as an attribute.

Easy to use, 100% Lua-based event scripting framework.
Post Reply
BertB
Posts: 220
Joined: Tuesday 24 June 2014 19:02
Target OS: Raspberry Pi
Domoticz version:
Location: struggling and stumbling. Amsterdam area
Contact:

How can I use a string as an attribute.

Post by BertB » 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 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

User avatar
waaren
Posts: 798
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Sunday 12 August 2018 17:27

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
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

BertB
Posts: 220
Joined: Tuesday 24 June 2014 19:02
Target OS: Raspberry Pi
Domoticz version:
Location: struggling and stumbling. Amsterdam area
Contact:

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

Post by BertB » Sunday 12 August 2018 19:16

Thank you so mutch for pointing me in the right direction

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest