Use string to get defined variable
Posted: Thursday 28 June 2018 22:56
I have declared 5 variables (which define the 5 micro solar inverter devices) like this:
then I would like to run a loop 'for i = 1, 5 do' (do some calculations with the daily generated energy per micro inverter, by comparing the daily energy generation from inverter 1 to 4 with the value of inverter 5).
So I set up my code like this:
Unfortunately the value of 'inverterValue' is 'nil', while it has the correct value when I use inv1.WhToday.
My conclusion is that 'inverterName' doesn't inherit the defined variable in the for..next loop.
I looked all over the place, found tips about using global variables (don't need those) or tables (don't understand how to apply them).
I could already add the '.WhToday' to my variable declaration, but I think it end up with the same trouble that the generated string inv1 is not equal to variable inv1.
Code: Select all
local inv1 = domoticz.devices('11000xxx power')
local inv2 = domoticz.devices('110007xxx power')
local inv3 = domoticz.devices('1100xxx2 power')
local inv4 = domoticz.devices('100xxxx9 power')
local inv5 = domoticz.devices('100Xxxx8 power')So I set up my code like this:
Code: Select all
for i = 1,4 do
inverterName = 'inv'..i
inverterValue = inverterName.WhToday
etc
etcMy conclusion is that 'inverterName' doesn't inherit the defined variable in the for..next loop.
I looked all over the place, found tips about using global variables (don't need those) or tables (don't understand how to apply them).
I could already add the '.WhToday' to my variable declaration, but I think it end up with the same trouble that the generated string inv1 is not equal to variable inv1.