This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

dynamic variable name
#1
i like to get data from a table, but the variable name is dynamic. See my example:


Code:
-- devices
devices = {
  {'9d', 'SENSOR', '8/4/5', 'v.hum'},
  {'9e', 'COMMON', '8/4/6', 'v.tst'}
}

-- data get from webservice
v = {hum=1, tst='abc'}
log(v.hum, v.tst)

-- print some data from v, based on data in devices
for k,k_items in pairs(devices) do
  -- log variable name
  log(k_items[4])
  -- log the data in variable with name k_items[4], for example the data in v.hum or v.tst (1 or abc)
end
Reply
#2
Like this, and make sure that v is defined before devices:
Code:
-- data get from webservice
v = {hum=1, tst='abc'}
log(v.hum, v.tst)

-- devices
devices = {
  {'9d', 'SENSOR', '8/4/5', v.hum},
  {'9e', 'COMMON', '8/4/6', v.tst}
}

-- print some data from v, based on data in devices
for k,k_items in pairs(devices) do
  -- log variable name
  log(k_items[4])
  -- log the data in variable with name k_items[4], for example the data in v.hum or v.tst (1 or abc)
end
Reply
#3
(15.03.2021, 13:02)admin Wrote: Like this, and make sure that v is defined before devices:
Code:
-- data get from webservice
v = {hum=1, tst='abc'}
log(v.hum, v.tst)

-- devices
devices = {
  {'9d', 'SENSOR', '8/4/5', v.hum},
  {'9e', 'COMMON', '8/4/6', v.tst}
}

-- print some data from v, based on data in devices
for k,k_items in pairs(devices) do
  -- log variable name
  log(k_items[4])
  -- log the data in variable with name k_items[4], for example the data in v.hum or v.tst (1 or abc)
end

No thats not possible, because my devices are defined before i get the data from the webservice.
Now i defined v hardcoded, but normally this thats comes from the web and is parsed.
Reply
#4
Then do it like this:
Code:
-- devices
devices = {
  {'9d', 'SENSOR', '8/4/5', 'hum'},
  {'9e', 'COMMON', '8/4/6', 'tst'}
}

-- data get from webservice
v = {hum=1, tst='abc'}
log(v.hum, v.tst)

-- print some data from v, based on data in devices
for k,k_items in pairs(devices) do
  key = k_items[4]
  val = v[ key ]
  -- log variable name
  log(val)
  -- log the data in variable with name k_items[4], for example the data in v.hum or v.tst (1 or abc)
end
Reply
#5
yeah.. thanks

i need the value 'attributes.soilHumidity.value'

how to do this in a nested table?



Code:
["type"]
  * string: SENSOR
["attributes"]
  * table:
   ["soilTemperature"]
    * table:
     ["timestamp"]
      * string: 2020-10-31T14:49:03.034+0000
     ["value"]
      * number: 13
   ["lightIntensity"]
    * table:
     ["timestamp"]
      * string: 2020-10-31T14:49:16.260+0000
     ["value"]
      * number: 7
   ["soilHumidity"]
    * table:
     ["timestamp"]
      * string: 2020-10-31T14:49:03.004+0000
     ["value"]
      * number: 100
   ["ambientTemperature"]
    * table:
     ["timestamp"]
      * string: 2020-10-31T14:49:17.023+0000
     ["value"]
      * number: 16
["relationships"]
  * table:
   ["device"]
    * table:
     ["data"]
      * table:
       ["type"]
        * string: DEVICE
       ["id"]
        * string: 40940964969046840
["id"]
  * string: 5647657567567567567

here my new example code
Code:
-- devices
devices = {
  {'9d', 'SENSOR', '8/4/5', 'attributes.soilHumidity.value'},
  {'9e', 'COMMON', '8/4/6', 'attributes.batteryLevel.value'}
}

-- data get from webservice
v = { attributes= { soilHumidity={value=1},batteryLevel={value=100} } }
log(v)
log(v.attributes.soilHumidity.value)
log(v.attributes.batteryLevel.value)

-- print some data from v, based on data in devices (dynamic)
for k,k_items in pairs(devices) do
  key = k_items[4]
  val = v[ key ]
  log(key, val)
end
Reply


Forum Jump: