How can I use a string as an attribute.

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

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

How can I use a string as an attribute.

Post 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
Set 1= RPI 3B, latest Beta, RFLink USB, RFLINK WIFI, Zwave
Set 2= RPI 2B, Latest Beta, OTGW (USB), SBFSpot (Bluetooth)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BertB
Posts: 72
Joined: Tuesday 24 June 2014 19:02
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: struggling and stumbling. Amsterdam area
Contact:

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

Post by BertB »

Thank you so mutch for pointing me in the right direction
Set 1= RPI 3B, latest Beta, RFLink USB, RFLINK WIFI, Zwave
Set 2= RPI 2B, Latest Beta, OTGW (USB), SBFSpot (Bluetooth)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest