Logic Machine Forum
dynamic variable name - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: dynamic variable name (/showthread.php?tid=3225)



dynamic variable name - gjniewenhuijse - 15.03.2021

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



RE: dynamic variable name - admin - 15.03.2021

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



RE: dynamic variable name - gjniewenhuijse - 15.03.2021

(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.


RE: dynamic variable name - admin - 15.03.2021

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



RE: dynamic variable name - gjniewenhuijse - 15.03.2021

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